Skip to content

Commit 05a8f88

Browse files
authored
feat(client): dynamic retry for "Client Retry Plugin" (#318)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## 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. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 0335922 commit 05a8f88

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

apps/content/docs/plugins/client-retry.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ const link = new RPCLink<ORPCClientContext>({
2525
url: 'http://localhost:3000/rpc',
2626
plugins: [
2727
new ClientRetryPlugin({
28-
default: {}, // Override the default context if needed
28+
default: { // Optional override for default options
29+
retry: (options, path) => {
30+
if (path.join('.') === 'planet.list') {
31+
return 2
32+
}
33+
34+
return 0
35+
}
36+
},
2937
}),
3038
],
3139
})

packages/client/src/plugins/retry.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ describe('clientRetryPlugin', () => {
5252
it('should retry', async () => {
5353
handlerFn.mockRejectedValue(new Error('fail'))
5454

55-
await expect(client('hello', { context: { retry: 3, retryDelay: 0 } })).rejects.toThrow('Internal server error')
55+
const retry = vi.fn(() => 3)
56+
57+
await expect(client('hello', { context: { retry, retryDelay: 0 } })).rejects.toThrow('Internal server error')
5658

5759
expect(handlerFn).toHaveBeenCalledTimes(4)
60+
expect(retry).toHaveBeenCalledTimes(1)
61+
expect(retry).toHaveBeenCalledWith({ context: { retry, retryDelay: 0 } }, [], 'hello')
5862
})
5963

6064
it('should not retry if success', async () => {

packages/client/src/plugins/retry.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export interface ClientRetryPluginContext {
1818
*
1919
* @default 0
2020
*/
21-
retry?: number
21+
retry?: Value<number, [
22+
clientOptions: ClientOptions<ClientRetryPluginContext>,
23+
path: readonly string[],
24+
input: unknown,
25+
]>
2226

2327
/**
2428
* Delay (in ms) before retrying.
@@ -78,7 +82,13 @@ export class ClientRetryPlugin<T extends ClientRetryPluginContext> implements St
7882
options.interceptors ??= []
7983

8084
options.interceptors.push(async (interceptorOptions) => {
81-
const maxAttempts = interceptorOptions.options.context.retry ?? this.defaultRetry
85+
const maxAttempts = await value(
86+
interceptorOptions.options.context.retry ?? this.defaultRetry,
87+
interceptorOptions.options,
88+
interceptorOptions.path,
89+
interceptorOptions.input,
90+
)
91+
8292
const retryDelay = interceptorOptions.options.context.retryDelay ?? this.defaultRetryDelay
8393
const shouldRetry = interceptorOptions.options.context.shouldRetry ?? this.defaultShouldRetry
8494
const onRetry = interceptorOptions.options.context.onRetry ?? this.defaultOnRetry

0 commit comments

Comments
 (0)