File tree 5 files changed +35
-17
lines changed 5 files changed +35
-17
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
+ timerpkg "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 := timerpkg .StoppedTimer ()
138
137
139
138
defer timer .Stop ()
140
139
for {
Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ import (
15
15
"github.com/ava-labs/avalanchego/ids"
16
16
"github.com/ava-labs/avalanchego/snow/networking/tracker"
17
17
"github.com/ava-labs/avalanchego/utils/timer/mockable"
18
+
19
+ timerpkg "github.com/ava-labs/avalanchego/utils/timer"
18
20
)
19
21
20
22
const epsilon = time .Millisecond
@@ -107,11 +109,7 @@ func NewSystemThrottler(
107
109
timerPool : sync.Pool {
108
110
New : func () interface {} {
109
111
// 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 ()
115
113
},
116
114
},
117
115
}, nil
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ import (
33
33
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
34
34
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
35
35
36
+ timerpkg "github.com/ava-labs/avalanchego/utils/timer"
36
37
xtxs "github.com/ava-labs/avalanchego/vms/avm/txs"
37
38
ptxs "github.com/ava-labs/avalanchego/vms/platformvm/txs"
38
39
xbuilder "github.com/ava-labs/avalanchego/wallet/chain/x/builder"
@@ -145,10 +146,7 @@ type workload struct {
145
146
}
146
147
147
148
func (w * workload ) run (ctx context.Context ) {
148
- timer := time .NewTimer (0 )
149
- if ! timer .Stop () {
150
- <- timer .C
151
- }
149
+ timer := timerpkg .StoppedTimer ()
152
150
153
151
tc := tests .NewTestContext ()
154
152
defer tc .Cleanup ()
Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ import (
26
26
"github.com/ava-labs/avalanchego/vms/example/xsvm/api"
27
27
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/status"
28
28
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/transfer"
29
+
30
+ timerpkg "github.com/ava-labs/avalanchego/utils/timer"
29
31
)
30
32
31
33
const (
@@ -123,10 +125,7 @@ type workload struct {
123
125
}
124
126
125
127
func (w * workload ) run (ctx context.Context ) {
126
- timer := time .NewTimer (0 )
127
- if ! timer .Stop () {
128
- <- timer .C
129
- }
128
+ timer := timerpkg .StoppedTimer ()
130
129
131
130
tc := tests .NewTestContext ()
132
131
defer tc .Cleanup ()
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