feat: Add WithRateLimitSleepCallback option#4316
Open
JamBalaya56562 wants to merge 2 commits into
Open
Conversation
When SleepUntilPrimaryRateLimitResetWhenRateLimited is set, the client sleeps until the primary rate limit resets, but never signals that the sleep is happening, so callers cannot log or otherwise react to it. WithRateLimitSleepCallback registers a callback that is invoked just before the client sleeps, on both the pre-emptive and the retry paths. The callback receives a RateLimitSleepInfo with the triggering request and rate limit. It is notification-only and the client behaves exactly as before when no callback is set. Fixes google#3220.
WithRateLimitSleepCallback option
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4316 +/- ##
=======================================
Coverage 97.47% 97.47%
=======================================
Files 192 192
Lines 19310 19320 +10
=======================================
+ Hits 18822 18832 +10
Misses 270 270
Partials 218 218 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
gmlewis
approved these changes
Jun 20, 2026
gmlewis
left a comment
Collaborator
There was a problem hiding this comment.
Thank you, @JamBalaya56562!
LGTM.
Awaiting second LGTM+Approval from any other contributor to this repo before merging.
gmlewis
requested changes
Jun 20, 2026
gmlewis
left a comment
Collaborator
There was a problem hiding this comment.
Whups, I didn't wait for the linter. Please run step 4 of CONTRIBUTING.md and fix the lint issues then push the changes to this PR.
Running [/home/runner/work/go-github/go-github/custom-gcl config path] in [/home/runner/work/go-github/go-github] ...
Running [/home/runner/work/go-github/go-github/custom-gcl config verify] in [/home/runner/work/go-github/go-github] ...
Running [/home/runner/work/go-github/go-github/custom-gcl run --path-mode=abs] in [/home/runner/work/go-github/go-github] ...
Error: /home/runner/work/go-github/go-github/github/github_test.go:2796:3: use %v instead of %d (fmtpercentv)
t.Errorf("rate limit sleep callback called %d times, want 1", callbackCount)
^
Error: /home/runner/work/go-github/go-github/github/github_test.go:2838:3: use %v instead of %d (fmtpercentv)
t.Errorf("rate limit sleep callback called %d times, want 1", callbackCount)
^
2 issues:
* fmtpercentv: 2
* ```
golangci-lint (fmtpercentv) requires %v instead of %d.
Contributor
Author
gmlewis
approved these changes
Jun 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
SleepUntilPrimaryRateLimitResetWhenRateLimitedis set, the client sleeps until the primary rate limit resets, but it never signals that the sleep is happening — so callers cannot log a message or otherwise react, and the program can appear stuck. This was noted as a possible future improvement in #3117.This adds
WithRateLimitSleepCallback, aClientOptionsFunc(as suggested by @erezrokah in the issue) that registers a callback invoked just before the client sleeps, on both the pre-emptive (checkRateLimitBeforeDo) and the retry (bareDo) paths. The callback receives aRateLimitSleepInfocarrying the triggering request and theRate, and is notification-only.Behavior is unchanged when no callback is set (nil-guarded). The option is carried over by
Clone. Tests cover the option, both sleep paths, and the existing no-callback paths.Fixes #3220.