Skip to content

Commit

Permalink
Add StoppedTimer helper (#3280)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
  • Loading branch information
marun and StephenButtolph committed Aug 8, 2024
1 parent f5cb0ae commit e85b99a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 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"

timerpkg "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 := timerpkg.StoppedTimer()

defer timer.Stop()
for {
Expand Down
8 changes: 3 additions & 5 deletions network/throttling/inbound_resource_throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/networking/tracker"
"github.com/ava-labs/avalanchego/utils/timer/mockable"

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

const epsilon = time.Millisecond
Expand Down Expand Up @@ -107,11 +109,7 @@ func NewSystemThrottler(
timerPool: sync.Pool{
New: func() interface{} {
// Satisfy invariant that timer is stopped and drained.
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
return timer
return timerpkg.StoppedTimer()
},
},
}, nil
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 @@ -33,6 +33,7 @@ import (
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"

timerpkg "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 @@ -145,10 +146,7 @@ type workload struct {
}

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

tc := tests.NewTestContext()
defer tc.Cleanup()
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 @@ -26,6 +26,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"

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

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

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

tc := tests.NewTestContext()
defer tc.Cleanup()
Expand Down
24 changes: 24 additions & 0 deletions utils/timer/stopped_timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 e85b99a

Please sign in to comment.