From 5a661cde678ecd51f70abb713d6291460c0468c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 27 Nov 2024 18:05:30 +0800 Subject: [PATCH] clashapi: Remove traffic loop --- experimental/clashapi/server.go | 16 ++++--- .../clashapi/trafficontrol/manager.go | 46 +------------------ experimental/libbox/command_status.go | 15 +++++- 3 files changed, 24 insertions(+), 53 deletions(-) diff --git a/experimental/clashapi/server.go b/experimental/clashapi/server.go index 889d191e..f47e08cf 100644 --- a/experimental/clashapi/server.go +++ b/experimental/clashapi/server.go @@ -316,27 +316,31 @@ func traffic(trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, tick := time.NewTicker(time.Second) defer tick.Stop() buf := &bytes.Buffer{} - var err error + var ( + uploadTotal int64 + doanloadTotal int64 + err error + ) for range tick.C { buf.Reset() - up, down := trafficManager.Now() + uploadTotalNew, downloadTotalNew := trafficManager.Total() if err := json.NewEncoder(buf).Encode(Traffic{ - Up: up, - Down: down, + Up: uploadTotalNew - uploadTotal, + Down: downloadTotalNew - doanloadTotal, }); err != nil { break } - if conn == nil { _, err = w.Write(buf.Bytes()) w.(http.Flusher).Flush() } else { err = wsutil.WriteServerText(conn, buf.Bytes()) } - if err != nil { break } + uploadTotal = uploadTotalNew + doanloadTotal = downloadTotalNew } } } diff --git a/experimental/clashapi/trafficontrol/manager.go b/experimental/clashapi/trafficontrol/manager.go index 9b22f1e3..757ffdf9 100644 --- a/experimental/clashapi/trafficontrol/manager.go +++ b/experimental/clashapi/trafficontrol/manager.go @@ -16,30 +16,18 @@ import ( ) type Manager struct { - uploadTemp atomic.Int64 - downloadTemp atomic.Int64 - uploadBlip atomic.Int64 - downloadBlip atomic.Int64 uploadTotal atomic.Int64 downloadTotal atomic.Int64 connections compatible.Map[uuid.UUID, Tracker] closedConnectionsAccess sync.Mutex closedConnections list.List[TrackerMetadata] - ticker *time.Ticker - done chan struct{} // process *process.Process memory uint64 } func NewManager() *Manager { - manager := &Manager{ - ticker: time.NewTicker(time.Second), - done: make(chan struct{}), - // process: &process.Process{Pid: int32(os.Getpid())}, - } - go manager.handle() - return manager + return &Manager{} } func (m *Manager) Join(c Tracker) { @@ -61,19 +49,13 @@ func (m *Manager) Leave(c Tracker) { } func (m *Manager) PushUploaded(size int64) { - m.uploadTemp.Add(size) m.uploadTotal.Add(size) } func (m *Manager) PushDownloaded(size int64) { - m.downloadTemp.Add(size) m.downloadTotal.Add(size) } -func (m *Manager) Now() (up int64, down int64) { - return m.uploadBlip.Load(), m.downloadBlip.Load() -} - func (m *Manager) Total() (up int64, down int64) { return m.uploadTotal.Load(), m.downloadTotal.Load() } @@ -127,36 +109,10 @@ func (m *Manager) Snapshot() *Snapshot { } func (m *Manager) ResetStatistic() { - m.uploadTemp.Store(0) - m.uploadBlip.Store(0) m.uploadTotal.Store(0) - m.downloadTemp.Store(0) - m.downloadBlip.Store(0) m.downloadTotal.Store(0) } -func (m *Manager) handle() { - var uploadTemp int64 - var downloadTemp int64 - for { - select { - case <-m.done: - return - case <-m.ticker.C: - } - uploadTemp = m.uploadTemp.Swap(0) - downloadTemp = m.downloadTemp.Swap(0) - m.uploadBlip.Store(uploadTemp) - m.downloadBlip.Store(downloadTemp) - } -} - -func (m *Manager) Close() error { - m.ticker.Stop() - close(m.done) - return nil -} - type Snapshot struct { Download int64 Upload int64 diff --git a/experimental/libbox/command_status.go b/experimental/libbox/command_status.go index 4ab09d4b..07e66934 100644 --- a/experimental/libbox/command_status.go +++ b/experimental/libbox/command_status.go @@ -34,7 +34,6 @@ func (s *CommandServer) readStatus() StatusMessage { if clashServer := s.service.instance.Router().ClashServer(); clashServer != nil { message.TrafficAvailable = true trafficManager := clashServer.(*clashapi.Server).TrafficManager() - message.Uplink, message.Downlink = trafficManager.Now() message.UplinkTotal, message.DownlinkTotal = trafficManager.Total() message.ConnectionsIn = int32(trafficManager.ConnectionsLen()) } @@ -52,8 +51,20 @@ func (s *CommandServer) handleStatusConn(conn net.Conn) error { ticker := time.NewTicker(time.Duration(interval)) defer ticker.Stop() ctx := connKeepAlive(conn) + var ( + status StatusMessage + uploadTotal int64 + downloadTotal int64 + ) for { - err = binary.Write(conn, binary.BigEndian, s.readStatus()) + status = s.readStatus() + upload := status.UplinkTotal - uploadTotal + download := status.DownlinkTotal - downloadTotal + uploadTotal = status.UplinkTotal + downloadTotal = status.DownlinkTotal + status.Uplink = upload + status.Downlink = download + err = binary.Write(conn, binary.BigEndian, status) if err != nil { return err }