Skip to content

Commit

Permalink
Add StoppedTimer helper
Browse files Browse the repository at this point in the history
As per @StephenButtolph's comment:
ava-labs/subnet-evm#1166 (comment)

Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
marun and StephenButtolph committed Aug 8, 2024
1 parent 6626d2b commit b384723
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
7 changes: 3 additions & 4 deletions network/throttling/inbound_conn_upgrade_throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer/mockable"

utils_timer "github.com/ava-labs/avalanchego/utils/timer"
)

var (
Expand Down Expand Up @@ -131,10 +133,7 @@ func (n *inboundConnUpgradeThrottler) ShouldUpgrade(addrPort netip.AddrPort) boo
}

func (n *inboundConnUpgradeThrottler) Dispatch() {
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
timer := utils_timer.StoppedTimer()

defer timer.Stop()
for {
Expand Down
6 changes: 2 additions & 4 deletions tests/antithesis/avalanchego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"

utils_timer "github.com/ava-labs/avalanchego/utils/timer"
xtxs "github.com/ava-labs/avalanchego/vms/avm/txs"
ptxs "github.com/ava-labs/avalanchego/vms/platformvm/txs"
xbuilder "github.com/ava-labs/avalanchego/wallet/chain/x/builder"
Expand Down Expand Up @@ -148,10 +149,7 @@ type workload struct {
}

func (w *workload) run(ctx context.Context) {
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
timer := utils_timer.StoppedTimer()

var (
xWallet = w.wallet.X()
Expand Down
7 changes: 3 additions & 4 deletions tests/antithesis/xsvm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/ava-labs/avalanchego/vms/example/xsvm/api"
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/status"
"github.com/ava-labs/avalanchego/vms/example/xsvm/cmd/issue/transfer"

utils_timer "github.com/ava-labs/avalanchego/utils/timer"
)

const (
Expand Down Expand Up @@ -118,10 +120,7 @@ type workload struct {
}

func (w *workload) run(ctx context.Context) {
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
timer := utils_timer.StoppedTimer()

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

Expand Down
26 changes: 26 additions & 0 deletions utils/timer/stopped_timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package timer

import (
"time"
)

// StoppedTimer returns a stopped timer so that there is no entry on
// the C channel (and there isn't one scheduled to be added).
//
// This means that after calling Reset there will be no events on the
// channel until the timer fires (at which point there will be exactly
// one event sent to the channel).

// It enables re-using the timer across loop iterations without
// needing to have the first loop iteration perform any == nil checks
// to initialize the first invocation.
func StoppedTimer() *time.Timer {
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
return timer
}

0 comments on commit b384723

Please sign in to comment.