Registry Mirror
Skip to content

File tree

‎apps/content/docs/adapters/websocket.mdx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,23 @@ const link = new RPCLink({
287287

288288
/**
289289
* Maximum number of consecutive failed attempts before giving up.
290+
* It counts every attempt of a (re)connect cycle, the first one included (see `attempt`),
291+
* so it should be at least 1.
290292
* When reached, calls waiting for the connection fail, and the next
291293
* call starts a new reconnect cycle.
292-
* Should greater than 1
293294
*
294295
* @default Infinity
295296
*/
296-
maxAttempt: Infinity,
297+
maxAttempts: Infinity,
297298

298299
/**
299300
* Maximum number of connection attempts over the transport's lifetime,
300-
* successful ones included (see `totalAttempt`).
301+
* successful ones included (see `totalAttempts`).
301302
* When reached, calls that need a new connection fail instead of connecting.
302303
*
303304
* @default Infinity
304305
*/
305-
maxTotalAttempt: Infinity,
306+
maxTotalAttempts: Infinity,
306307

307308
/**
308309
* Whether to proactively reconnect right after the socket closes,
@@ -325,5 +326,5 @@ const link = new RPCLink({
325326
```
326327

327328
:::info
328-
A connection proves healthy once it receives a message from the server or stays open for 5 seconds. Only then does `attempt` reset to 1. A server that closes connections right after the handshake, for example to reject authentication, gets the `delay` backoff and the `maxAttempt` limit instead of an immediate reconnect loop.
329+
A connection proves healthy once it receives a message from the server or stays open for 5 seconds. Only then does `attempt` reset to 1. A server that closes connections right after the handshake, for example to reject authentication, gets the `delay` backoff and the `maxAttempts` limit instead of an immediate reconnect loop.
329330
:::

‎apps/content/docs/migrations/from-v1.mdx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,26 @@ const retryPlugin = new RetryLinkPlugin({
979979
})
980980
```
981981

982+
### Retry After Plugin
983+
984+
The `condition` option of [`RetryAfterLinkPlugin`](/docs/plugins/retry-after) was renamed to `shouldRetry`, matching the Retry Plugin:
985+
986+
<CodeGroup>
987+
988+
```ts v2
989+
const retryAfterPlugin = new RetryAfterLinkPlugin({
990+
shouldRetry: (response) => response.status === 429,
991+
})
992+
```
993+
994+
```ts v1
995+
const retryAfterPlugin = new RetryAfterPlugin({
996+
condition: (response) => response.status === 429,
997+
})
998+
```
999+
1000+
</CodeGroup>
1001+
9821002
## Contract-First
9831003

9841004
Contract types and utilities changed word order from `ContractRouter*` to `RouterContract*`. Deprecated aliases exist for all of them. See [Procedure Contract](/docs/contract/procedure) and [Router Contract](/docs/contract/router).

‎apps/content/docs/plugins/retry-after.mdx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ Requests whose body is a [`ReadableStream`](/docs/binary-data#readablestreamuint
2525

2626
## Options
2727

28-
By default, the plugin retries only requests that receive a `429` (Too Many Requests) or `503` (Service Unavailable) status code. It times out after 5 minutes and allows up to 3 retry attempts. You can customize this behavior with the following options:
28+
By default, the plugin retries only requests that receive a `429` (Too Many Requests) or `503` (Service Unavailable) status code. It allows up to 3 attempts in total, the first request included, and stops retrying once the total wait would exceed 5 minutes. You can customize this behavior with the following options:
2929

3030
```ts
3131
const link = new RPCLink({
3232
plugins: [
3333
new RetryAfterLinkPlugin({
34-
condition: (response) => [429, 503].includes(response.status),
34+
shouldRetry: (response) => [429, 503].includes(response.status),
3535
timeout: 5 * 60 * 1000, // 5 minutes
36-
maxAttempts: 3,
36+
maxAttempts: 3, // Total attempts, the first request included, so up to 2 retries
3737
}),
3838
],
3939
})

‎apps/content/docs/plugins/retry.mdx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const planets = await client.planet.list(
4040
{ limit: 10 },
4141
{
4242
context: {
43-
retry: 3, // Maximum retry attempts
43+
retry: 3, // Retries after the first call, so up to 4 attempts in total
4444
retryDelay: 2000, // Delay between retries in ms
4545
shouldRetry: (options) => true, // Determines whether to retry based on the error
4646
onRetry: (options) => {
@@ -58,8 +58,8 @@ const planets = await client.planet.list(
5858
:::info
5959
The following context options control retry behavior:
6060

61-
- **retry:** Maximum number of retry attempts before throwing an error _(default: `0`)_.
62-
- **retryDelay:** Delay between retry attempts _(default: `(o) => o.lastEventRetry ?? 2000`)_.
61+
- **retry:** Maximum number of retries after the first call before throwing an error. The first call is not counted, so `3` allows up to 4 attempts in total _(default: `0`)_.
62+
- **retryDelay:** Delay before each retry, in milliseconds _(default: `(o) => o.lastEventRetry ?? 2000`)_.
6363
- **shouldRetry:** Function that determines whether a retry should be attempted _(default: `(o) => isRetryableError(o.error)`, see [Client Error Responses](#client-error-responses))_.
6464

6565
You can override the default retry behavior globally by passing `default` options when initializing the plugin:

‎packages/client/src/adapters/websocket/rpc-link.test.ts‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('rpcLink', () => {
160160
const orpc = createORPCClient(new RPCLink({ connect, connectOnInit: true })) as any
161161

162162
await vi.waitFor(() => expect(connect).toHaveBeenCalledTimes(1))
163-
expect(connect).toHaveBeenCalledWith({ totalAttempt: 1, attempt: 1 })
163+
expect(connect).toHaveBeenCalledWith({ totalAttempts: 1, attempt: 1 })
164164

165165
const promise = orpc.ping('input')
166166

@@ -188,7 +188,7 @@ describe('rpcLink', () => {
188188
const orpc = createORPCClient(new RPCLink({ connect, connectOnInit: true })) as any
189189

190190
await vi.waitFor(() => expect(connect).toHaveBeenCalledTimes(1))
191-
expect(connect).toHaveBeenCalledWith({ totalAttempt: 1, attempt: 1 })
191+
expect(connect).toHaveBeenCalledWith({ totalAttempts: 1, attempt: 1 })
192192
expect(unhandledRejectionHandler).toHaveBeenCalledTimes(0) // no background error
193193
})
194194

@@ -368,7 +368,7 @@ describe('rpcLink', () => {
368368
const promise = orpc.ping('input')
369369

370370
await vi.waitFor(() => expect(openSocket.send).toHaveBeenCalledTimes(1))
371-
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempt: 2, attempt: 2 })
371+
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempts: 2, attempt: 2 })
372372
expect(closedSocket.send).toHaveBeenCalledTimes(0)
373373
expect(closedSocket.addEventListener).toHaveBeenCalledTimes(0)
374374

@@ -386,15 +386,15 @@ describe('rpcLink', () => {
386386
connect,
387387
reconnect: {
388388
delay,
389-
maxAttempt: 1,
389+
maxAttempts: 1,
390390
},
391391
}),
392392
) as any
393393

394394
await expect(orpc.ping('input')).rejects.toThrow(
395395
'WebSocket reconnect failed after 1 attempt(s)',
396396
)
397-
expect(delay).toHaveBeenCalledWith({ totalAttempt: 1, attempt: 1 })
397+
expect(delay).toHaveBeenCalledWith({ totalAttempts: 1, attempt: 1 })
398398
expect(connect).toHaveBeenCalledTimes(1)
399399
})
400400

@@ -403,7 +403,7 @@ describe('rpcLink', () => {
403403
const orpc = createORPCClient(
404404
new RPCLink({
405405
connect,
406-
reconnect: { delay: () => 0, maxAttempt: 3 },
406+
reconnect: { delay: () => 0, maxAttempts: 3 },
407407
}),
408408
) as any
409409

@@ -441,7 +441,7 @@ describe('rpcLink', () => {
441441
const orpc = createORPCClient(
442442
new RPCLink({
443443
connect,
444-
reconnect: { delay: () => 0, maxAttempt: 2 },
444+
reconnect: { delay: () => 0, maxAttempts: 2 },
445445
}),
446446
) as any
447447

@@ -454,7 +454,7 @@ describe('rpcLink', () => {
454454
const secondCall = orpc.ping('second')
455455

456456
await vi.waitFor(() => expect(recoveredSocket.send).toHaveBeenCalledTimes(1))
457-
expect(connect).toHaveBeenNthCalledWith(3, { totalAttempt: 3, attempt: 1 })
457+
expect(connect).toHaveBeenNthCalledWith(3, { totalAttempts: 3, attempt: 1 })
458458

459459
const request = getSentRequest(recoveredSocket)
460460
await recoveredSocket.receive(
@@ -464,7 +464,7 @@ describe('rpcLink', () => {
464464
},
465465
)
466466

467-
it('stops connecting once maxTotalAttempt is reached, successful attempts included', async () => {
467+
it('stops connecting once maxTotalAttempts is reached, successful attempts included', async () => {
468468
const firstSocket = createWs()
469469
const connect = vi
470470
.fn()
@@ -473,7 +473,7 @@ describe('rpcLink', () => {
473473
const orpc = createORPCClient(
474474
new RPCLink({
475475
connect,
476-
reconnect: { delay: () => 0, maxTotalAttempt: 3 },
476+
reconnect: { delay: () => 0, maxTotalAttempts: 3 },
477477
}),
478478
) as any
479479

@@ -520,13 +520,13 @@ describe('rpcLink', () => {
520520
const promise = orpc.ping('input')
521521

522522
await vi.advanceTimersByTimeAsync(0)
523-
expect(connect).toHaveBeenNthCalledWith(1, { totalAttempt: 1, attempt: 1 })
523+
expect(connect).toHaveBeenNthCalledWith(1, { totalAttempts: 1, attempt: 1 })
524524

525525
await vi.advanceTimersByTimeAsync(1_999)
526526
expect(connect).toHaveBeenCalledTimes(1)
527527

528528
await vi.advanceTimersByTimeAsync(1)
529-
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempt: 2, attempt: 2 })
529+
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempts: 2, attempt: 2 })
530530
expect(recoveredSocket.send).toHaveBeenCalledTimes(1)
531531

532532
const request = getSentRequest(recoveredSocket)
@@ -565,8 +565,8 @@ describe('rpcLink', () => {
565565
const secondCall = orpc.ping('second')
566566

567567
await vi.waitFor(() => expect(connect).toHaveBeenCalledTimes(2))
568-
expect(connect).toHaveBeenNthCalledWith(1, { totalAttempt: 1, attempt: 1 })
569-
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempt: 2, attempt: 1 })
568+
expect(connect).toHaveBeenNthCalledWith(1, { totalAttempts: 1, attempt: 1 })
569+
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempts: 2, attempt: 1 })
570570
await vi.waitFor(() => expect(secondSocket.send).toHaveBeenCalledTimes(1))
571571

572572
const secondRequest = getSentRequest(secondSocket)
@@ -635,7 +635,7 @@ describe('rpcLink', () => {
635635
new RPCLink({
636636
connect,
637637
reconnect: {
638-
maxAttempt: 1,
638+
maxAttempts: 1,
639639
onClose: { delay: 0 },
640640
},
641641
}),
@@ -673,7 +673,7 @@ describe('rpcLink', () => {
673673
new RPCLink({
674674
connect,
675675
connectOnInit: true,
676-
reconnect: { delay: () => 0, maxAttempt: 2, onClose: true },
676+
reconnect: { delay: () => 0, maxAttempts: 2, onClose: true },
677677
}),
678678
)
679679

@@ -685,8 +685,8 @@ describe('rpcLink', () => {
685685
await vi.advanceTimersByTimeAsync(10_000)
686686

687687
expect(connect).toHaveBeenCalledTimes(3)
688-
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempt: 2, attempt: 1 })
689-
expect(connect).toHaveBeenNthCalledWith(3, { totalAttempt: 3, attempt: 2 })
688+
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempts: 2, attempt: 1 })
689+
expect(connect).toHaveBeenNthCalledWith(3, { totalAttempts: 3, attempt: 2 })
690690
})
691691

692692
it('treats a healthy socket that is still closing as healthy', async ({ onTestFinished }) => {
@@ -711,13 +711,13 @@ describe('rpcLink', () => {
711711

712712
void orpc.ping('input').catch(() => {})
713713
await vi.advanceTimersByTimeAsync(0)
714-
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempt: 2, attempt: 1 })
714+
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempts: 2, attempt: 1 })
715715

716716
// the late close event must not reset the new cycle
717717
await firstSocket.close()
718718
await secondSocket.close()
719719
await vi.advanceTimersByTimeAsync(2_000)
720-
expect(connect).toHaveBeenNthCalledWith(3, { totalAttempt: 3, attempt: 2 })
720+
expect(connect).toHaveBeenNthCalledWith(3, { totalAttempts: 3, attempt: 2 })
721721
})
722722

723723
describe('connections that close right after opening', () => {
@@ -730,7 +730,7 @@ describe('rpcLink', () => {
730730
})
731731

732732
const createRejectingConnect = () =>
733-
vi.fn((_info: { totalAttempt: number; attempt: number }) => {
733+
vi.fn((_info: { totalAttempts: number; attempt: number }) => {
734734
const ws = createWs()
735735
setTimeout(() => ws.close({ code: 4001, reason: 'unauthorized' }))
736736
return ws
@@ -743,25 +743,25 @@ describe('rpcLink', () => {
743743

744744
await vi.advanceTimersByTimeAsync(1_000)
745745
expect(connect).toHaveBeenCalledTimes(1)
746-
expect(connect).toHaveBeenNthCalledWith(1, { totalAttempt: 1, attempt: 1 })
746+
expect(connect).toHaveBeenNthCalledWith(1, { totalAttempts: 1, attempt: 1 })
747747

748748
await vi.advanceTimersByTimeAsync(2_000)
749749
expect(connect).toHaveBeenCalledTimes(2)
750-
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempt: 2, attempt: 2 })
750+
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempts: 2, attempt: 2 })
751751

752752
await vi.advanceTimersByTimeAsync(2_000)
753753
expect(connect).toHaveBeenCalledTimes(3)
754-
expect(connect).toHaveBeenNthCalledWith(3, { totalAttempt: 3, attempt: 3 })
754+
expect(connect).toHaveBeenNthCalledWith(3, { totalAttempts: 3, attempt: 3 })
755755
})
756756

757-
it('count toward maxAttempt', async () => {
757+
it('count toward maxAttempts', async () => {
758758
const connect = createRejectingConnect()
759759

760760
createORPCClient(
761761
new RPCLink({
762762
connect,
763763
connectOnInit: true,
764-
reconnect: { delay: () => 0, maxAttempt: 3, onClose: true },
764+
reconnect: { delay: () => 0, maxAttempts: 3, onClose: true },
765765
}),
766766
)
767767

@@ -772,7 +772,7 @@ describe('rpcLink', () => {
772772
it('fail every waiting call with the last close reason when giving up', async () => {
773773
const connect = createRejectingConnect()
774774
const orpc = createORPCClient(
775-
new RPCLink({ connect, reconnect: { delay: () => 0, maxAttempt: 2 } }),
775+
new RPCLink({ connect, reconnect: { delay: () => 0, maxAttempts: 2 } }),
776776
) as any
777777

778778
const call = async (input: string) => {
@@ -804,7 +804,7 @@ describe('rpcLink', () => {
804804
await vi.advanceTimersByTimeAsync(2_000)
805805

806806
expect(connect).toHaveBeenCalledTimes(2)
807-
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempt: 2, attempt: 2 })
807+
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempts: 2, attempt: 2 })
808808
})
809809

810810
it('reset the attempt once the server sends a message', async () => {
@@ -828,7 +828,7 @@ describe('rpcLink', () => {
828828
void orpc.ping('second').catch(() => {})
829829
await vi.advanceTimersByTimeAsync(0)
830830

831-
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempt: 2, attempt: 1 })
831+
expect(connect).toHaveBeenNthCalledWith(2, { totalAttempts: 2, attempt: 1 })
832832
})
833833
})
834834

‎packages/client/src/adapters/websocket/transport.ts‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ export type WebSocketLike = Pick<
3535

3636
export interface WebSocketLinkTransportAttemptInfo {
3737
/**
38-
* Total number of connection attempts for this transport's lifetime.
38+
* Total number of connection attempts for this transport's lifetime, this one included.
3939
* Starts at 1 on the first attempt, increments on every subsequent
4040
* attempt, and never resets.
4141
*/
42-
totalAttempt: number
42+
totalAttempts: number
4343

4444
/**
4545
* Attempt number within the current (re)connect cycle.
@@ -65,22 +65,23 @@ export interface WebSocketLinkTransportReconnectOptions {
6565

6666
/**
6767
* Maximum number of consecutive failed attempts before giving up.
68+
* It counts every attempt of a (re)connect cycle, the first one included (see `attempt`),
69+
* so it should be at least 1.
6870
* When reached, calls waiting for the connection fail, and the next
6971
* call starts a new reconnect cycle.
70-
* Should greater than 1
7172
*
7273
* @default Infinity
7374
*/
74-
maxAttempt?: number | undefined
75+
maxAttempts?: number | undefined
7576

7677
/**
7778
* Maximum number of connection attempts over the transport's lifetime,
78-
* successful ones included (see `totalAttempt`).
79+
* successful ones included (see `totalAttempts`).
7980
* When reached, calls that need a new connection fail instead of connecting.
8081
*
8182
* @default Infinity
8283
*/
83-
maxTotalAttempt?: number | undefined
84+
maxTotalAttempts?: number | undefined
8485

8586
/**
8687
* Whether to proactively reconnect right after the socket closes,
@@ -165,8 +166,8 @@ export class WebSocketLinkTransport<T extends ClientContext> implements Standard
165166

166167
this.reconnectEnabled = options.reconnect !== undefined && options.reconnect !== false
167168
this.reconnectDelay = reconnect.delay ?? ((info) => (info.attempt === 1 ? 0 : 2_000))
168-
this.reconnectMaxAttempt = reconnect.maxAttempt ?? Infinity
169-
this.reconnectMaxTotalAttempt = reconnect.maxTotalAttempt ?? Infinity
169+
this.reconnectMaxAttempt = reconnect.maxAttempts ?? Infinity
170+
this.reconnectMaxTotalAttempt = reconnect.maxTotalAttempts ?? Infinity
170171
this.reconnectOnCloseEnabled =
171172
this.reconnectEnabled && reconnect.onClose !== undefined && reconnect.onClose !== false
172173
this.reconnectOnCloseDelay = onClose.delay ?? 0
@@ -224,7 +225,7 @@ export class WebSocketLinkTransport<T extends ClientContext> implements Standard
224225
this.attempt += 1
225226

226227
const info: WebSocketLinkTransportAttemptInfo = {
227-
totalAttempt: this.totalAttempt,
228+
totalAttempts: this.totalAttempt,
228229
attempt: this.attempt,
229230
}
230231

0 commit comments

Comments
 (0)