Skip to content

Conversation

@ggreer
Copy link
Contributor

@ggreer ggreer commented Jun 27, 2024

The current behavior is to retry recoverable errors every second and linearly backoff. This adds backoff that respects rate limit annotations.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced response handling across resource and entitlement listing methods, ensuring structured responses are returned even in error scenarios.
    • Improved synchronization error handling by integrating annotations into retry logic, providing better context during operations.
    • Added granular error reporting for server errors in the HTTP client, improving clarity in error responses.
    • Introduced detailed error reporting for rate limiting and service unavailability in the HTTP client, enhancing user awareness of retry conditions.
  • Bug Fixes

    • Adjusted error handling to return additional context alongside errors, facilitating better debugging and operational insights.

@ggreer ggreer requested a review from jirwin as a code owner June 27, 2024 18:00
@ggreer ggreer force-pushed the ggreer/rate-limits branch 2 times, most recently from ee25737 to 2aa8a42 Compare July 18, 2024 18:25
@shackra shackra self-assigned this Sep 3, 2024
@coderabbitai
Copy link

coderabbitai bot commented Sep 4, 2024

Warning

Rate limit exceeded

@ggreer has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 6 minutes and 11 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 4cc68b3 and 4269249.

Walkthrough

The changes involve modifications to error handling and response structures across multiple files. Key updates include the introduction of structured response variables in the ListResources, ListEntitlements, and ListGrants methods, enhanced error handling in synchronization functions to include annotations, and improved granularity in HTTP response handling for specific server errors, particularly regarding rate limit details. Additionally, the make method in the Transport struct has been simplified by removing an unused context parameter.

Changes

File Change Summary
pkg/connectorbuilder/... Introduced structured response variables in ListResources, ListEntitlements, and ListGrants methods, ensuring consistent response handling even in error scenarios.
pkg/sync/... Updated synchronization functions to return annotations alongside errors, enhancing error handling and retry logic. Adjusted method signatures accordingly.
pkg/uhttp/... Enhanced error handling in the Do method to distinguish between client-side and server-side errors based on specific HTTP status codes, including handling of rate limit details. Modified the make method to remove the unused ctx parameter, simplifying the method signature.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Syncer
    participant Annotations
    participant HttpClient

    Client->>Syncer: SyncResources()
    Syncer->>Annotations: GetAnnotations()
    Syncer->>HttpClient: DoRequest()
    HttpClient-->>Syncer: Response
    Syncer->>Annotations: ProcessResponse()
    Syncer-->>Client: Return Annotations and Error
Loading

🐰 "In the meadow, changes bloom,
With structured responses, we make room.
Annotations dance, errors refined,
In sync and HTTP, clarity we find.
Hooray for updates, let joy take flight,
A hop, a skip, all feels just right!" 🐇✨


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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 74d2500 and c8f3db9.

Files selected for processing (3)
  • pkg/connectorbuilder/connectorbuilder.go (3 hunks)
  • pkg/sync/syncer.go (34 hunks)
  • pkg/uhttp/wrapper.go (1 hunks)
Additional comments not posted (14)
pkg/uhttp/wrapper.go (1)

177-179: LGTM!

The new conditional check for handling server errors (status codes 500-599) is a good addition. It improves the error handling by:

  • Distinguishing between client-side and server-side errors.
  • Returning a specific error code (codes.Unavailable) for server errors, enhancing the granularity of error reporting.
  • Including the actual HTTP response status in the error message, providing more context about the error.

The code changes are approved.

pkg/connectorbuilder/connectorbuilder.go (3)

351-366: LGTM!

The code changes introduce a new response variable resp that is constructed as a pointer to v2.ResourcesServiceListResourcesResponse struct. This ensures that a structured response is returned, even in the event of an error, providing additional context to the caller. The error messages remain largely unchanged, but they now accompany the structured response.

The final return statement has been simplified to return the resp variable along with a nil error, replacing the previous inline construction of the response struct. This enhances code readability and maintainability.


383-398: LGTM!

The code changes in ListEntitlements follow the same pattern as ListResources. A new response variable resp is introduced, which is constructed as a pointer to v2.EntitlementsServiceListEntitlementsResponse struct. This ensures that a structured response is returned, even in the event of an error, providing additional context to the caller. The error messages remain largely unchanged, but they now accompany the structured response.

The final return statement has been simplified to return the resp variable along with a nil error, replacing the previous inline construction of the response struct. This enhances code readability and maintainability.


416-433: LGTM!

The code changes in ListGrants follow the same pattern as ListResources and ListEntitlements. A new response variable resp is introduced, which is constructed as a pointer to v2.GrantsServiceListGrantsResponse struct. This ensures that a structured response is returned, even in the event of an error, providing additional context to the caller. The error messages remain largely unchanged, but they now accompany the structured response.

The final return statement has been simplified to return the resp variable along with a nil error, replacing the previous inline construction of the response struct. This enhances code readability and maintainability.

pkg/sync/syncer.go (10)

Line range hint 87-120: LGTM!

The code changes in shouldWaitAndRetry look good:

  • The function correctly handles the new annos parameter.
  • It extracts the rate limit data from annotations to determine the wait duration.
  • It falls back to the default linear backoff if annotations are missing.

The changes are approved.


Line range hint 125-268: LGTM!

The code changes in Sync look good:

  • The function correctly handles the new return types from the synchronization functions.
  • It captures the returned annotations and passes them to shouldWaitAndRetry for informed retry decisions.

The changes are approved.


Line range hint 280-315: LGTM!

The code changes in SyncResourceTypes look good:

  • The function correctly returns the annotations from the connector response.
  • It handles the case when the connector returns an error and returns the annotations along with the error.

The changes are approved.


Line range hint 343-373: LGTM!

The code changes in SyncResources look good:

  • The function correctly returns the annotations from the syncResources function or the store's ListResourceTypes method.
  • It handles the case when an error occurs and returns the annotations along with the error.

The changes are approved.


Line range hint 377-442: LGTM!

The code changes in syncResources look good:

  • The function correctly returns the annotations from the connector response.
  • It handles the case when the connector returns an error and returns the annotations along with the error.

The changes are approved.


Line range hint 509-555: LGTM!

The code changes in SyncEntitlements look good:

  • The function correctly returns the annotations from the store's ListResources method when fetching the resources.
  • It returns nil annotations when syncing entitlements for a specific resource.

The changes are approved.


Line range hint 701-741: LGTM!

The code changes in SyncAssets look good:

  • The function correctly returns the annotations from the store's ListResources method when fetching the resources.
  • It returns nil annotations when syncing assets for a specific resource.

The changes are approved.


Line range hint 746-859: LGTM!

The code changes in SyncGrantExpansion look good:

  • The function correctly returns the annotations from the store's ListGrants method when fetching the grants.
  • It returns nil annotations when expanding grants for entitlements.

The changes are approved.


Line range hint 864-910: LGTM!

The code changes in SyncGrants look good:

  • The function correctly returns the annotations from the store's ListResources method when fetching the resources.
  • It returns the annotations from syncGrantsForResource when syncing grants for a specific resource.

The changes are approved.


Line range hint 1034-1126: LGTM!

The code changes in syncGrantsForResource look good:

  • The function correctly returns the annotations from the store's GetResource method when fetching the resource.
  • It returns the annotations from the connector's ListGrants method when listing the grants.
  • It handles the case when an error occurs and returns the annotations along with the error.

The changes are approved.

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c8f3db9 and bf04c68.

Files selected for processing (1)
  • pkg/uhttp/wrapper.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pkg/uhttp/wrapper.go

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bf04c68 and b2ef106.

Files selected for processing (1)
  • pkg/uhttp/wrapper.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pkg/uhttp/wrapper.go

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

Outside diff range, codebase verification and nitpick comments (5)
pkg/sync/syncer.go (5)

Line range hint 327-362: Refactor SyncResourceTypes to Handle Pagination and Errors More Effectively

The SyncResourceTypes function has been refactored to handle pagination and errors effectively. Here are some points for improvement:

  1. Error Handling: The function now returns annotations along with errors, which is great for detailed error context. Ensure that all possible error paths are tested, especially with non-standard API responses.
  2. Pagination Logic: The handling of pagination through NextPageToken is crucial for syncing large datasets. Ensure that this logic is robust and handles edge cases, such as API changes or unexpected response formats.
  3. Logging and Monitoring: Consider adding more detailed logging at each step of the pagination and error handling. This can help in quick diagnosis during issues in production environments.

Consider adding unit tests that mock various API response scenarios to ensure that the pagination and error handling work as expected under different conditions.


Line range hint 390-489: Optimize SyncResources for Performance and Clarity

The SyncResources function is critical for syncing resource data. While the updates are in line with the PR's objectives, here are some suggestions for further refinement:

  1. Performance: The function handles pagination and recursive resource fetching, which can be performance-intensive. Consider optimizing the data fetching and processing patterns, possibly by batching requests or parallelizing independent operations.
  2. Clarity and Maintenance: The function's logic is somewhat complex due to the recursive nature and multiple conditional branches. Refactoring for clarity, possibly by breaking down the function into smaller, more focused sub-functions, could improve maintainability.
  3. Error Handling: Enhanced error handling with detailed annotations helps in understanding the context of failures. Ensure comprehensive coverage of error scenarios in unit tests to prevent runtime issues.

Overall, the function's implementation aligns with the intended enhancements in error handling and rate limit respect. Further optimizations could help in maintaining high performance and reliability.


Line range hint 556-602: Improve SyncEntitlements with Clear Error Handling and Efficient Pagination

The SyncEntitlements function has been updated to handle entitlements syncing with detailed annotations and error handling. Here are some areas for improvement:

  1. Error Handling: The function now returns detailed error information along with annotations, which is excellent for debugging and error tracking. Ensure that all error paths are thoroughly tested.
  2. Pagination Handling: The function deals with pagination, which is essential for handling large datasets. Ensure that the pagination logic is robust and handles all edge cases, such as incomplete pages or API changes.
  3. Code Clarity: Given the complexity introduced by error handling and pagination, consider refactoring the function to improve readability and maintainability. Breaking down complex logic into smaller, well-named functions could help.

Adding more detailed logging and monitoring at critical steps can also help in quicker issue resolution during production incidents.


Line range hint 748-788: Refactor SyncAssets for Better Error Handling and Clarity

The SyncAssets function has been updated to handle asset synchronization with enhanced error handling and annotations. Here are some suggestions for improvement:

  1. Error Handling: The function's approach to handling errors and returning annotations is a step in the right direction for better error context. Ensure that all potential error scenarios are covered in tests.
  2. Clarity and Maintenance: The function's logic around pagination and error handling could be clearer. Consider refactoring for better readability and maintainability, possibly by extracting some logic into helper functions.
  3. Performance Considerations: Given the potentially large number of assets to sync, consider performance optimizations such as request batching or parallel processing.

Overall, the updates are aligned with the PR's objectives, and further refinements could enhance the function's effectiveness and reliability.


Line range hint 793-906: Enhance SyncGrantExpansion with Robust Error Handling and Logical Flow

The SyncGrantExpansion function has been significantly updated to handle grant expansion with detailed annotations and robust error handling. Here are some points for further enhancement:

  1. Error Handling: The detailed error handling and use of annotations provide excellent visibility into the function's operation. Ensure that all error paths are well-tested, especially in scenarios where annotations might be missing or incomplete.
  2. Logical Flow: The function handles complex logic related to grant expansion, including handling cycles and conditional processing based on the state of the entitlement graph. Consider simplifying or documenting this logic to improve understandability and maintainability.
  3. Performance Optimization: Given the potentially intensive operations involved in grant expansion, consider optimizations such as caching frequently accessed data or parallelizing independent operations.

Refactoring for clarity and adding comprehensive unit tests can help ensure that the function performs as expected under various scenarios.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b2ef106 and d2cb3e0.

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

Line range hint 200-262: Enhanced Error Handling and Retry Logic in Sync Operations

The synchronization functions such as SyncResourceTypes, SyncResources, etc., have been updated to return both annotations and errors. This change is crucial for implementing the retry logic based on rate limit information provided in the annotations.

  1. Unified Error Handling: It's good to see a consistent approach to handling retries across different sync operations. This makes the code easier to maintain and understand.
  2. Annotations Usage: The use of annotations to decide on retries is a significant improvement. It allows the system to be more responsive to backend server states and rate limiting, which can help prevent overloading the server and improve sync reliability.
  3. Potential Refinement: While the current implementation covers the basic needs, consider abstracting some of the repetitive code into a helper function or middleware that handles retries. This could reduce code duplication and centralize the retry logic.

Overall, these changes are well-aligned with the PR's goals and should contribute to a more robust synchronization process.

Comment on lines 90 to 113
func shouldWaitAndRetry(ctx context.Context, annos annotations.Annotations, err error) bool {
if err == nil {
attempts = 0
return true
}
if status.Code(err) != codes.Unavailable {
if status.Code(err) != codes.Unavailable && status.Code(err) != codes.DeadlineExceeded {
return false
}

attempts++
l := ctxzap.Extract(ctx)

// use lineal time by default
var wait time.Duration = time.Duration(attempts) * time.Second
rlData := &v2.RateLimitDescription{}
if annos != nil {
ok, err := annos.Pick(rlData)
if ok && err == nil {
// or use time provided in the annotations
wait = time.Until(rlData.ResetAt.AsTime())
}
}

l.Error("retrying operation", zap.Error(err), zap.Duration("wait", wait))
l.Error("RETRYING OPERATION", zap.Error(err), zap.Duration("wait", wait), zap.Any("rate_limit", rlData))
Copy link

Choose a reason for hiding this comment

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

Refactor suggestion for shouldWaitAndRetry function

The shouldWaitAndRetry function has been updated to include rate limit handling using annotations. Here are a few suggestions and observations:

  1. Error Handling: The function now handles Unavailable and DeadlineExceeded errors specifically, which is a good practice for retry mechanisms. However, it might be beneficial to expand this to other transient errors that could also benefit from retries.
  2. Rate Limit Handling: The use of rate limit data from annotations is a robust way to respect server-specified backoff times. Ensure that the RateLimitDescription is being parsed correctly and that the ResetAt field is always present when expected.
  3. Logging: The error logging before entering the retry loop is helpful for debugging. Consider adding more detailed logs that could help trace the values of variables more precisely, especially around the rate limit data.
  4. Infinite Loop Risk: The function uses a for loop with a select statement for retrying, which is generally safe. However, ensure there's a mechanism to break out of the loop in case of unexpected conditions to prevent potential infinite loops.

Overall, the changes are aligned with the PR's objectives to handle rate limits more effectively. Consider adding more comprehensive tests to cover various scenarios of rate limit headers and error types.

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d2cb3e0 and a12cd1d.

Files selected for processing (1)
  • pkg/uhttp/wrapper.go (5 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pkg/uhttp/wrapper.go

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a12cd1d and 3519a9c.

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

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

Outside diff range, codebase verification and nitpick comments (2)
pkg/sync/syncer.go (2)

Line range hint 250-262: Enhanced error handling in SyncAssets and SyncGrantExpansion

The modifications in the SyncAssets and SyncGrantExpansion functions include improved error handling by integrating annotations into the retry logic. This is a significant improvement as it allows these functions to provide additional context about the operation's outcome, which can be leveraged in the retry logic.

  • SyncAssets: The function now correctly handles errors by retrying based on the annotations provided. This is crucial for operations that may be rate-limited or face temporary issues.
  • SyncGrantExpansion: Similarly, this function uses the enhanced retry logic to handle errors more effectively. The detailed debug logs added in this function will aid in troubleshooting and ensuring that the grant expansion process is robust.

These changes are in line with the PR's objectives to improve the clarity of the code and potentially increase the reliability of the synchronization operations.


Line range hint 950-1174: Review of syncGrantsForResource function

The syncGrantsForResource function has undergone significant changes to include annotations in its return type and to enhance error handling. Here are some specific observations:

  1. Annotations Handling: The function now returns annotations along with the error, which is a good practice as it provides more context about the error or the state of the operation.
  2. Error Handling: The function's error handling has been improved by checking for specific conditions and returning detailed errors. This makes the function more robust and easier to debug.
  3. Integration with ETag Logic: The integration of ETag logic to handle conditional requests based on previous sync states is a sophisticated feature that can reduce unnecessary data transfer and processing. This is particularly useful in environments where data changes infrequently.
  4. Potential Improvements:
    • Error Handling Consistency: Ensure that all potential error paths are handled consistently and that errors are logged with sufficient detail.
    • Testing: Given the complexity of the function, especially with the integration of ETag logic, comprehensive testing is crucial to ensure that all edge cases are covered.

Overall, the changes enhance the function's capability to handle synchronization tasks more effectively, aligning with the PR's goals.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3519a9c and 44e3951.

Files selected for processing (1)
  • pkg/sync/syncer.go (10 hunks)

Comment on lines 95 to 116
if status.Code(err) != codes.Unavailable && status.Code(err) != codes.DeadlineExceeded {
return false
}

attempts++
l := ctxzap.Extract(ctx)

// use lineal time by default
var wait time.Duration = time.Duration(attempts) * time.Second
if st, ok := status.FromError(err); ok {
details := st.Details()
for _, detail := range details {
if rlData, ok := detail.(*v2.RateLimitDescription); ok {
wait = time.Until(rlData.ResetAt.AsTime())
}
}
l.Debug("details from status error", zap.Any("details", details), zap.Int("len of details", len(details)), zap.Error(err))
} else {
l.Debug("unable to parse rate limit description from error", zap.Error(err), zap.Any("status", st))
}

l.Error("retrying operation", zap.Error(err), zap.Duration("wait", wait))
l.Error("RETRYING OPERATION", zap.Error(err), zap.Duration("wait", wait))
Copy link

Choose a reason for hiding this comment

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

Refactor suggestion for shouldWaitAndRetry function

The shouldWaitAndRetry function has been updated to include rate limit handling using annotations. Here are a few suggestions and observations:

  1. Error Handling: The function now handles Unavailable and DeadlineExceeded errors specifically, which is a good practice for retry mechanisms. However, it might be beneficial to expand this to other transient errors that could also benefit from retries.
  2. Rate Limit Handling: The use of rate limit data from annotations is a robust way to respect server-specified backoff times. Ensure that the RateLimitDescription is being parsed correctly and that the ResetAt field is always present when expected.
  3. Logging: The error logging before entering the retry loop is helpful for debugging. Consider adding more detailed logs that could help trace the values of variables more precisely, especially around the rate limit data.
  4. Infinite Loop Risk: The function uses a for loop with a select statement for retrying, which is generally safe. However, ensure there's a mechanism to break out of the loop in case of unexpected conditions to prevent potential infinite loops.

Overall, the changes are aligned with the PR's objectives to handle rate limits more effectively. Consider adding more comprehensive tests to cover various scenarios of rate limit headers and error types.

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 44e3951 and a588805.

Files selected for processing (2)
  • pkg/uhttp/transport.go (1 hunks)
  • pkg/uhttp/wrapper.go (3 hunks)
Files skipped from review due to trivial changes (1)
  • pkg/uhttp/transport.go
Files skipped from review as they are similar to previous changes (1)
  • pkg/uhttp/wrapper.go

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

Outside diff range and nitpick comments (3)
pkg/sync/syncer.go (3)

95-117: Approved with suggestions

The changes to the shouldWaitAndRetry function look good! Parsing the rate limit data from the error details to determine the retry wait duration is a nice enhancement.

Suggestions:

  1. Consider providing a more detailed error message when unable to parse the rate limit description from the error. This will aid in debugging such scenarios.
  2. Add unit tests to cover various scenarios of rate limit headers and error types to ensure the robustness of the retry logic.

1084-1089: Approved with suggestion

The new SyncGrantExpansion function looks good! It aligns with the PR objective of enhancing the grant expansion process.

Suggestion:

  1. Add documentation to explain the purpose and behavior of this function. This will help future maintainers understand its role in the grant expansion process.

Line range hint 1104-1176: Approved with suggestion

The changes to the syncGrantsForResource function look good! Fetching and processing etagged grants before fetching new grants, and updating the resource etag based on the etag match or response etag, are nice enhancements to the grant fetching process.

Suggestion:

  1. Consider adding more detailed logging statements to help trace the values of variables more precisely, especially around the etag handling logic. This will aid in debugging and understanding the flow of the function.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a588805 and b2318d0.

Files selected for processing (2)
  • pkg/sync/syncer.go (10 hunks)
  • pkg/uhttp/wrapper.go (3 hunks)
Additional context used
golangci-lint
pkg/uhttp/wrapper.go

362-362: ineffectual assignment to err

(ineffassign)

GitHub Check: go-lint
pkg/uhttp/wrapper.go

[failure] 362-362:
ineffectual assignment to err (ineffassign)

Additional comments not posted (5)
pkg/uhttp/wrapper.go (2)

217-229: LGTM!

The new wrapRetryAfterInStatus function looks good. It correctly uses the ratelimit.ExtractRateLimitData function to handle different rate limit headers, addressing the previous comment.


286-366: Great job on enhancing the error handling!

The changes to the Do function make the error handling more robust by providing clearer feedback on rate limiting issues. This addresses the previous comment to make the code as general as possible.

Tools
golangci-lint

362-362: ineffectual assignment to err

(ineffassign)

GitHub Check: go-lint

[failure] 362-362:
ineffectual assignment to err (ineffassign)

pkg/sync/syncer.go (3)

252-252: Approved

The changes to the SyncResourceTypes function look good! Returning annotations along with the error allows for providing additional context about the operation's outcome, which is a nice improvement.


264-264: Approved

The changes to the SyncAssets function look good! Returning annotations alongside errors allows for providing additional context about the operation's outcome, which is a nice improvement.


1343-1343: Approved

The new newExpandedGrant function looks good! It aligns with the PR objective of enhancing the grant expansion process by creating a new grant with the provided entitlement and principal.

l.Debug("error setting cache", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String()), zap.Error(err))
return resp, err
l.Warn("error setting cache", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String()), zap.Error(err))
err = nil
Copy link

Choose a reason for hiding this comment

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

Fix the ineffectual assignment to err.

The static analysis tools have correctly flagged an ineffectual assignment to err at line 362.

To fix this, remove the assignment to err:

-      err = nil
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
err = nil
Tools
golangci-lint

362-362: ineffectual assignment to err

(ineffassign)

GitHub Check: go-lint

[failure] 362-362:
ineffectual assignment to err (ineffassign)

@ggreer ggreer changed the title WIP: Rate limits Rate limit support Sep 11, 2024
@ggreer ggreer merged commit d3a21e0 into main Sep 12, 2024
@ggreer ggreer deleted the ggreer/rate-limits branch September 12, 2024 00:14
@coderabbitai coderabbitai bot mentioned this pull request Nov 22, 2024
@coderabbitai coderabbitai bot mentioned this pull request Oct 22, 2025
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