chore: avoid unneeded map copy when close connection in restful api

This commit is contained in:
wwqgtxx 2023-06-26 17:46:14 +08:00
parent 2284acce94
commit 42ef4fedfa
4 changed files with 24 additions and 18 deletions

View file

@ -147,15 +147,15 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
} }
func (pp *proxySetProvider) closeAllConnections() { func (pp *proxySetProvider) closeAllConnections() {
snapshot := statistic.DefaultManager.Snapshot() statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool {
for _, c := range snapshot.Connections {
for _, chain := range c.Chains() { for _, chain := range c.Chains() {
if chain == pp.Name() { if chain == pp.Name() {
_ = c.Close() _ = c.Close()
break break
} }
} }
} return true
})
} }
func stopProxyProvider(pd *ProxySetProvider) { func stopProxyProvider(pd *ProxySetProvider) {

View file

@ -73,20 +73,20 @@ func getConnections(w http.ResponseWriter, r *http.Request) {
func closeConnection(w http.ResponseWriter, r *http.Request) { func closeConnection(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id") id := chi.URLParam(r, "id")
snapshot := statistic.DefaultManager.Snapshot() statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool {
for _, c := range snapshot.Connections {
if id == c.ID() { if id == c.ID() {
c.Close() _ = c.Close()
break return false
}
} }
return true
})
render.NoContent(w, r) render.NoContent(w, r)
} }
func closeAllConnections(w http.ResponseWriter, r *http.Request) { func closeAllConnections(w http.ResponseWriter, r *http.Request) {
snapshot := statistic.DefaultManager.Snapshot() statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool {
for _, c := range snapshot.Connections { _ = c.Close()
c.Close() return true
} })
render.NoContent(w, r) render.NoContent(w, r)
} }

View file

@ -38,11 +38,11 @@ type Manager struct {
memory uint64 memory uint64
} }
func (m *Manager) Join(c tracker) { func (m *Manager) Join(c Tracker) {
m.connections.Store(c.ID(), c) m.connections.Store(c.ID(), c)
} }
func (m *Manager) Leave(c tracker) { func (m *Manager) Leave(c Tracker) {
m.connections.Delete(c.ID()) m.connections.Delete(c.ID())
} }
@ -66,9 +66,9 @@ func (m *Manager) Memory() uint64 {
} }
func (m *Manager) Snapshot() *Snapshot { func (m *Manager) Snapshot() *Snapshot {
connections := []tracker{} connections := []Tracker{}
m.connections.Range(func(key, value any) bool { m.connections.Range(func(key, value any) bool {
connections = append(connections, value.(tracker)) connections = append(connections, value.(Tracker))
return true return true
}) })
return &Snapshot{ 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() { func (m *Manager) updateMemory() {
stat, err := m.process.MemoryInfo() stat, err := m.process.MemoryInfo()
if err != nil { if err != nil {
@ -110,6 +116,6 @@ func (m *Manager) handle() {
type Snapshot struct { type Snapshot struct {
DownloadTotal int64 `json:"downloadTotal"` DownloadTotal int64 `json:"downloadTotal"`
UploadTotal int64 `json:"uploadTotal"` UploadTotal int64 `json:"uploadTotal"`
Connections []tracker `json:"connections"` Connections []Tracker `json:"connections"`
Memory uint64 `json:"memory"` Memory uint64 `json:"memory"`
} }

View file

@ -15,7 +15,7 @@ import (
"github.com/gofrs/uuid/v5" "github.com/gofrs/uuid/v5"
) )
type tracker interface { type Tracker interface {
ID() string ID() string
Close() error Close() error
C.Connection C.Connection