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: Allow blocking until primary rate limit is reset #3117

Merged
merged 11 commits into from May 1, 2024

Conversation

erezrokah
Copy link
Contributor

@erezrokah erezrokah commented Apr 1, 2024

Fixes #3114

Feedback very much welcomed, some notes:

  1. I used the same approach as bypassRateLimitCheck with the exception that bypassRateLimitCheck is private, not sure if to use another dedicated exported requestContext type or also make bypassRateLimitCheck public for consistency
  2. Checking req.Context().Value(SleepUntilPrimaryRateLimitResetWhenRateLimited) == true is also an option (instead of !=nil) and might be more readable, but that is not consistent with the bypassRateLimitCheck check
  3. A possible improvement can be to add a log message/invoke a callback to consumers so they can show an indication the sleep is happening to avoid looking like something is stuck. If that makes sense it should be in a separate PR I think as that's would be a new API/interface for this module
  4. If this approach makes send I will add tests for it

Copy link

codecov bot commented Apr 1, 2024

Codecov Report

Attention: Patch coverage is 90.90909% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 92.87%. Comparing base (2b8c7fa) to head (960ce81).
Report is 43 commits behind head on master.

❗ Current head 960ce81 differs from pull request most recent head ea82df3. Consider uploading reports for the commit ea82df3 to get more accurate results

Files Patch % Lines
github/github.go 90.90% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3117      +/-   ##
==========================================
- Coverage   97.72%   92.87%   -4.85%     
==========================================
  Files         153      170      +17     
  Lines       13390    11445    -1945     
==========================================
- Hits        13085    10630    -2455     
- Misses        215      724     +509     
- Partials       90       91       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

github/github.go Outdated Show resolved Hide resolved
@gmlewis
Copy link
Collaborator

gmlewis commented Apr 1, 2024

It would also be nice to add one or more unit tests that would increase the code coverage for the new code.

@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Apr 2, 2024
Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thank you, @erezrokah !
LGTM.

Awaiting second LGTM+Approval from any other contributor to this repo before merging.

@erezrokah erezrokah requested a review from gmlewis April 2, 2024 16:16
@erezrokah
Copy link
Contributor Author

Thank you, @erezrokah ! LGTM.

Awaiting second LGTM+Approval from any other contributor to this repo before merging.

Thanks for the quick review and feedback, I also added a 1 second buffer to the sleep time to avoid any edge cases

Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thanks, @erezrokah .

Still awaiting second LGTM+Approval from any other contributor to this repo before merging.

All contributions, including code reviews, by other volunteers is greatly appreciated, as we have several outstanding PRs that need reviews. Thanks.

@erezrokah
Copy link
Contributor Author

Hi @gmlewis I'm wondering if I should update the PR to take into account context cancellation, by changing sleepUntilResetWithBuffer to

timer := time.NewTimer(time.Until(reset) + buffer)
select {
case <-ctx.Done():
	if !timer.Stop() {
	<-timer.C
	}
	return nil, ctx.Err()
case <-timer.C:
}

@gmlewis
Copy link
Collaborator

gmlewis commented Apr 17, 2024

Ah, that's an excellent idea, @erezrokah - I think you are right that this should be added.

@erezrokah
Copy link
Contributor Author

Ah, that's an excellent idea, @erezrokah - I think you are right that this should be added.

Added 22eba4c. In one case I had to wrap the context error in a rate limit error. I can also refactor the code a bit to have checkRateLimitBeforeDo return an error instead of *RateLimitError, but thought it would be better to get some feedback first

Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Just a couple nits, otherwise LGTM.

github/github.go Outdated Show resolved Hide resolved
github/github.go Outdated Show resolved Hide resolved
erezrokah and others added 2 commits April 18, 2024 20:57
Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thank you, @erezrokah !
LGTM.

Copy link
Contributor

@be0x74a be0x74a left a comment

Choose a reason for hiding this comment

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

LGTM!

@gmlewis gmlewis removed the NeedsReview PR is awaiting a review before merging. label May 1, 2024
@gmlewis
Copy link
Collaborator

gmlewis commented May 1, 2024

Thank you, @be0x74a !
Merging.

@gmlewis gmlewis merged commit 497dbe8 into google:master May 1, 2024
5 checks passed
@erezrokah erezrokah deleted the feat/sleep_until_primary_rate_limit branch May 6, 2024 10:26
@erezrokah
Copy link
Contributor Author

Thanks for the review(s) @be0x74a and @gmlewis, when can we expect a new release to be tagged with this commit?

@gmlewis
Copy link
Collaborator

gmlewis commented May 6, 2024

Thanks for the review(s) @be0x74a and @gmlewis, when can we expect a new release to be tagged with this commit?

I'm away from Internet this week so the earliest will be this Friday.

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.

feat: Add an option to wait for primary rate limit reset and retry instead of erroring out
3 participants