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

Feat/refactor wait time #1

Merged
merged 7 commits into from
Nov 9, 2018
Merged

Feat/refactor wait time #1

merged 7 commits into from
Nov 9, 2018

Conversation

hannahhoward
Copy link
Contributor

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

  • Add a unit test
  • Implement NextWaitTime() for Fixed, VariableNormalized, and VariableUniform
  • Refactor Wait() to simply be a call to NextWaitTime() followed by a sleep
  • Update travis go version so CI will run

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.

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
@ghost ghost assigned hannahhoward Nov 6, 2018
@ghost ghost added the status/in-progress In progress label Nov 6, 2018
Copy link

@eingenito eingenito left a 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)

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

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.

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 {

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

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.

@hannahhoward hannahhoward merged commit 4e66968 into master Nov 9, 2018
@ghost ghost removed the status/in-progress In progress label Nov 9, 2018
@Stebalien Stebalien deleted the feat/refactor-wait-time branch November 9, 2018 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants