Skip to content

feat: Allow blocking until primary rate limit is reset - #3117

Merged
gmlewis merged 11 commits into
google:masterfrom
erezrokah:feat/sleep_until_primary_rate_limit
May 1, 2024
Merged

feat: Allow blocking until primary rate limit is reset#3117
gmlewis merged 11 commits into
google:masterfrom
erezrokah:feat/sleep_until_primary_rate_limit

Conversation

@erezrokah

@erezrokaherezrokah commented Apr 1, 2024

Copy link
Copy Markdown
Contributor

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

@codecov

codecovBot commented Apr 1, 2024

Copy link
Copy Markdown

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

FilesPatch %Lines
github/github.go90.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.

Comment threadgithub/github.go Outdated
@gmlewis

Copy link
Copy Markdown
Collaborator

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

@gmlewisgmlewis added the NeedsReview PR is awaiting a review before merging. label Apr 2, 2024

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

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 gmlewisApril 2, 2024 16:16
@erezrokah

Copy link
Copy Markdown
ContributorAuthor

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

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
ContributorAuthor

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
}
returnnil, ctx.Err()
case<-timer.C:
}

@gmlewis

Copy link
Copy Markdown
Collaborator

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

@erezrokah

Copy link
Copy Markdown
ContributorAuthor

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

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

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.

Comment threadgithub/github.go Outdated
Comment threadgithub/github.go Outdated
erezrokahand 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>

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, @erezrokah !
LGTM.

@be0x74abe0x74a left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

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

Copy link
Copy Markdown
Collaborator

Thank you, @be0x74a !
Merging.

@gmlewis
gmlewis merged commit 497dbe8 into google:masterMay 1, 2024
@erezrokah
erezrokah deleted the feat/sleep_until_primary_rate_limit branch May 6, 2024 10:26
@erezrokah

Copy link
Copy Markdown
ContributorAuthor

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

@gmlewis

Copy link
Copy Markdown
Collaborator

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.

@gmlewis

Copy link
Copy Markdown
Collaborator

This is now available here: https://github.com/google/go-github/releases/tag/v62.0.0

Sign up for freeto 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

@erezrokah@gmlewis@be0x74a