1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/stats.go

88 lines
1.5 KiB
Go
Raw Normal View History

2018-03-30 13:56:59 -04:00
package core
import (
"sync"
)
type StatCounter interface {
Value() int64
2018-03-30 17:17:28 -04:00
Set(int64) int64
2018-03-30 13:56:59 -04:00
Add(int64) int64
}
type StatManager interface {
Feature
RegisterCounter(string) (StatCounter, error)
GetCounter(string) StatCounter
}
2018-04-11 18:10:14 -04:00
// GetOrRegisterStatCounter tries to get the StatCounter first. If not exist, it then tries to create a new counter.
func GetOrRegisterStatCounter(m StatManager, name string) (StatCounter, error) {
counter := m.GetCounter(name)
if counter != nil {
return counter, nil
}
return m.RegisterCounter(name)
}
2018-03-30 13:56:59 -04:00
type syncStatManager struct {
sync.RWMutex
StatManager
}
func (s *syncStatManager) Start() error {
s.RLock()
defer s.RUnlock()
if s.StatManager == nil {
2018-03-31 04:30:12 -04:00
return nil
2018-03-30 13:56:59 -04:00
}
return s.StatManager.Start()
}
func (s *syncStatManager) Close() error {
s.RLock()
defer s.RUnlock()
if s.StatManager == nil {
2018-03-31 04:30:12 -04:00
return nil
2018-03-30 13:56:59 -04:00
}
return s.StatManager.Close()
}
func (s *syncStatManager) RegisterCounter(name string) (StatCounter, error) {
s.RLock()
defer s.RUnlock()
if s.StatManager == nil {
return nil, newError("StatManager not set.")
}
return s.StatManager.RegisterCounter(name)
}
func (s *syncStatManager) GetCounter(name string) StatCounter {
s.RLock()
defer s.RUnlock()
if s.StatManager == nil {
return nil
}
return s.StatManager.GetCounter(name)
}
func (s *syncStatManager) Set(m StatManager) {
2018-04-03 05:03:03 -04:00
if m == nil {
return
}
2018-03-30 13:56:59 -04:00
s.Lock()
defer s.Unlock()
2018-04-03 05:40:21 -04:00
if s.StatManager != nil {
2018-05-31 05:55:11 -04:00
s.StatManager.Close() // nolint: errcheck
2018-04-03 05:40:21 -04:00
}
2018-03-30 13:56:59 -04:00
s.StatManager = m
}