forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
87 lines (69 loc) · 1.48 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package core
import (
"sync"
)
type StatCounter interface {
Value() int64
Set(int64) int64
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)
}
type syncStatManager struct {
sync.RWMutex
StatManager
}
func (s *syncStatManager) Start() error {
s.RLock()
defer s.RUnlock()
if s.StatManager == nil {
return nil
}
return s.StatManager.Start()
}
func (s *syncStatManager) Close() error {
s.RLock()
defer s.RUnlock()
if s.StatManager == nil {
return nil
}
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
}
s.Lock()
defer s.Unlock()
if s.StatManager != nil {
s.StatManager.Close() // nolint: errcheck
}
s.StatManager = m
}