stats.go 1.9 KB

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