File tree 4 files changed +32
-12
lines changed
4 files changed +32
-12
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ import (
11
11
"github.com/ava-labs/avalanchego/utils/logging"
12
12
"github.com/ava-labs/avalanchego/utils/set"
13
13
"github.com/ava-labs/avalanchego/utils/timer/mockable"
14
+
15
+ utils_timer "github.com/ava-labs/avalanchego/utils/timer"
14
16
)
15
17
16
18
var (
@@ -131,10 +133,7 @@ func (n *inboundConnUpgradeThrottler) ShouldUpgrade(addrPort netip.AddrPort) boo
131
133
}
132
134
133
135
func (n * inboundConnUpgradeThrottler ) Dispatch () {
134
- timer := time .NewTimer (0 )
135
- if ! timer .Stop () {
136
- <- timer .C
137
- }
136
+ timer := utils_timer .StoppedTimer ()
138
137
139
138
defer timer .Stop ()
140
139
for {
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ import (
31
31
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
32
32
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
33
33
34
+ utils_timer "github.com/ava-labs/avalanchego/utils/timer"
34
35
xtxs "github.com/ava-labs/avalanchego/vms/avm/txs"
35
36
ptxs "github.com/ava-labs/avalanchego/vms/platformvm/txs"
36
37
xbuilder "github.com/ava-labs/avalanchego/wallet/chain/x/builder"
@@ -148,10 +149,7 @@ type workload struct {
148
149
}
149
150
150
151
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 ()
155
153
156
154
var (
157
155
xWallet = w .wallet .X ()
Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ import (
23
23
"github.com/ava-labs/avalanchego/vms/example/xsvm/api"
24
24
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/status"
25
25
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/transfer"
26
+
27
+ utils_timer "github.com/ava-labs/avalanchego/utils/timer"
26
28
)
27
29
28
30
const (
@@ -118,10 +120,7 @@ type workload struct {
118
120
}
119
121
120
122
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 ()
125
124
126
125
uri := w .uris [w .id % len (w .uris )]
127
126
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments