Skip to content

Commit 3c107ff

Browse files
authored
net/connstats: fix ticker in NewStatistics (tailscale#7225)
`:=` was accidentally used, so `maxPeriod` never worked. Signed-off-by: Colin Adler <colin1adler@gmail.com>
1 parent 6ef834a commit 3c107ff

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

net/connstats/stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewStatistics(maxPeriod time.Duration, maxConns int, dump func(start, end t
5454
// a time.Timer that is triggered upon network activity.
5555
ticker := new(time.Ticker)
5656
if maxPeriod > 0 {
57-
ticker := time.NewTicker(maxPeriod)
57+
ticker = time.NewTicker(maxPeriod)
5858
defer ticker.Stop()
5959
}
6060

net/connstats/stats_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ func testPacketV4(proto ipproto.Proto, srcAddr, dstAddr [4]byte, srcPort, dstPor
4545
return append(out, make([]byte, int(size)-len(out))...)
4646
}
4747

48+
// TestInterval ensures that we receive at least one call to `dump` using only
49+
// maxPeriod.
50+
func TestInterval(t *testing.T) {
51+
c := qt.New(t)
52+
53+
const maxPeriod = 10 * time.Millisecond
54+
const maxConns = 2048
55+
56+
gotDump := make(chan struct{}, 1)
57+
stats := NewStatistics(maxPeriod, maxConns, func(_, _ time.Time, _, _ map[netlogtype.Connection]netlogtype.Counts) {
58+
select {
59+
case gotDump <- struct{}{}:
60+
default:
61+
}
62+
})
63+
defer stats.Shutdown(context.Background())
64+
65+
srcAddr := netip.AddrFrom4([4]byte{192, 168, 0, byte(rand.Intn(16))})
66+
dstAddr := netip.AddrFrom4([4]byte{192, 168, 0, byte(rand.Intn(16))})
67+
srcPort := uint16(rand.Intn(16))
68+
dstPort := uint16(rand.Intn(16))
69+
size := uint16(64 + rand.Intn(1024))
70+
p := testPacketV4(ipproto.TCP, srcAddr.As4(), dstAddr.As4(), srcPort, dstPort, size)
71+
stats.UpdateRxVirtual(p)
72+
73+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
74+
defer cancel()
75+
select {
76+
case <-ctx.Done():
77+
c.Fatal("didn't receive dump within context deadline")
78+
case <-gotDump:
79+
}
80+
}
81+
4882
func TestConcurrent(t *testing.T) {
4983
flakytest.Mark(t, "https://github.com/tailscale/tailscale/issues/7030")
5084
c := qt.New(t)

0 commit comments

Comments
 (0)