diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 60fbb5f04..7a5f5853d 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -147,15 +147,15 @@ func (pp *proxySetProvider) getSubscriptionInfo() { } func (pp *proxySetProvider) closeAllConnections() { - snapshot := statistic.DefaultManager.Snapshot() - for _, c := range snapshot.Connections { + statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool { for _, chain := range c.Chains() { if chain == pp.Name() { _ = c.Close() break } } - } + return true + }) } func stopProxyProvider(pd *ProxySetProvider) { diff --git a/hub/route/connections.go b/hub/route/connections.go index bfe3b42dc..927fdefd0 100644 --- a/hub/route/connections.go +++ b/hub/route/connections.go @@ -73,20 +73,20 @@ func getConnections(w http.ResponseWriter, r *http.Request) { func closeConnection(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") - snapshot := statistic.DefaultManager.Snapshot() - for _, c := range snapshot.Connections { + statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool { if id == c.ID() { - c.Close() - break + _ = c.Close() + return false } - } + return true + }) render.NoContent(w, r) } func closeAllConnections(w http.ResponseWriter, r *http.Request) { - snapshot := statistic.DefaultManager.Snapshot() - for _, c := range snapshot.Connections { - c.Close() - } + statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool { + _ = c.Close() + return true + }) render.NoContent(w, r) } diff --git a/tunnel/statistic/manager.go b/tunnel/statistic/manager.go index ba2e1298c..8218472cf 100644 --- a/tunnel/statistic/manager.go +++ b/tunnel/statistic/manager.go @@ -38,11 +38,11 @@ type Manager struct { memory uint64 } -func (m *Manager) Join(c tracker) { +func (m *Manager) Join(c Tracker) { m.connections.Store(c.ID(), c) } -func (m *Manager) Leave(c tracker) { +func (m *Manager) Leave(c Tracker) { m.connections.Delete(c.ID()) } @@ -66,9 +66,9 @@ func (m *Manager) Memory() uint64 { } func (m *Manager) Snapshot() *Snapshot { - connections := []tracker{} + connections := []Tracker{} m.connections.Range(func(key, value any) bool { - connections = append(connections, value.(tracker)) + connections = append(connections, value.(Tracker)) return true }) return &Snapshot{ @@ -79,6 +79,12 @@ 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 { @@ -110,6 +116,6 @@ func (m *Manager) handle() { type Snapshot struct { DownloadTotal int64 `json:"downloadTotal"` UploadTotal int64 `json:"uploadTotal"` - Connections []tracker `json:"connections"` + Connections []Tracker `json:"connections"` Memory uint64 `json:"memory"` } diff --git a/tunnel/statistic/tracker.go b/tunnel/statistic/tracker.go index a2a921ac9..332be13d4 100644 --- a/tunnel/statistic/tracker.go +++ b/tunnel/statistic/tracker.go @@ -15,7 +15,7 @@ import ( "github.com/gofrs/uuid/v5" ) -type tracker interface { +type Tracker interface { ID() string Close() error C.Connection