Skip to content

Commit e85b99a

Browse files
Add StoppedTimer helper (#3280)
Co-authored-by: Stephen Buttolph <[email protected]>
1 parent f5cb0ae commit e85b99a

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

network/throttling/inbound_conn_upgrade_throttler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/ava-labs/avalanchego/utils/logging"
1212
"github.com/ava-labs/avalanchego/utils/set"
1313
"github.com/ava-labs/avalanchego/utils/timer/mockable"
14+
15+
timerpkg "github.com/ava-labs/avalanchego/utils/timer"
1416
)
1517

1618
var (
@@ -131,10 +133,7 @@ func (n *inboundConnUpgradeThrottler) ShouldUpgrade(addrPort netip.AddrPort) boo
131133
}
132134

133135
func (n *inboundConnUpgradeThrottler) Dispatch() {
134-
timer := time.NewTimer(0)
135-
if !timer.Stop() {
136-
<-timer.C
137-
}
136+
timer := timerpkg.StoppedTimer()
138137

139138
defer timer.Stop()
140139
for {

network/throttling/inbound_resource_throttler.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/ava-labs/avalanchego/ids"
1616
"github.com/ava-labs/avalanchego/snow/networking/tracker"
1717
"github.com/ava-labs/avalanchego/utils/timer/mockable"
18+
19+
timerpkg "github.com/ava-labs/avalanchego/utils/timer"
1820
)
1921

2022
const epsilon = time.Millisecond
@@ -107,11 +109,7 @@ func NewSystemThrottler(
107109
timerPool: sync.Pool{
108110
New: func() interface{} {
109111
// Satisfy invariant that timer is stopped and drained.
110-
timer := time.NewTimer(0)
111-
if !timer.Stop() {
112-
<-timer.C
113-
}
114-
return timer
112+
return timerpkg.StoppedTimer()
115113
},
116114
},
117115
}, nil

tests/antithesis/avalanchego/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
3434
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
3535

36+
timerpkg "github.com/ava-labs/avalanchego/utils/timer"
3637
xtxs "github.com/ava-labs/avalanchego/vms/avm/txs"
3738
ptxs "github.com/ava-labs/avalanchego/vms/platformvm/txs"
3839
xbuilder "github.com/ava-labs/avalanchego/wallet/chain/x/builder"
@@ -145,10 +146,7 @@ type workload struct {
145146
}
146147

147148
func (w *workload) run(ctx context.Context) {
148-
timer := time.NewTimer(0)
149-
if !timer.Stop() {
150-
<-timer.C
151-
}
149+
timer := timerpkg.StoppedTimer()
152150

153151
tc := tests.NewTestContext()
154152
defer tc.Cleanup()

tests/antithesis/xsvm/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/ava-labs/avalanchego/vms/example/xsvm/api"
2727
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/status"
2828
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/transfer"
29+
30+
timerpkg "github.com/ava-labs/avalanchego/utils/timer"
2931
)
3032

3133
const (
@@ -123,10 +125,7 @@ type workload struct {
123125
}
124126

125127
func (w *workload) run(ctx context.Context) {
126-
timer := time.NewTimer(0)
127-
if !timer.Stop() {
128-
<-timer.C
129-
}
128+
timer := timerpkg.StoppedTimer()
130129

131130
tc := tests.NewTestContext()
132131
defer tc.Cleanup()

utils/timer/stopped_timer.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package timer
5+
6+
import "time"
7+
8+
// StoppedTimer returns a stopped timer so that there is no entry on
9+
// the C channel (and there isn't one scheduled to be added).
10+
//
11+
// This means that after calling Reset there will be no events on the
12+
// channel until the timer fires (at which point there will be exactly
13+
// one event sent to the channel).
14+
//
15+
// It enables re-using the timer across loop iterations without
16+
// needing to have the first loop iteration perform any == nil checks
17+
// to initialize the first invocation.
18+
func StoppedTimer() *time.Timer {
19+
timer := time.NewTimer(0)
20+
if !timer.Stop() {
21+
<-timer.C
22+
}
23+
return timer
24+
}

0 commit comments

Comments
 (0)