Skip to content

fix: Do first try immediately, then activate floor #27

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

Merged
merged 1 commit into from
Feb 10, 2023

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Feb 9, 2023

I noticed retries were always taking floor to activate (https://github.com/coder/coder/blob/af59e2bcfa524dd0f93a186b69a8e43d2a31702e/agent/agent.go#L189), and then realized that floor isn't really even used here. So I assumed this was a bug and changed it so that first try is done immediately.

@mafredri mafredri self-assigned this Feb 9, 2023
@mafredri mafredri requested a review from ammario February 9, 2023 20:57
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

While this change seems innocuous enough, I could imagine an existing use case similar to the following:

var wg sync.WaitGroup
for i := 0; i < 1_000_000; i++ {
  wg.Add(1)
  go func() {
    defer wg.Done()
    for retrier := retry.New(rand.Intn(30) * time.Second, time.Minute); retrier.Wait(ctx); {
      if err := doSomething(); err == nil {
        return
      }
    }
  }
}
wg.Wait()

With the current behaviour, the parallel executions of doSomething would be smeared across the initial delay. The new behaviour would cause a thundering herd of doSomethings() all at once, which would be a rather unexpected change IMO.

@mafredri
Copy link
Member Author

@johnstcn that's a great observation and definitely worth consideration. I don't know what the original intent here was, but to me it feels weird that when using this library you'd have to write it like this:

if err := doSomething(); err == nil {
	return
}
for r := retry.New(time.Second, 10*time.Second); r.Wait(ctx); {
	if err := doSomething(); err == nil {
		return
	}
}

Our current use in https://github.com/coder/coder all seems to behave in such a way that I would expect the behavior provided by this PR, but ¯\_(ツ)_/¯.

@johnstcn
Copy link
Member

Our current use in https://github.com/coder/coder all seems to behave in such a way that I would expect the behavior provided by this PR, but ¯\_(ツ)_/¯.

A quick search on SourceGraph doesn't bring up any public external usages. That doesn't rule out private repo usage however.

@mafredri
Copy link
Member Author

Judging by the old API, https://github.com/coder/retry/blob/v1.2.0/retry.go#L274-L288, I'd say this is how this library used to behave as well. I.e. first try is done immediately, then sleep.

@Emyrk
Copy link
Member

Emyrk commented Feb 10, 2023

The current api matches how time.Ticker works:

https://goplay.tools/snippet/C_LwHjojQi9

Food for thought.

@Emyrk
Copy link
Member

Emyrk commented Feb 10, 2023

just noticed the rety package we use in v1 is totally different syntax

https://github.com/coder/m/blob/882403200cc9e649fd7265938f3855ab43ce93ad/product/coder/pkg/model/license.go#L146-L150

we use a different version of the same package, and the package looks entirely different 🤷‍♂️

@mafredri
Copy link
Member Author

@Emyrk fair point, but do you think people would expect it to behave the same way? The only way I can make sense of the API as it's intended to be used (readme), and enforcing an initial delay, would be to have an explicit argument for it. 🤔

@Emyrk
Copy link
Member

Emyrk commented Feb 10, 2023

@mafredri I think you are right, it should fire immediately. Was just trying to play devil's advocate in any rational of the opposite 😄. I then realized then the retry I've been using in v1 is completely different then v2.

You could always make it some argument in the chaining if someone needs that original delay.


tl'dr I like your PR, I just do not know the impact

Copy link
Member

@ammario ammario left a comment

Choose a reason for hiding this comment

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

Good catch. This should be fine.

@ammario ammario merged commit e90a2e1 into master Feb 10, 2023
@mafredri mafredri deleted the mafredri/quick-first-try branch February 10, 2023 18:19
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.

4 participants