Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StoppedTimer helper #3280

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional, subjective) When a package and a local variable clash, one recommended approach is to use the pkg suffix so this would be timerpkg.

The underscore in the name is what jumped out at me as out of the ordinary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

)

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
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).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) You've accidentally broken the comment here. I'm not sure if doc parsers will fix that or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should also be used here:

// Satisfy invariant that timer is stopped and drained.
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
return timer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
return timer
}
Loading