stats.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package stats
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common"
  5. "github.com/xtls/xray-core/common/errors"
  6. "github.com/xtls/xray-core/features"
  7. )
  8. // Counter is the interface for stats counters.
  9. //
  10. // xray:api:stable
  11. type Counter interface {
  12. // Value is the current value of the counter.
  13. Value() int64
  14. // Set sets a new value to the counter, and returns the previous one.
  15. Set(int64) int64
  16. // Add adds a value to the current counter value, and returns the previous value.
  17. Add(int64) int64
  18. }
  19. // OnlineMap is the interface for stats.
  20. //
  21. // xray:api:stable
  22. type OnlineMap interface {
  23. // Count is the current value of the OnlineMap.
  24. Count() int
  25. // AddIP adds a ip to the current OnlineMap.
  26. AddIP(string)
  27. // List is the current OnlineMap ip list.
  28. List() []string
  29. }
  30. // Channel is the interface for stats channel.
  31. //
  32. // xray:api:stable
  33. type Channel interface {
  34. // Runnable implies that Channel is a runnable unit.
  35. common.Runnable
  36. // Publish broadcasts a message through the channel with a controlling context.
  37. Publish(context.Context, interface{})
  38. // Subscribers returns all subscribers.
  39. Subscribers() []chan interface{}
  40. // Subscribe registers for listening to channel stream and returns a new listener channel.
  41. Subscribe() (chan interface{}, error)
  42. // Unsubscribe unregisters a listener channel from current Channel object.
  43. Unsubscribe(chan interface{}) error
  44. }
  45. // SubscribeRunnableChannel subscribes the channel and starts it if there is first subscriber coming.
  46. func SubscribeRunnableChannel(c Channel) (chan interface{}, error) {
  47. if len(c.Subscribers()) == 0 {
  48. if err := c.Start(); err != nil {
  49. return nil, err
  50. }
  51. }
  52. return c.Subscribe()
  53. }
  54. // UnsubscribeClosableChannel unsubscribes the channel and close it if there is no more subscriber.
  55. func UnsubscribeClosableChannel(c Channel, sub chan interface{}) error {
  56. if err := c.Unsubscribe(sub); err != nil {
  57. return err
  58. }
  59. if len(c.Subscribers()) == 0 {
  60. return c.Close()
  61. }
  62. return nil
  63. }
  64. // Manager is the interface for stats manager.
  65. //
  66. // xray:api:stable
  67. type Manager interface {
  68. features.Feature
  69. // RegisterCounter registers a new counter to the manager. The identifier string must not be empty, and unique among other counters.
  70. RegisterCounter(string) (Counter, error)
  71. // UnregisterCounter unregisters a counter from the manager by its identifier.
  72. UnregisterCounter(string) error
  73. // GetCounter returns a counter by its identifier.
  74. GetCounter(string) Counter
  75. // RegisterOnlineMap registers a new onlinemap to the manager. The identifier string must not be empty, and unique among other onlinemaps.
  76. RegisterOnlineMap(string) (OnlineMap, error)
  77. // UnregisterOnlineMap unregisters a onlinemap from the manager by its identifier.
  78. UnregisterOnlineMap(string) error
  79. // GetOnlineMap returns a onlinemap by its identifier.
  80. GetOnlineMap(string) OnlineMap
  81. // RegisterChannel registers a new channel to the manager. The identifier string must not be empty, and unique among other channels.
  82. RegisterChannel(string) (Channel, error)
  83. // UnregisterChannel unregisters a channel from the manager by its identifier.
  84. UnregisterChannel(string) error
  85. // GetChannel returns a channel by its identifier.
  86. GetChannel(string) Channel
  87. }
  88. // GetOrRegisterCounter tries to get the StatCounter first. If not exist, it then tries to create a new counter.
  89. func GetOrRegisterCounter(m Manager, name string) (Counter, error) {
  90. counter := m.GetCounter(name)
  91. if counter != nil {
  92. return counter, nil
  93. }
  94. return m.RegisterCounter(name)
  95. }
  96. // GetOrRegisterOnlineMap tries to get the OnlineMap first. If not exist, it then tries to create a new onlinemap.
  97. func GetOrRegisterOnlineMap(m Manager, name string) (OnlineMap, error) {
  98. onlineMap := m.GetOnlineMap(name)
  99. if onlineMap != nil {
  100. return onlineMap, nil
  101. }
  102. return m.RegisterOnlineMap(name)
  103. }
  104. // GetOrRegisterChannel tries to get the StatChannel first. If not exist, it then tries to create a new channel.
  105. func GetOrRegisterChannel(m Manager, name string) (Channel, error) {
  106. channel := m.GetChannel(name)
  107. if channel != nil {
  108. return channel, nil
  109. }
  110. return m.RegisterChannel(name)
  111. }
  112. // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
  113. //
  114. // xray:api:stable
  115. func ManagerType() interface{} {
  116. return (*Manager)(nil)
  117. }
  118. // NoopManager is an implementation of Manager, which doesn't has actual functionalities.
  119. type NoopManager struct{}
  120. // Type implements common.HasType.
  121. func (NoopManager) Type() interface{} {
  122. return ManagerType()
  123. }
  124. // RegisterCounter implements Manager.
  125. func (NoopManager) RegisterCounter(string) (Counter, error) {
  126. return nil, errors.New("not implemented")
  127. }
  128. // UnregisterCounter implements Manager.
  129. func (NoopManager) UnregisterCounter(string) error {
  130. return nil
  131. }
  132. // GetCounter implements Manager.
  133. func (NoopManager) GetCounter(string) Counter {
  134. return nil
  135. }
  136. // RegisterOnlineMap implements Manager.
  137. func (NoopManager) RegisterOnlineMap(string) (OnlineMap, error) {
  138. return nil, errors.New("not implemented")
  139. }
  140. // UnregisterOnlineMap implements Manager.
  141. func (NoopManager) UnregisterOnlineMap(string) error {
  142. return nil
  143. }
  144. // GetOnlineMap implements Manager.
  145. func (NoopManager) GetOnlineMap(string) OnlineMap {
  146. return nil
  147. }
  148. // RegisterChannel implements Manager.
  149. func (NoopManager) RegisterChannel(string) (Channel, error) {
  150. return nil, errors.New("not implemented")
  151. }
  152. // UnregisterChannel implements Manager.
  153. func (NoopManager) UnregisterChannel(string) error {
  154. return nil
  155. }
  156. // GetChannel implements Manager.
  157. func (NoopManager) GetChannel(string) Channel {
  158. return nil
  159. }
  160. // Start implements common.Runnable.
  161. func (NoopManager) Start() error { return nil }
  162. // Close implements common.Closable.
  163. func (NoopManager) Close() error { return nil }