chore: avoid unneeded map copy when close connection in restful api
This commit is contained in:
parent
26f97b45d6
commit
abf80601e1
4 changed files with 24 additions and 18 deletions
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import (
|
|||
"github.com/gofrs/uuid/v5"
|
||||
)
|
||||
|
||||
type tracker interface {
|
||||
type Tracker interface {
|
||||
ID() string
|
||||
Close() error
|
||||
C.Connection
|
||||
|
|
Loading…
Reference in a new issue