-
Notifications
You must be signed in to change notification settings - Fork 4
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
Feat/refactor wait time #1
Conversation
Refactor to seperate the act of waiting from reading the duration of the next wait time, so that a calling module can use the waiting on its own Also add a unit test
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.
Stylistic comments only. I wonder what others will think (if anyone else reviews).
delay.go
Outdated
d.l.RLock() | ||
time.Sleep(nextWaitTime) |
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.
I think maybe this should go after the defer call.
delay.go
Outdated
@@ -6,24 +6,22 @@ import ( | |||
"time" | |||
) | |||
|
|||
var sharedRNG = rand.New(rand.NewSource(time.Now().UnixNano())) | |||
var sharedRealSleeper = NewRealSleeper() |
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.
This is a stylistic thing but I think I'd just name these like sleeper and NewSleeper(). I would put the onus on the tests to name things in a 'test' obvious way.
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.
Also should we bother with a shared sleeper? The cached RNG is probably expensive, but this sleeper is for sure not. I don't know if it's confusing or not.
sleeper.go
Outdated
} | ||
|
||
// NewRealSleeper - returns a new sleeper that uses the real time.Sleep function | ||
func NewRealSleeper() Sleeper { |
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.
Do we always do this? Do we ever just instantiate sleepers where we need them?
delay.go
Outdated
@@ -37,7 +35,13 @@ func (d *delay) Set(t time.Duration) time.Duration { | |||
func (d *delay) Wait() { | |||
d.l.RLock() | |||
defer d.l.RUnlock() | |||
time.Sleep(d.t) | |||
d.sleeper.Sleep(d.generator.NextWaitTime(d.t)) |
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.
I get that this becomes crazy to test when there's a real sleep in here and there's random variation. This is on the limit for me in terms of tests invading my impl.
Goals
Some calling code may want to perform the waiting on its own but still be able to access normalized or uniform random wait times. (I have a specific use case for this in go-bitswap's testing in mind) This PR allows you to get those values directly via a NextWaitTime() function.
Implementation
For discussion
I left mutex locking in the Wait function just to make sure the functionality is exactly equivalent so this is not a non-breaking change. However, maybe it isn't necessary.