-
Notifications
You must be signed in to change notification settings - Fork 4
Fix panic in rate limit #226
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
Conversation
WalkthroughThe pull request introduces changes to the error handling and rate limit management in the codebase. It modifies the Changes
Assessment against linked issues
Possibly related issues
Possibly related PRs
Poem
Recent review detailsConfiguration used: CodeRabbit UI Files selected for processing (2)
Files skipped from review as they are similar to previous changes (1)
Additional comments not posted (2)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- pkg/sync/syncer.go (1 hunks)
- pkg/uhttp/wrapper.go (3 hunks)
Additional comments not posted (2)
pkg/uhttp/wrapper.go (1)
Line range hint
216-232: LGTM!The function correctly wraps errors with rate limit information extracted from the HTTP response. It handles the case when no rate limit data is found by returning the joined errors, and adds the rate limit data to the status details when it is found. The status error is correctly joined with the provided errors.
pkg/sync/syncer.go (1)
111-119: The changes toshouldWaitAndRetryimprove the robustness of the wait time calculation.The modifications handle edge cases more gracefully:
- Checking for zero duration to prevent division by zero errors.
- Calculating wait time using
waitAtRestderived from the reset time and adjusted duration.- Rounding up the final wait time to the nearest second to avoid hitting the rate limit again.
These enhancements ensure the function can handle various scenarios without causing unexpected issues.
pkg/uhttp/wrapper.go
Outdated
| return resp, WrapErrorsWithRateLimitInfo(codes.DeadlineExceeded, resp, optErrs...) | ||
| case http.StatusTooManyRequests, http.StatusServiceUnavailable: | ||
| return resp, GRPCWrap(codes.Unavailable, resp, optErrs...) | ||
| return resp, WrapErrorsWithRateLimitInfo(codes.Unavailable, resp, optErrs...) | ||
| case http.StatusNotFound: | ||
| return resp, GRPCWrap(codes.NotFound, resp, optErrs...) | ||
| return resp, WrapErrorsWithRateLimitInfo(codes.NotFound, resp, optErrs...) | ||
| case http.StatusUnauthorized: | ||
| return resp, GRPCWrap(codes.Unauthenticated, resp, optErrs...) | ||
| return resp, WrapErrorsWithRateLimitInfo(codes.Unauthenticated, resp, optErrs...) | ||
| case http.StatusForbidden: | ||
| return resp, GRPCWrap(codes.PermissionDenied, resp, optErrs...) | ||
| return resp, WrapErrorsWithRateLimitInfo(codes.PermissionDenied, resp, optErrs...) | ||
| case http.StatusNotImplemented: | ||
| return resp, GRPCWrap(codes.Unimplemented, resp, optErrs...) | ||
| return resp, WrapErrorsWithRateLimitInfo(codes.Unimplemented, resp, optErrs...) | ||
| } | ||
|
|
||
| if resp.StatusCode >= 500 && resp.StatusCode <= 599 { | ||
| return resp, GRPCWrap(codes.Unavailable, resp, optErrs...) | ||
| return resp, WrapErrorsWithRateLimitInfo(codes.Unavailable, resp, optErrs...) | ||
| } | ||
|
|
||
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
| return resp, GRPCWrap(codes.Unknown, resp, append(optErrs, fmt.Errorf("unexpected status code: %d", resp.StatusCode))...) | ||
| // TODO: add opterrs here | ||
| return resp, WrapErrorsWithRateLimitInfo(codes.Unknown, resp, fmt.Errorf("unexpected status code: %d", resp.StatusCode)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass optErrs to WrapErrorsWithRateLimitInfo for unexpected status codes.
The method correctly uses WrapErrorsWithRateLimitInfo to wrap errors with rate limit information for specific HTTP status codes and unexpected status codes. However, it does not pass the optErrs to WrapErrorsWithRateLimitInfo for unexpected status codes, which is inconsistent with the behavior for specific HTTP status codes.
Apply this diff to pass the optErrs to WrapErrorsWithRateLimitInfo for unexpected status codes:
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
- // TODO: add opterrs here
- return resp, WrapErrorsWithRateLimitInfo(codes.Unknown, resp, fmt.Errorf("unexpected status code: %d", resp.StatusCode))
+ return resp, WrapErrorsWithRateLimitInfo(codes.Unknown, resp, append(optErrs, fmt.Errorf("unexpected status code: %d", resp.StatusCode))...)
}Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| return resp, WrapErrorsWithRateLimitInfo(codes.DeadlineExceeded, resp, optErrs...) | |
| case http.StatusTooManyRequests, http.StatusServiceUnavailable: | |
| return resp, GRPCWrap(codes.Unavailable, resp, optErrs...) | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unavailable, resp, optErrs...) | |
| case http.StatusNotFound: | |
| return resp, GRPCWrap(codes.NotFound, resp, optErrs...) | |
| return resp, WrapErrorsWithRateLimitInfo(codes.NotFound, resp, optErrs...) | |
| case http.StatusUnauthorized: | |
| return resp, GRPCWrap(codes.Unauthenticated, resp, optErrs...) | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unauthenticated, resp, optErrs...) | |
| case http.StatusForbidden: | |
| return resp, GRPCWrap(codes.PermissionDenied, resp, optErrs...) | |
| return resp, WrapErrorsWithRateLimitInfo(codes.PermissionDenied, resp, optErrs...) | |
| case http.StatusNotImplemented: | |
| return resp, GRPCWrap(codes.Unimplemented, resp, optErrs...) | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unimplemented, resp, optErrs...) | |
| } | |
| if resp.StatusCode >= 500 && resp.StatusCode <= 599 { | |
| return resp, GRPCWrap(codes.Unavailable, resp, optErrs...) | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unavailable, resp, optErrs...) | |
| } | |
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | |
| return resp, GRPCWrap(codes.Unknown, resp, append(optErrs, fmt.Errorf("unexpected status code: %d", resp.StatusCode))...) | |
| // TODO: add opterrs here | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unknown, resp, fmt.Errorf("unexpected status code: %d", resp.StatusCode)) | |
| return resp, WrapErrorsWithRateLimitInfo(codes.DeadlineExceeded, resp, optErrs...) | |
| case http.StatusTooManyRequests, http.StatusServiceUnavailable: | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unavailable, resp, optErrs...) | |
| case http.StatusNotFound: | |
| return resp, WrapErrorsWithRateLimitInfo(codes.NotFound, resp, optErrs...) | |
| case http.StatusUnauthorized: | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unauthenticated, resp, optErrs...) | |
| case http.StatusForbidden: | |
| return resp, WrapErrorsWithRateLimitInfo(codes.PermissionDenied, resp, optErrs...) | |
| case http.StatusNotImplemented: | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unimplemented, resp, optErrs...) | |
| } | |
| if resp.StatusCode >= 500 && resp.StatusCode <= 599 { | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unavailable, resp, optErrs...) | |
| } | |
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | |
| return resp, WrapErrorsWithRateLimitInfo(codes.Unknown, resp, append(optErrs, fmt.Errorf("unexpected status code: %d", resp.StatusCode))...) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ggreer thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
6583fb1 to
80cce11
Compare
| wait /= time.Duration(rlData.Limit) | ||
| waitResetAt := time.Until(rlData.ResetAt.AsTime()) | ||
| duration := time.Duration(rlData.Limit) | ||
| if duration == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duration can be negative so we probably want this to be <= 0.
Summary by CodeRabbit
New Features
Bug Fixes