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/app/stats/stats.go

96 lines
1.8 KiB

// +build !confonly
7 years ago
package stats
6 years ago
//go:generate errorgen
7 years ago
import (
"context"
"sync"
"sync/atomic"
"v2ray.com/core/features/stats"
7 years ago
)
// Counter is an implementation of stats.Counter.
7 years ago
type Counter struct {
value int64
}
// Value implements stats.Counter.
7 years ago
func (c *Counter) Value() int64 {
return atomic.LoadInt64(&c.value)
}
// Set implements stats.Counter.
func (c *Counter) Set(newValue int64) int64 {
7 years ago
return atomic.SwapInt64(&c.value, newValue)
}
// Add implements stats.Counter.
7 years ago
func (c *Counter) Add(delta int64) int64 {
return atomic.AddInt64(&c.value, delta)
}
// Manager is an implementation of stats.Manager.
7 years ago
type Manager struct {
access sync.RWMutex
counters map[string]*Counter
}
func NewManager(ctx context.Context, config *Config) (*Manager, error) {
m := &Manager{
7 years ago
counters: make(map[string]*Counter),
}
return m, nil
7 years ago
}
func (*Manager) Type() interface{} {
return stats.ManagerType()
}
func (m *Manager) RegisterCounter(name string) (stats.Counter, error) {
7 years ago
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()
7 years ago
c := new(Counter)
m.counters[name] = c
return c, nil
}
func (m *Manager) GetCounter(name string) stats.Counter {
7 years ago
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, stats.Counter) bool) {
m.access.RLock()
defer m.access.RUnlock()
for name, c := range m.counters {
if !visitor(name, c) {
break
}
}
}
7 years ago
// Start implements common.Runnable.
7 years ago
func (m *Manager) Start() error {
return nil
}
7 years ago
// Close implement common.Closable.
7 years ago
func (m *Manager) Close() error {
return nil
}