From 42ef4fedfa583bbb0ef7a0d2fe9200930942dc31 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Mon, 26 Jun 2023 17:46:14 +0800 Subject: [PATCH] chore: avoid unneeded map copy when close connection in restful api --- adapter/provider/provider.go | 6 +++--- hub/route/connections.go | 18 +++++++++--------- tunnel/statistic/manager.go | 16 +++++++++++----- tunnel/statistic/tracker.go | 2 +- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 60fbb5f0..7a5f5853 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 bfe3b42d..927fdefd 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 ba2e1298..8218472c 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 a2a921ac..332be13d 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