mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-05-01 01:44:15 +00:00
API: Add user online stats (#3637)
* add statsUserOnline bool to policy * add OnlineMap struct to stats * apply UserOnline functionality to dispatcher * add statsonline api command * fix comments * Update app/stats/online_map.go Co-authored-by: mmmray <142015632+mmmray@users.noreply.github.com> * improve AddIP * regenerate pb --------- Co-authored-by: mmmray <142015632+mmmray@users.noreply.github.com>
This commit is contained in:
parent
e3276df725
commit
2c72864935
15 changed files with 575 additions and 177 deletions
|
@ -11,17 +11,19 @@ import (
|
|||
|
||||
// Manager is an implementation of stats.Manager.
|
||||
type Manager struct {
|
||||
access sync.RWMutex
|
||||
counters map[string]*Counter
|
||||
channels map[string]*Channel
|
||||
running bool
|
||||
access sync.RWMutex
|
||||
counters map[string]*Counter
|
||||
onlineMap map[string]*OnlineMap
|
||||
channels map[string]*Channel
|
||||
running bool
|
||||
}
|
||||
|
||||
// NewManager creates an instance of Statistics Manager.
|
||||
func NewManager(ctx context.Context, config *Config) (*Manager, error) {
|
||||
m := &Manager{
|
||||
counters: make(map[string]*Counter),
|
||||
channels: make(map[string]*Channel),
|
||||
counters: make(map[string]*Counter),
|
||||
onlineMap: make(map[string]*OnlineMap),
|
||||
channels: make(map[string]*Channel),
|
||||
}
|
||||
|
||||
return m, nil
|
||||
|
@ -81,6 +83,43 @@ func (m *Manager) VisitCounters(visitor func(string, stats.Counter) bool) {
|
|||
}
|
||||
}
|
||||
|
||||
// RegisterOnlineMap implements stats.Manager.
|
||||
func (m *Manager) RegisterOnlineMap(name string) (stats.OnlineMap, error) {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
if _, found := m.onlineMap[name]; found {
|
||||
return nil, errors.New("onlineMap ", name, " already registered.")
|
||||
}
|
||||
errors.LogDebug(context.Background(), "create new onlineMap ", name)
|
||||
om := NewOnlineMap()
|
||||
m.onlineMap[name] = om
|
||||
return om, nil
|
||||
}
|
||||
|
||||
// UnregisterOnlineMap implements stats.Manager.
|
||||
func (m *Manager) UnregisterOnlineMap(name string) error {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
|
||||
if _, found := m.onlineMap[name]; found {
|
||||
errors.LogDebug(context.Background(), "remove onlineMap ", name)
|
||||
delete(m.onlineMap, name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetOnlineMap implements stats.Manager.
|
||||
func (m *Manager) GetOnlineMap(name string) stats.OnlineMap {
|
||||
m.access.RLock()
|
||||
defer m.access.RUnlock()
|
||||
|
||||
if om, found := m.onlineMap[name]; found {
|
||||
return om
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterChannel implements stats.Manager.
|
||||
func (m *Manager) RegisterChannel(name string) (stats.Channel, error) {
|
||||
m.access.Lock()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue