You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/stats.go

88 lines
1.5 KiB

7 years ago
package core
import (
"sync"
)
type StatCounter interface {
Value() int64
Set(int64) int64
7 years ago
Add(int64) int64
}
type StatManager interface {
Feature
RegisterCounter(string) (StatCounter, error)
GetCounter(string) StatCounter
}
// 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)
}
7 years ago
type syncStatManager struct {
sync.RWMutex
StatManager
}
func (s *syncStatManager) Start() error {
s.RLock()
defer s.RUnlock()
if s.StatManager == nil {
return nil
7 years ago
}
return s.StatManager.Start()
}
func (s *syncStatManager) Close() error {
s.RLock()
defer s.RUnlock()
if s.StatManager == nil {
return nil
7 years ago
}
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) {
if m == nil {
return
}
7 years ago
s.Lock()
defer s.Unlock()
7 years ago
if s.StatManager != nil {
s.StatManager.Close() // nolint: errcheck
7 years ago
}
7 years ago
s.StatManager = m
}