fix(tunnel): race
This commit is contained in:
1 parent
daf8cece80
commit
02c696fa28
2 files changed
+10
-5
No files matched your search
+7
-3
@@ -3,6 +3,7 @@ package mobile
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strings"
|
||||
@@ -256,13 +257,16 @@ func directSnapshot(l *live) *Snapshot {
|
||||
}
|
||||
|
||||
func (l *live) fillRates(s *Snapshot) {
|
||||
l.fillRatesAt(s, time.Now())
|
||||
}
|
||||
|
||||
func (l *live) fillRatesAt(s *Snapshot, now time.Time) {
|
||||
l.rateMu.Lock()
|
||||
defer l.rateMu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
if dt := now.Sub(l.prevAt).Seconds(); !l.prevAt.IsZero() && dt > 0 {
|
||||
s.TxRate = int64(float64(s.TxTotal-l.prevTx) / dt)
|
||||
s.RxRate = int64(float64(s.RxTotal-l.prevRx) / dt)
|
||||
s.TxRate = int64(math.Round(float64(s.TxTotal-l.prevTx) / dt))
|
||||
s.RxRate = int64(math.Round(float64(s.RxTotal-l.prevRx) / dt))
|
||||
}
|
||||
l.prevTx, l.prevRx, l.prevAt = s.TxTotal, s.RxTotal, now
|
||||
}
|
||||
@@ -310,10 +310,11 @@ func TestDirectSnapshotErrorKeepsStreams(t *testing.T) {
|
||||
|
||||
// Скорость берётся из дельты между опросами хоста.
|
||||
func TestFillRatesUsesDelta(t *testing.T) {
|
||||
l := &live{prevTx: 1000, prevRx: 500, prevAt: time.Now().Add(-2 * time.Second)}
|
||||
now := time.Now()
|
||||
l := &live{prevTx: 1000, prevRx: 500, prevAt: now.Add(-2 * time.Second)}
|
||||
s := &Snapshot{TxTotal: 3000, RxTotal: 1500}
|
||||
|
||||
l.fillRates(s)
|
||||
l.fillRatesAt(s, now)
|
||||
|
||||
if s.TxRate != 1000 || s.RxRate != 500 {
|
||||
t.Errorf("rates = %d/%d, want 1000/500", s.TxRate, s.RxRate)
|
||||
|
||||
Reference in new issue
Block a user