stats.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package stats
  2. //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg stats -path App,Stats
  3. import (
  4. "context"
  5. "sync"
  6. "sync/atomic"
  7. "v2ray.com/core"
  8. )
  9. // Counter is an implementation of core.StatCounter.
  10. type Counter struct {
  11. value int64
  12. }
  13. // Value implements core.StatCounter.
  14. func (c *Counter) Value() int64 {
  15. return atomic.LoadInt64(&c.value)
  16. }
  17. // Set implements core.StatCounter.
  18. func (c *Counter) Set(newValue int64) int64 {
  19. return atomic.SwapInt64(&c.value, newValue)
  20. }
  21. // Add implements core.StatCounter.
  22. func (c *Counter) Add(delta int64) int64 {
  23. return atomic.AddInt64(&c.value, delta)
  24. }
  25. // Manager is an implementation of core.StatManager.
  26. type Manager struct {
  27. access sync.RWMutex
  28. counters map[string]*Counter
  29. }
  30. func NewManager(ctx context.Context, config *Config) (*Manager, error) {
  31. m := &Manager{
  32. counters: make(map[string]*Counter),
  33. }
  34. v := core.FromContext(ctx)
  35. if v != nil {
  36. if err := v.RegisterFeature((*core.StatManager)(nil), m); err != nil {
  37. return nil, newError("failed to register StatManager").Base(err)
  38. }
  39. }
  40. return m, nil
  41. }
  42. func (m *Manager) RegisterCounter(name string) (core.StatCounter, error) {
  43. m.access.Lock()
  44. defer m.access.Unlock()
  45. if _, found := m.counters[name]; found {
  46. return nil, newError("Counter ", name, " already registered.")
  47. }
  48. newError("create new counter ", name).AtDebug().WriteToLog()
  49. c := new(Counter)
  50. m.counters[name] = c
  51. return c, nil
  52. }
  53. func (m *Manager) GetCounter(name string) core.StatCounter {
  54. m.access.RLock()
  55. defer m.access.RUnlock()
  56. if c, found := m.counters[name]; found {
  57. return c
  58. }
  59. return nil
  60. }
  61. func (m *Manager) Start() error {
  62. return nil
  63. }
  64. func (m *Manager) Close() error {
  65. return nil
  66. }