stats.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package stats
  2. import "v2ray.com/core/features"
  3. // Counter is the interface for stats counters.
  4. type Counter interface {
  5. // Value is the current value of the counter.
  6. Value() int64
  7. // Set sets a new value to the counter, and returns the previous one.
  8. Set(int64) int64
  9. // Add adds a value to the current counter value, and returns the previous value.
  10. Add(int64) int64
  11. }
  12. // Manager is the interface for stats manager.
  13. type Manager interface {
  14. features.Feature
  15. // RegisterCounter registers a new counter to the manager. The identifier string must not be emtpy, and unique among other counters.
  16. RegisterCounter(string) (Counter, error)
  17. // GetCounter returns a counter by its identifier.
  18. GetCounter(string) Counter
  19. }
  20. // GetOrRegisterCounter tries to get the StatCounter first. If not exist, it then tries to create a new counter.
  21. func GetOrRegisterCounter(m Manager, name string) (Counter, error) {
  22. counter := m.GetCounter(name)
  23. if counter != nil {
  24. return counter, nil
  25. }
  26. return m.RegisterCounter(name)
  27. }
  28. // ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
  29. func ManagerType() interface{} {
  30. return (*Manager)(nil)
  31. }