1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-13 19:30:43 +00:00
v2fly/app/stats/stats.go
2018-09-30 23:08:41 +02:00

97 lines
1.9 KiB
Go

package stats
//go:generate errorgen
import (
"context"
"sync"
"sync/atomic"
"v2ray.com/core"
)
// Counter is an implementation of core.StatCounter.
type Counter struct {
value int64
}
// Value implements core.StatCounter.
func (c *Counter) Value() int64 {
return atomic.LoadInt64(&c.value)
}
// Set implements core.StatCounter.
func (c *Counter) Set(newValue int64) int64 {
return atomic.SwapInt64(&c.value, newValue)
}
// Add implements core.StatCounter.
func (c *Counter) Add(delta int64) int64 {
return atomic.AddInt64(&c.value, delta)
}
// Manager is an implementation of core.StatManager.
type Manager struct {
access sync.RWMutex
counters map[string]*Counter
}
func NewManager(ctx context.Context, config *Config) (*Manager, error) {
m := &Manager{
counters: make(map[string]*Counter),
}
v := core.FromContext(ctx)
if v != nil {
if err := v.RegisterFeature((*core.StatManager)(nil), m); err != nil {
return nil, newError("failed to register StatManager").Base(err)
}
}
return m, nil
}
func (m *Manager) RegisterCounter(name string) (core.StatCounter, error) {
m.access.Lock()
defer m.access.Unlock()
if _, found := m.counters[name]; found {
return nil, newError("Counter ", name, " already registered.")
}
newError("create new counter ", name).AtDebug().WriteToLog()
c := new(Counter)
m.counters[name] = c
return c, nil
}
func (m *Manager) GetCounter(name string) core.StatCounter {
m.access.RLock()
defer m.access.RUnlock()
if c, found := m.counters[name]; found {
return c
}
return nil
}
func (m *Manager) Visit(visitor func(string, core.StatCounter) bool) {
m.access.RLock()
defer m.access.RUnlock()
for name, c := range m.counters {
if !visitor(name, c) {
break
}
}
}
// Start implements common.Runnable.
func (m *Manager) Start() error {
return nil
}
// Close implement common.Closable.
func (m *Manager) Close() error {
return nil
}