stats.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package stats
  2. //go:generate errorgen
  3. import "v2ray.com/core/features"
  4. // Counter is the interface for stats counters.
  5. type Counter interface {
  6. // Value is the current value of the counter.
  7. Value() int64
  8. // Set sets a new value to the counter, and returns the previous one.
  9. Set(int64) int64
  10. // Add adds a value to the current counter value, and returns the previous value.
  11. Add(int64) int64
  12. }
  13. // Manager is the interface for stats manager.
  14. type Manager interface {
  15. features.Feature
  16. // RegisterCounter registers a new counter to the manager. The identifier string must not be emtpy, and unique among other counters.
  17. RegisterCounter(string) (Counter, error)
  18. // GetCounter returns a counter by its identifier.
  19. GetCounter(string) Counter
  20. }
  21. // GetOrRegisterCounter tries to get the StatCounter first. If not exist, it then tries to create a new counter.
  22. func GetOrRegisterCounter(m Manager, name string) (Counter, error) {
  23. counter := m.GetCounter(name)
  24. if counter != nil {
  25. return counter, nil
  26. }
  27. return m.RegisterCounter(name)
  28. }
  29. // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
  30. func ManagerType() interface{} {
  31. return (*Manager)(nil)
  32. }
  33. // NoopManager is an implementation of Manager, which doesn't has actual functionalities.
  34. type NoopManager struct{}
  35. // Type implements common.HasType.
  36. func (NoopManager) Type() interface{} {
  37. return ManagerType()
  38. }
  39. // RegisterCounter implements Manager.
  40. func (NoopManager) RegisterCounter(string) (Counter, error) {
  41. return nil, newError("not implemented")
  42. }
  43. // GetCounter implements Manager.
  44. func (NoopManager) GetCounter(string) Counter {
  45. return nil
  46. }
  47. // Start implements common.Runnable.
  48. func (NoopManager) Start() error { return nil }
  49. // Close implements common.Closable.
  50. func (NoopManager) Close() error { return nil }