1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/features/stats/stats.go

71 lines
1.9 KiB
Go
Raw Normal View History

package stats
2018-10-22 05:26:22 -04:00
//go:generate errorgen
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
type Counter interface {
2018-10-22 02:42:10 -04:00
// Value is the current value of the counter.
Value() int64
2018-10-22 02:42:10 -04:00
// Set sets a new value to the counter, and returns the previous one.
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.
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
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.
RegisterCounter(string) (Counter, error)
2018-10-22 02:42:10 -04:00
// GetCounter returns a counter by its identifier.
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 }