-
Notifications
You must be signed in to change notification settings - Fork 675
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
Add StoppedTimer helper #3280
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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). | ||||||||||||||
|
||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should also be used here: avalanchego/network/throttling/inbound_resource_throttler.go Lines 109 to 114 in 6626d2b
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||
timer := time.NewTimer(0) | ||||||||||||||
if !timer.Stop() { | ||||||||||||||
<-timer.C | ||||||||||||||
} | ||||||||||||||
return timer | ||||||||||||||
} |
There was a problem hiding this comment.
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 betimerpkg
.The underscore in the name is what jumped out at me as out of the ordinary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done