stats.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // Channel is the interface for stats channel
  16. //
  17. // v2ray:api:stable
  18. type Channel interface {
  19. // Channel returns the underlying go channel.
  20. Channel() chan interface{}
  21. // SubscriberCount returns the number of the subscribers.
  22. Subscribers() []chan interface{}
  23. // Subscribe registers for listening to channel stream and returns a new listener channel.
  24. Subscribe() chan interface{}
  25. // Unsubscribe unregisters a listener channel from current Channel object.
  26. Unsubscribe(chan interface{})
  27. }
  28. // Manager is the interface for stats manager.
  29. //
  30. // v2ray:api:stable
  31. type Manager interface {
  32. features.Feature
  33. // RegisterCounter registers a new counter to the manager. The identifier string must not be empty, and unique among other counters.
  34. RegisterCounter(string) (Counter, error)
  35. // UnregisterCounter unregisters a counter from the manager by its identifier.
  36. UnregisterCounter(string) error
  37. // GetCounter returns a counter by its identifier.
  38. GetCounter(string) Counter
  39. // RegisterChannel registers a new channel to the manager. The identifier string must not be empty, and unique among other channels.
  40. RegisterChannel(string) (Channel, error)
  41. // UnregisterCounter unregisters a channel from the manager by its identifier.
  42. UnregisterChannel(string) error
  43. // GetChannel returns a channel by its identifier.
  44. GetChannel(string) Channel
  45. }
  46. // GetOrRegisterCounter tries to get the StatCounter first. If not exist, it then tries to create a new counter.
  47. func GetOrRegisterCounter(m Manager, name string) (Counter, error) {
  48. counter := m.GetCounter(name)
  49. if counter != nil {
  50. return counter, nil
  51. }
  52. return m.RegisterCounter(name)
  53. }
  54. // GetOrRegisterChannel tries to get the StatChannel first. If not exist, it then tries to create a new channel.
  55. func GetOrRegisterChannel(m Manager, name string) (Channel, error) {
  56. channel := m.GetChannel(name)
  57. if channel != nil {
  58. return channel, nil
  59. }
  60. return m.RegisterChannel(name)
  61. }
  62. // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
  63. //
  64. // v2ray:api:stable
  65. func ManagerType() interface{} {
  66. return (*Manager)(nil)
  67. }
  68. // NoopManager is an implementation of Manager, which doesn't has actual functionalities.
  69. type NoopManager struct{}
  70. // Type implements common.HasType.
  71. func (NoopManager) Type() interface{} {
  72. return ManagerType()
  73. }
  74. // RegisterCounter implements Manager.
  75. func (NoopManager) RegisterCounter(string) (Counter, error) {
  76. return nil, newError("not implemented")
  77. }
  78. // UnregisterCounter implements Manager.
  79. func (NoopManager) UnregisterCounter(string) error {
  80. return nil
  81. }
  82. // GetCounter implements Manager.
  83. func (NoopManager) GetCounter(string) Counter {
  84. return nil
  85. }
  86. // RegisterChannel implements Manager.
  87. func (NoopManager) RegisterChannel(string) (Channel, error) {
  88. return nil, newError("not implemented")
  89. }
  90. // UnregisterChannel implements Manager.
  91. func (NoopManager) UnregisterChannel(string) error {
  92. return nil
  93. }
  94. // GetChannel implements Manager.
  95. func (NoopManager) GetChannel(string) Channel {
  96. return nil
  97. }
  98. // Start implements common.Runnable.
  99. func (NoopManager) Start() error { return nil }
  100. // Close implements common.Closable.
  101. func (NoopManager) Close() error { return nil }