2019-02-01 14:08:21 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2018-03-30 13:56:59 -04:00
|
|
|
package stats
|
|
|
|
|
2018-09-30 17:08:41 -04:00
|
|
|
//go:generate errorgen
|
2018-03-30 13:56:59 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
"v2ray.com/core/features/stats"
|
2018-03-30 13:56:59 -04:00
|
|
|
)
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// Counter is an implementation of stats.Counter.
|
2018-03-30 13:56:59 -04:00
|
|
|
type Counter struct {
|
|
|
|
value int64
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// Value implements stats.Counter.
|
2018-03-30 13:56:59 -04:00
|
|
|
func (c *Counter) Value() int64 {
|
|
|
|
return atomic.LoadInt64(&c.value)
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// Set implements stats.Counter.
|
2018-03-30 17:17:28 -04:00
|
|
|
func (c *Counter) Set(newValue int64) int64 {
|
2018-03-30 13:56:59 -04:00
|
|
|
return atomic.SwapInt64(&c.value, newValue)
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// Add implements stats.Counter.
|
2018-03-30 13:56:59 -04:00
|
|
|
func (c *Counter) Add(delta int64) int64 {
|
|
|
|
return atomic.AddInt64(&c.value, delta)
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// Manager is an implementation of stats.Manager.
|
2018-03-30 13:56:59 -04:00
|
|
|
type Manager struct {
|
|
|
|
access sync.RWMutex
|
|
|
|
counters map[string]*Counter
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager(ctx context.Context, config *Config) (*Manager, error) {
|
2018-03-31 04:30:12 -04:00
|
|
|
m := &Manager{
|
2018-03-30 13:56:59 -04:00
|
|
|
counters: make(map[string]*Counter),
|
2018-03-31 04:30:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
2018-03-30 13:56:59 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
func (*Manager) Type() interface{} {
|
|
|
|
return stats.ManagerType()
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
func (m *Manager) RegisterCounter(name string) (stats.Counter, error) {
|
2018-03-30 13:56:59 -04:00
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
|
|
|
if _, found := m.counters[name]; found {
|
|
|
|
return nil, newError("Counter ", name, " already registered.")
|
|
|
|
}
|
2018-03-31 04:30:12 -04:00
|
|
|
newError("create new counter ", name).AtDebug().WriteToLog()
|
2018-03-30 13:56:59 -04:00
|
|
|
c := new(Counter)
|
|
|
|
m.counters[name] = c
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
func (m *Manager) GetCounter(name string) stats.Counter {
|
2018-03-30 13:56:59 -04:00
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
|
|
|
if c, found := m.counters[name]; found {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
func (m *Manager) Visit(visitor func(string, stats.Counter) bool) {
|
2018-07-10 17:40:58 -04:00
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
|
|
|
for name, c := range m.counters {
|
|
|
|
if !visitor(name, c) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:20:24 -04:00
|
|
|
// Start implements common.Runnable.
|
2018-03-30 13:56:59 -04:00
|
|
|
func (m *Manager) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:20:24 -04:00
|
|
|
// Close implement common.Closable.
|
2018-03-30 13:56:59 -04:00
|
|
|
func (m *Manager) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|