chore: better close single connection in restful api

This commit is contained in:
wwqgtxx 2023-06-26 18:25:36 +08:00
parent 1cb75350e2
commit 614cc93cac
3 changed files with 21 additions and 18 deletions

View File

@ -147,7 +147,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
} }
func (pp *proxySetProvider) closeAllConnections() { 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() { for _, chain := range c.Chains() {
if chain == pp.Name() { if chain == pp.Name() {
_ = c.Close() _ = c.Close()

View File

@ -73,18 +73,14 @@ 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")
statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool { if c := statistic.DefaultManager.Get(id); c != nil {
if id == c.ID() { _ = c.Close()
_ = c.Close() }
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) {
statistic.DefaultManager.ConnectionsRange(func(c statistic.Tracker) bool { statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
_ = c.Close() _ = c.Close()
return true return true
}) })

View File

@ -46,6 +46,19 @@ func (m *Manager) Leave(c Tracker) {
m.connections.Delete(c.ID()) 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) { func (m *Manager) PushUploaded(size int64) {
m.uploadTemp.Add(size) m.uploadTemp.Add(size)
m.uploadTotal.Add(size) m.uploadTotal.Add(size)
@ -67,8 +80,8 @@ func (m *Manager) Memory() uint64 {
func (m *Manager) Snapshot() *Snapshot { func (m *Manager) Snapshot() *Snapshot {
var connections []*TrackerInfo var connections []*TrackerInfo
m.connections.Range(func(key, value any) bool { m.Range(func(c Tracker) bool {
connections = append(connections, value.(Tracker).Info()) connections = append(connections, c.Info())
return true return true
}) })
return &Snapshot{ 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() { func (m *Manager) updateMemory() {
stat, err := m.process.MemoryInfo() stat, err := m.process.MemoryInfo()
if err != nil { if err != nil {
@ -117,5 +124,5 @@ type Snapshot struct {
DownloadTotal int64 `json:"downloadTotal"` DownloadTotal int64 `json:"downloadTotal"`
UploadTotal int64 `json:"uploadTotal"` UploadTotal int64 `json:"uploadTotal"`
Connections []*TrackerInfo `json:"connections"` Connections []*TrackerInfo `json:"connections"`
Memory uint64 `json:"memory"` Memory uint64 `json:"memory"`
} }