Skip to content

Commit 7ed9df8

Browse files
Add StoppedTimer helper
As per @StephenButtolph's comment: ava-labs/subnet-evm#1166 (comment) Co-authored-by: Stephen Buttolph <[email protected]>
1 parent 6626d2b commit 7ed9df8

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
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+
utils_timer "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 := utils_timer.StoppedTimer()
138137

139138
defer timer.Stop()
140139
for {

tests/antithesis/avalanchego/main.go

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

34+
utils_timer "github.com/ava-labs/avalanchego/utils/timer"
3435
xtxs "github.com/ava-labs/avalanchego/vms/avm/txs"
3536
ptxs "github.com/ava-labs/avalanchego/vms/platformvm/txs"
3637
xbuilder "github.com/ava-labs/avalanchego/wallet/chain/x/builder"
@@ -148,10 +149,7 @@ type workload struct {
148149
}
149150

150151
func (w *workload) run(ctx context.Context) {
151-
timer := time.NewTimer(0)
152-
if !timer.Stop() {
153-
<-timer.C
154-
}
152+
timer := utils_timer.StoppedTimer()
155153

156154
var (
157155
xWallet = w.wallet.X()

tests/antithesis/xsvm/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"github.com/ava-labs/avalanchego/vms/example/xsvm/api"
2424
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/status"
2525
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/transfer"
26+
27+
utils_timer "github.com/ava-labs/avalanchego/utils/timer"
2628
)
2729

2830
const (
@@ -118,10 +120,7 @@ type workload struct {
118120
}
119121

120122
func (w *workload) run(ctx context.Context) {
121-
timer := time.NewTimer(0)
122-
if !timer.Stop() {
123-
<-timer.C
124-
}
123+
timer := utils_timer.StoppedTimer()
125124

126125
uri := w.uris[w.id%len(w.uris)]
127126

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)