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 Wait and WaitCtx convenience functions #1

Merged
merged 1 commit into from
Aug 7, 2023
Merged

add Wait and WaitCtx convenience functions #1

merged 1 commit into from
Aug 7, 2023

Conversation

RaduBerinde
Copy link
Member

The common usage for TryToFulfill is to sleep in a loop. This code adds a convenience Wait method and a corresponding WaitCtx which respects context cancelation.

@RaduBerinde RaduBerinde requested review from dt and knz July 31, 2023 16:12
@RaduBerinde
Copy link
Member Author

CC @RahulAggarwal1016 WaitCtx could be used in cockroachdb/pebble#2779

@@ -137,6 +139,39 @@ func (tb *TokenBucket) TryToFulfill(amount Tokens) (fulfilled bool, tryAgainAfte
return true, 0
}

// Wait removes the given amount, waiting as long as necessary.
func (tb *TokenBucket) Wait(amount Tokens) {
Copy link
Member

Choose a reason for hiding this comment

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

Is Wait equivalent to WaitCtx(context.Background(), amount)? Is it worth expressing it like that, or are you concerned about the added cost of the context accesses?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I removed the no-context version.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I wasn't saying to remove it, I was just suggesting that you could implement Wait as WaitCtx(context.Background(), amount). But this works too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking callers can just pass context.Background(). I realized that it is annoying that it returns an error in that case though, added it back.

ctxCancel()
select {
case err := <-waitResult:
if err == nil || !strings.Contains(err.Error(), "context canceled") {
Copy link
Member

Choose a reason for hiding this comment

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

nit: can we replace the string comparison with errors.Is(err, context.Canceled)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

LGTM, with or without Wait.

The common usage for `TryToFulfill` is to sleep in a loop. This code
adds a convenience `Wait` method and a corresponding `WaitCtx` which
respects context cancelation.
@RaduBerinde
Copy link
Member Author

TFTR!

@RaduBerinde RaduBerinde merged commit cc333fc into master Aug 7, 2023
@RaduBerinde RaduBerinde deleted the wait branch August 7, 2023 17:45
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