1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 17:35:23 +00:00
v2fly/app/stats/stats.go

96 lines
1.8 KiB
Go
Raw Normal View History

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