stats.go 3.6 KB

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