Skip to content

Conversation

@shackra
Copy link
Contributor

@shackra shackra commented Sep 12, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced error handling now includes rate limit information, improving clarity in error reporting.
  • Bug Fixes

    • Improved wait time calculation for rate limits to prevent potential errors and ensure more robust handling of edge cases.

@shackra shackra requested a review from jirwin as a code owner September 12, 2024 23:12
@coderabbitai
Copy link

coderabbitai bot commented Sep 12, 2024

Walkthrough

The pull request introduces changes to the error handling and rate limit management in the codebase. It modifies the shouldWaitAndRetry function to enhance wait time calculations based on rate limits and renames the GRPCWrap function to WrapErrorsWithRateLimitInfo, improving error reporting by incorporating rate limit information. These updates ensure that edge cases are handled more effectively, particularly concerning rate limit scenarios.

Changes

Files Change Summary
pkg/sync/syncer.go Modified the shouldWaitAndRetry function to improve wait time calculations, preventing division by zero errors and ensuring robust handling of rate limit data.
pkg/uhttp/wrapper.go Renamed GRPCWrap to WrapErrorsWithRateLimitInfo and updated its logic to include rate limit information in error handling, enhancing the clarity and consistency of error reporting in the HTTP client.

Assessment against linked issues

Objective Addressed Explanation
Improve error handling (undefined)
Enhance rate limit management (undefined)

Possibly related issues

  • None identified.

Possibly related PRs

  • Rate limit support #165: This PR also focuses on enhancing error handling and retry logic related to rate limits, making it closely related to the current changes.

Poem

In the garden of code, we hop and play,
Fixing bugs and brightening the day.
With limits in check, we dance with glee,
Error messages wrapped, as clear as can be!
A leap for the future, so bright and so bold,
In the realm of sync, our story unfolds! 🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6583fb1 and f06ae51.

Files selected for processing (2)
  • pkg/sync/syncer.go (1 hunks)
  • pkg/uhttp/wrapper.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pkg/sync/syncer.go
Additional comments not posted (2)
pkg/uhttp/wrapper.go (2)

Line range hint 216-227: LGTM!

The changes to the WrapErrorsWithRateLimitInfo function improve the error handling logic by incorporating rate limit information when available. The function now checks if rate limit data can be extracted from the HTTP response and wraps the errors accordingly. If no rate limit data is found, it directly returns the joined errors passed as arguments. This enhances the error reporting and provides more context about rate limits in the error response.


299-317: LGTM!

The invocations of the WrapErrorsWithRateLimitInfo function within the Do method are consistent and handle various error scenarios appropriately. The function is used to wrap errors with rate limit information for specific HTTP status codes and unexpected status codes. This ensures that all error scenarios are consistently managed with the new error wrapping approach.

The previous review comment about passing optErrs to WrapErrorsWithRateLimitInfo for unexpected status codes has been addressed, ensuring consistency in error handling.


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between 8457b29 and 6583fb1.

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 to shouldWaitAndRetry improve the robustness of the wait time calculation.

The modifications handle edge cases more gracefully:

  1. Checking for zero duration to prevent division by zero errors.
  2. Calculating wait time using waitAtRest derived from the reset time and adjusted duration.
  3. 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.

Comment on lines 301 to 320
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))
Copy link

@coderabbitai coderabbitai bot Sep 12, 2024

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.

Suggested change
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))...)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ggreer thoughts?

Copy link

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!

@shackra shackra force-pushed the shackra/fix-rate-limit-panic-and-other-things branch from 6583fb1 to 80cce11 Compare September 12, 2024 23:50
@shackra shackra requested a review from ggreer September 12, 2024 23:53
wait /= time.Duration(rlData.Limit)
waitResetAt := time.Until(rlData.ResetAt.AsTime())
duration := time.Duration(rlData.Limit)
if duration == 0 {
Copy link
Contributor

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.

@ggreer ggreer merged commit 14ded34 into main Sep 13, 2024
@ggreer ggreer deleted the shackra/fix-rate-limit-panic-and-other-things branch September 13, 2024 00:57
@coderabbitai coderabbitai bot mentioned this pull request Dec 3, 2024
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.

3 participants