From d8c0fcb9b388c722c737b1131dea1250805bfd93 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 3 Sep 2020 10:30:18 +0800 Subject: [PATCH] Chore: use only one goroutine to handle statistic (#940) --- tunnel/manager.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/tunnel/manager.go b/tunnel/manager.go index aef8b617..1493bc00 100644 --- a/tunnel/manager.go +++ b/tunnel/manager.go @@ -10,7 +10,8 @@ var DefaultManager *Manager func init() { DefaultManager = &Manager{} - DefaultManager.handle() + + go DefaultManager.handle() } type Manager struct { @@ -69,18 +70,13 @@ func (m *Manager) ResetStatistic() { } func (m *Manager) handle() { - go m.handleCh(&m.uploadTemp, &m.uploadBlip) - go m.handleCh(&m.downloadTemp, &m.downloadBlip) -} - -func (m *Manager) handleCh(temp *int64, blip *int64) { ticker := time.NewTicker(time.Second) - for { - <-ticker.C - - atomic.StoreInt64(blip, atomic.LoadInt64(temp)) - atomic.StoreInt64(temp, 0) + for range ticker.C { + atomic.StoreInt64(&m.uploadBlip, atomic.LoadInt64(&m.uploadTemp)) + atomic.StoreInt64(&m.uploadTemp, 0) + atomic.StoreInt64(&m.downloadBlip, atomic.LoadInt64(&m.downloadTemp)) + atomic.StoreInt64(&m.downloadTemp, 0) } }