stats.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // GetCounter returns a counter by its identifier.
  36. GetCounter(string) Counter
  37. // RegisterChannel registers a new channel to the manager. The identifier string must not be empty, and unique among other channels.
  38. RegisterChannel(string) (Channel, error)
  39. // GetChannel returns a channel by its identifier.
  40. GetChannel(string) Channel
  41. }
  42. // GetOrRegisterCounter tries to get the StatCounter first. If not exist, it then tries to create a new counter.
  43. func GetOrRegisterCounter(m Manager, name string) (Counter, error) {
  44. counter := m.GetCounter(name)
  45. if counter != nil {
  46. return counter, nil
  47. }
  48. return m.RegisterCounter(name)
  49. }
  50. // GetOrRegisterChannel tries to get the StatChannel first. If not exist, it then tries to create a new channel.
  51. func GetOrRegisterChannel(m Manager, name string) (Channel, error) {
  52. channel := m.GetChannel(name)
  53. if channel != nil {
  54. return channel, nil
  55. }
  56. return m.RegisterChannel(name)
  57. }
  58. // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
  59. //
  60. // v2ray:api:stable
  61. func ManagerType() interface{} {
  62. return (*Manager)(nil)
  63. }
  64. // NoopManager is an implementation of Manager, which doesn't has actual functionalities.
  65. type NoopManager struct{}
  66. // Type implements common.HasType.
  67. func (NoopManager) Type() interface{} {
  68. return ManagerType()
  69. }
  70. // RegisterCounter implements Manager.
  71. func (NoopManager) RegisterCounter(string) (Counter, error) {
  72. return nil, newError("not implemented")
  73. }
  74. // GetCounter implements Manager.
  75. func (NoopManager) GetCounter(string) Counter {
  76. return nil
  77. }
  78. // RegisterChannel implements Manager.
  79. func (NoopManager) RegisterChannel(string) (Channel, error) {
  80. return nil, newError("not implemented")
  81. }
  82. // GetChannel implements Manager.
  83. func (NoopManager) GetChannel(string) Channel {
  84. return nil
  85. }
  86. // Start implements common.Runnable.
  87. func (NoopManager) Start() error { return nil }
  88. // Close implements common.Closable.
  89. func (NoopManager) Close() error { return nil }