2018-10-11 15:14:53 -04:00
|
|
|
package stats
|
|
|
|
|
2018-10-22 05:26:22 -04:00
|
|
|
//go:generate errorgen
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
import "v2ray.com/core/features"
|
|
|
|
|
2018-10-22 02:42:10 -04:00
|
|
|
// Counter is the interface for stats counters.
|
2018-12-03 16:44:42 -05:00
|
|
|
//
|
|
|
|
// v2ray:api:stable
|
2018-10-11 15:14:53 -04:00
|
|
|
type Counter interface {
|
2018-10-22 02:42:10 -04:00
|
|
|
// Value is the current value of the counter.
|
2018-10-11 15:14:53 -04:00
|
|
|
Value() int64
|
2018-10-22 02:42:10 -04:00
|
|
|
// Set sets a new value to the counter, and returns the previous one.
|
2018-10-11 15:14:53 -04:00
|
|
|
Set(int64) int64
|
2018-10-22 02:42:10 -04:00
|
|
|
// Add adds a value to the current counter value, and returns the previous value.
|
2018-10-11 15:14:53 -04:00
|
|
|
Add(int64) int64
|
|
|
|
}
|
|
|
|
|
2018-10-22 02:42:10 -04:00
|
|
|
// Manager is the interface for stats manager.
|
2018-12-03 16:44:42 -05:00
|
|
|
//
|
|
|
|
// v2ray:api:stable
|
2018-10-11 15:14:53 -04:00
|
|
|
type Manager interface {
|
|
|
|
features.Feature
|
|
|
|
|
2018-10-22 02:42:10 -04:00
|
|
|
// RegisterCounter registers a new counter to the manager. The identifier string must not be emtpy, and unique among other counters.
|
2018-10-11 15:14:53 -04:00
|
|
|
RegisterCounter(string) (Counter, error)
|
2018-10-22 02:42:10 -04:00
|
|
|
// GetCounter returns a counter by its identifier.
|
2018-10-11 15:14:53 -04:00
|
|
|
GetCounter(string) Counter
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrRegisterCounter tries to get the StatCounter first. If not exist, it then tries to create a new counter.
|
|
|
|
func GetOrRegisterCounter(m Manager, name string) (Counter, error) {
|
|
|
|
counter := m.GetCounter(name)
|
|
|
|
if counter != nil {
|
|
|
|
return counter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.RegisterCounter(name)
|
|
|
|
}
|
2018-10-12 17:57:56 -04:00
|
|
|
|
2018-10-13 09:15:49 -04:00
|
|
|
// ManagerType returns the type of Manager interface. Can be used to implement common.HasType.
|
2018-12-03 16:44:42 -05:00
|
|
|
//
|
|
|
|
// v2ray:api:stable
|
2018-10-12 17:57:56 -04:00
|
|
|
func ManagerType() interface{} {
|
|
|
|
return (*Manager)(nil)
|
|
|
|
}
|
2018-10-22 05:26:22 -04:00
|
|
|
|
|
|
|
// NoopManager is an implementation of Manager, which doesn't has actual functionalities.
|
|
|
|
type NoopManager struct{}
|
|
|
|
|
|
|
|
// Type implements common.HasType.
|
|
|
|
func (NoopManager) Type() interface{} {
|
|
|
|
return ManagerType()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterCounter implements Manager.
|
|
|
|
func (NoopManager) RegisterCounter(string) (Counter, error) {
|
|
|
|
return nil, newError("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCounter implements Manager.
|
|
|
|
func (NoopManager) GetCounter(string) Counter {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements common.Runnable.
|
|
|
|
func (NoopManager) Start() error { return nil }
|
|
|
|
|
|
|
|
// Close implements common.Closable.
|
|
|
|
func (NoopManager) Close() error { return nil }
|