1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 01:40:44 +00:00
v2fly/app/stats/stats.go

171 lines
3.8 KiB
Go
Raw Normal View History

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