Skip to content

feat(client): dynamic retry for "Client Retry Plugin"#318

Merged
dinwwwh merged 1 commit intomainfrom
feat/client/ClientRetryPlugin-dynamic-retry-support
Apr 1, 2025
Merged

feat(client): dynamic retry for "Client Retry Plugin"#318
dinwwwh merged 1 commit intomainfrom
feat/client/ClientRetryPlugin-dynamic-retry-support

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Apr 1, 2025

Summary by CodeRabbit

  • New Features
    • Implemented a dynamic retry mechanism that automatically adjusts the number of retry attempts based on request context, enhancing overall resilience.
  • Documentation
    • Updated guidelines to clearly explain the enhanced retry behavior for improved client operations.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 1, 2025 2:08am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 1, 2025

Walkthrough

This pull request updates the retry mechanism in the ClientRetryPlugin. A new retry function is introduced to define retry attempts based on the request path, returning 2 for 'planet.list' and 0 otherwise. The plugin’s type definition is modified to allow dynamic evaluation of retry attempts. Corresponding tests have been updated to use a mock function for verifying call count and parameters, and related documentation comments were clarified.

Changes

File(s) Change Summary
apps/.../client-retry.md Updated documentation to include the new retry function that returns 2 for 'planet.list' and 0 for other paths, with clarifying comments on override behavior.
packages/.../retry.ts, packages/.../retry.test.ts Modified the ClientRetryPlugin: changed the retry type to a dynamic evaluable function and updated the init method to compute maxAttempts asynchronously. Tests now use a mock retry function to verify parameter passing and call count.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant P as ClientRetryPlugin
    participant R as Retry Function
    C->>P: Send request (with context, path, input)
    P->>R: Evaluate retry(context, path, input)
    R-->>P: Return retry count (2 for 'planet.list', 0 otherwise)
    P->>C: Set maxAttempts & process request accordingly
Loading

Possibly related PRs

  • unnoq/orpc#291: Involves modifications to the ClientRetryPlugin signature and context handling, directly relating to the new retry function.
  • unnoq/orpc#267: Removes the default event iterator retry behavior, interlinking with the updated retry logic introduced here.
  • unnoq/orpc#265: Updates retry logic in tests, similarly involving the retry function within the ClientRetryPlugin.

Poem

I'm a rabbit with a code delight,
Hopping through changes in the moonlit night.
A new retry function hops into view,
With two quick leaps when the call is due.
Tests and docs join in the cheerful tune,
A bunny celebration under a carrot moon! 🐰
Happy hops to a bug-free afternoon!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 1, 2025

More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@318

@orpc/client

npm i https://pkg.pr.new/@orpc/client@318

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@318

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@318

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@318

@orpc/react

npm i https://pkg.pr.new/@orpc/react@318

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@318

@orpc/server

npm i https://pkg.pr.new/@orpc/server@318

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@318

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@318

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@318

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@318

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@318

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@318

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@318

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@318

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@318

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@318

commit: da2b10a

Copy link
Copy Markdown

@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

🧹 Nitpick comments (2)
apps/content/docs/plugins/client-retry.md (1)

28-36: Great implementation of dynamic retry behavior based on API path!

This is a well-implemented example showing how to configure different retry behaviors for specific endpoints. The function checks if the path matches 'planet.list' and returns 2 retry attempts for that path, or 0 for all other paths.

Consider using constants for the retry counts and path values to improve maintainability:

 default: { // Optional override for default options
   retry: (options, path) => {
+    const PLANET_LIST_PATH = 'planet.list';
+    const PLANET_LIST_RETRIES = 2;
+    const DEFAULT_RETRIES = 0;
-    if (path.join('.') === 'planet.list') {
-      return 2
+    if (path.join('.') === PLANET_LIST_PATH) {
+      return PLANET_LIST_RETRIES
     }

-    return 0
+    return DEFAULT_RETRIES
   }
 },
packages/client/src/plugins/retry.ts (1)

85-90: Correctly implemented dynamic retry value resolution!

The implementation properly handles evaluating the retry value, whether it's a static number or a function. By using the value helper, you ensure consistent behavior regardless of the retry value type.

Consider adding error handling to prevent unexpected failures if the retry function throws an error:

 const maxAttempts = await value(
   interceptorOptions.options.context.retry ?? this.defaultRetry,
   interceptorOptions.options,
   interceptorOptions.path,
   interceptorOptions.input,
+).catch(err => {
+  console.error('Error determining retry count:', err);
+  return 0; // Default to no retries if there's an error
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0335922 and da2b10a.

📒 Files selected for processing (3)
  • apps/content/docs/plugins/client-retry.md (1 hunks)
  • packages/client/src/plugins/retry.test.ts (1 hunks)
  • packages/client/src/plugins/retry.ts (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/client/src/plugins/retry.ts (2)
packages/client/src/types.ts (1)
  • ClientOptions (30-32)
packages/client/src/adapters/standard/link.ts (1)
  • path (44-56)
🔇 Additional comments (2)
packages/client/src/plugins/retry.test.ts (1)

55-61: Good test implementation for the dynamic retry function!

You've properly updated the test to validate the new dynamic retry behavior:

  1. Using a mock function instead of a static value
  2. Verifying the function is called exactly once with the correct parameters

This improves test coverage by ensuring not just that retries happen, but that the retry function is invoked with the expected arguments.

packages/client/src/plugins/retry.ts (1)

21-25: Well-designed type enhancement for dynamic retry behavior!

The update to the retry property's type signature properly expands its capabilities, allowing it to be:

  1. A static number (backward compatible)
  2. A function that can dynamically determine the retry count based on client options, path, and input

This design gives developers greater flexibility to implement contextual retry logic.

@dinwwwh dinwwwh merged commit 05a8f88 into main Apr 1, 2025
8 checks passed
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.

1 participant