diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 7a5f5853..10861217 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -147,7 +147,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() { } func (pp *proxySetProvider) closeAllConnections() { - statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool { + statistic.DefaultManager.Range(func(c statistic.Tracker) bool { for _, chain := range c.Chains() { if chain == pp.Name() { _ = c.Close() diff --git a/hub/route/connections.go b/hub/route/connections.go index 927fdefd..b123ecae 100644 --- a/hub/route/connections.go +++ b/hub/route/connections.go @@ -73,18 +73,14 @@ func getConnections(w http.ResponseWriter, r *http.Request) { func closeConnection(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") - statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool { - if id == c.ID() { - _ = c.Close() - return false - } - return true - }) + if c := statistic.DefaultManager.Get(id); c != nil { + _ = c.Close() + } render.NoContent(w, r) } func closeAllConnections(w http.ResponseWriter, r *http.Request) { - statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool { + statistic.DefaultManager.Range(func(c statistic.Tracker) bool { _ = c.Close() return true }) diff --git a/tunnel/statistic/manager.go b/tunnel/statistic/manager.go index 575649f5..2358d0bd 100644 --- a/tunnel/statistic/manager.go +++ b/tunnel/statistic/manager.go @@ -46,6 +46,19 @@ func (m *Manager) Leave(c Tracker) { m.connections.Delete(c.ID()) } +func (m *Manager) Get(id string) (c Tracker) { + if value, ok := m.connections.Load(id); ok { + c = value.(Tracker) + } + return +} + +func (m *Manager) Range(f func(c Tracker) bool) { + m.connections.Range(func(key, value any) bool { + return f(value.(Tracker)) + }) +} + func (m *Manager) PushUploaded(size int64) { m.uploadTemp.Add(size) m.uploadTotal.Add(size) @@ -67,8 +80,8 @@ func (m *Manager) Memory() uint64 { func (m *Manager) Snapshot() *Snapshot { var connections []*TrackerInfo - m.connections.Range(func(key, value any) bool { - connections = append(connections, value.(Tracker).Info()) + m.Range(func(c Tracker) bool { + connections = append(connections, c.Info()) return true }) return &Snapshot{ @@ -79,12 +92,6 @@ func (m *Manager) Snapshot() *Snapshot { } } -func (m *Manager) ConnectionsRange(f func(c Tracker) bool) { - m.connections.Range(func(key, value any) bool { - return f(value.(Tracker)) - }) -} - func (m *Manager) updateMemory() { stat, err := m.process.MemoryInfo() if err != nil { @@ -117,5 +124,5 @@ type Snapshot struct { DownloadTotal int64 `json:"downloadTotal"` UploadTotal int64 `json:"uploadTotal"` Connections []*TrackerInfo `json:"connections"` - Memory uint64 `json:"memory"` + Memory uint64 `json:"memory"` }