1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/app/stats/stats.go

94 lines
1.7 KiB
Go
Raw Normal View History

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"
"v2ray.com/core/features/stats"
2018-03-30 13:56:59 -04:00
)
// Counter is an implementation of stats.Counter.
2018-03-30 13:56:59 -04:00
type Counter struct {
value int64
}
// Value implements stats.Counter.
2018-03-30 13:56:59 -04:00
func (c *Counter) Value() int64 {
return atomic.LoadInt64(&c.value)
}
// 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)
}
// 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)
}
// 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()
}
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
}
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
}
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
}