2018-03-30 13:56:59 -04:00
|
|
|
package stats
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
|
2018-03-30 13:56:59 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/errors"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/features/stats"
|
2018-03-30 13:56:59 -04:00
|
|
|
)
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
// Manager is an implementation of stats.Manager.
|
2018-03-30 13:56:59 -04:00
|
|
|
type Manager struct {
|
|
|
|
access sync.RWMutex
|
|
|
|
counters map[string]*Counter
|
2020-09-04 03:07:42 -04:00
|
|
|
channels map[string]*Channel
|
2020-09-15 11:04:26 -04:00
|
|
|
running bool
|
2018-03-30 13:56:59 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
// NewManager creates an instance of Statistics Manager.
|
2018-03-30 13:56:59 -04:00
|
|
|
func NewManager(ctx context.Context, config *Config) (*Manager, error) {
|
2018-03-31 04:30:12 -04:00
|
|
|
m := &Manager{
|
2018-03-30 13:56:59 -04:00
|
|
|
counters: make(map[string]*Counter),
|
2020-09-04 03:07:42 -04:00
|
|
|
channels: make(map[string]*Channel),
|
2018-03-31 04:30:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
2018-03-30 13:56:59 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
// Type implements common.HasType.
|
2018-10-12 17:57:56 -04:00
|
|
|
func (*Manager) Type() interface{} {
|
|
|
|
return stats.ManagerType()
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:07:42 -04:00
|
|
|
// RegisterCounter implements stats.Manager.
|
2018-10-11 15:14:53 -04:00
|
|
|
func (m *Manager) RegisterCounter(name string) (stats.Counter, error) {
|
2018-03-30 13:56:59 -04:00
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
|
|
|
if _, found := m.counters[name]; found {
|
|
|
|
return nil, newError("Counter ", name, " already registered.")
|
|
|
|
}
|
2018-03-31 04:30:12 -04:00
|
|
|
newError("create new counter ", name).AtDebug().WriteToLog()
|
2018-03-30 13:56:59 -04:00
|
|
|
c := new(Counter)
|
|
|
|
m.counters[name] = c
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2020-09-08 23:26:23 -04:00
|
|
|
// UnregisterCounter implements stats.Manager.
|
|
|
|
func (m *Manager) UnregisterCounter(name string) error {
|
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
2020-09-08 23:33:29 -04:00
|
|
|
|
|
|
|
if _, found := m.counters[name]; found {
|
|
|
|
newError("remove counter ", name).AtDebug().WriteToLog()
|
|
|
|
delete(m.counters, name)
|
2020-09-08 23:26:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:07:42 -04:00
|
|
|
// GetCounter implements stats.Manager.
|
2018-10-11 15:14:53 -04:00
|
|
|
func (m *Manager) GetCounter(name string) stats.Counter {
|
2018-03-30 13:56:59 -04:00
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
|
|
|
if c, found := m.counters[name]; found {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:07:42 -04:00
|
|
|
// VisitCounters calls visitor function on all managed counters.
|
|
|
|
func (m *Manager) VisitCounters(visitor func(string, stats.Counter) bool) {
|
2018-07-10 17:40:58 -04:00
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
|
|
|
for name, c := range m.counters {
|
|
|
|
if !visitor(name, c) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:07:42 -04:00
|
|
|
// RegisterChannel implements stats.Manager.
|
|
|
|
func (m *Manager) RegisterChannel(name string) (stats.Channel, error) {
|
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
|
|
|
if _, found := m.channels[name]; found {
|
|
|
|
return nil, newError("Channel ", name, " already registered.")
|
|
|
|
}
|
|
|
|
newError("create new channel ", name).AtDebug().WriteToLog()
|
2020-10-02 15:06:32 -04:00
|
|
|
c := NewChannel(&ChannelConfig{BufferSize: 64, Blocking: false})
|
2020-09-04 03:07:42 -04:00
|
|
|
m.channels[name] = c
|
2020-09-15 11:04:26 -04:00
|
|
|
if m.running {
|
2020-09-23 10:35:13 -04:00
|
|
|
return c, c.Start()
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
2020-09-04 03:07:42 -04:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2020-09-08 23:33:29 -04:00
|
|
|
// UnregisterChannel implements stats.Manager.
|
|
|
|
func (m *Manager) UnregisterChannel(name string) error {
|
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if c, found := m.channels[name]; found {
|
2020-09-08 23:33:29 -04:00
|
|
|
newError("remove channel ", name).AtDebug().WriteToLog()
|
|
|
|
delete(m.channels, name)
|
2020-09-23 10:35:13 -04:00
|
|
|
return c.Close()
|
2020-09-08 23:33:29 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:07:42 -04:00
|
|
|
// GetChannel implements stats.Manager.
|
|
|
|
func (m *Manager) GetChannel(name string) stats.Channel {
|
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
|
|
|
if c, found := m.channels[name]; found {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:20:24 -04:00
|
|
|
// Start implements common.Runnable.
|
2018-03-30 13:56:59 -04:00
|
|
|
func (m *Manager) Start() error {
|
2020-09-15 11:04:26 -04:00
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
m.running = true
|
2020-09-23 10:35:13 -04:00
|
|
|
errs := []error{}
|
2020-09-15 11:04:26 -04:00
|
|
|
for _, channel := range m.channels {
|
2020-09-23 10:35:13 -04:00
|
|
|
if err := channel.Start(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errs) != 0 {
|
|
|
|
return errors.Combine(errs...)
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
2018-03-30 13:56:59 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:20:24 -04:00
|
|
|
// Close implement common.Closable.
|
2018-03-30 13:56:59 -04:00
|
|
|
func (m *Manager) Close() error {
|
2020-09-15 11:04:26 -04:00
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
m.running = false
|
2020-09-23 10:35:13 -04:00
|
|
|
errs := []error{}
|
2020-09-15 11:04:26 -04:00
|
|
|
for name, channel := range m.channels {
|
|
|
|
newError("remove channel ", name).AtDebug().WriteToLog()
|
|
|
|
delete(m.channels, name)
|
2020-09-23 10:35:13 -04:00
|
|
|
if err := channel.Close(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errs) != 0 {
|
|
|
|
return errors.Combine(errs...)
|
2020-09-15 11:04:26 -04:00
|
|
|
}
|
2018-03-30 13:56:59 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-23 10:35:13 -04:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewManager(ctx, config.(*Config))
|
|
|
|
}))
|
|
|
|
}
|