Skip to content

Commit

Permalink
Chore: use only one goroutine to handle statistic (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr328 authored Sep 3, 2020
1 parent 02d9169 commit 13275b1
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions tunnel/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var DefaultManager *Manager

func init() {
DefaultManager = &Manager{}
DefaultManager.handle()

go DefaultManager.handle()
}

type Manager struct {
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 13275b1

Please sign in to comment.