Repository navigation
Commit f293ef4
authored
fix(client): reset websocket reconnect attempt only after a healthy connection (#2506)
## Problem
The WebSocket link reset the reconnect `attempt` as soon as a socket
reached `OPEN`. If a server completes the handshake and then closes the
socket straight away, `attempt` never goes above 1. That happens when
auth is rejected after the upgrade (1008/4001), when the server is at a
connection limit, or when it is draining. With `reconnect: { onClose:
true }`, the default `delay` (`attempt === 1 ? 0 : 2_000`) and
`onClose.delay` (0) then cause a reconnect on every handshake round trip
with no pause. The documented backoff and `maxAttempt` never apply.
```ts
new RPCLink({ connect: () => new WebSocket(url), connectOnInit: true, reconnect: { onClose: true } })
// against a server that accepts and immediately closes: reconnects forever, no delay
```
## Fix
`attempt` now resets only once a connection proves healthy:
- it receives a message from the server, or
- it stays open for at least 5 seconds before closing. This covers
healthy connections that stay idle.
A connection that closes sooner counts as a failed attempt. It gets the
`delay` backoff, and it counts toward `maxAttempt`. Before this change,
`maxAttempt` was only checked when `connect` itself failed. Now, once
`attempt` reaches `maxAttempt`, the next attempt fails right away with
the last close reason. It goes through the same `.catch` as a failed
connect. Every call waiting on that connection gets `WebSocket reconnect
failed after N attempt(s)`, with the close reason as its `cause`, before
a later call can start a new cycle.
Health is judged when the next attempt starts, in `getConnectedPeer`,
and no timer is added. A socket still in `CLOSING` (its `close` event
hasn't fired) is measured up to now. So a call made during the close
handshake of a long-lived socket reconnects immediately as attempt 1,
and the late `close` event can't reset the new cycle's count. A socket
that closed is measured up to its `close`. So one that closed after a
few milliseconds still counts as failed, even if the next call comes
much later.
## Docs
- JSDoc on `WebSocketLinkTransportAttemptInfo.attempt` describes when a
connection counts as healthy.
- `apps/content/docs/adapters/websocket.mdx` has a `:::info` note under
Auto Reconnect.
## Tests
New fake-timer tests in `rpc-link.test.ts`:
- sockets that close right after opening get the default backoff
(attempt 1 immediately, then 2 and 3 at 2 s each). **Fails without the
fix.**
- those sockets count toward `maxAttempt`. **Fails without the fix.**
- a server message resets `attempt`.
- when a cycle gives up after quick closes, two concurrent calls both
fail with the give-up error, whose `cause` is the last close reason, and
no new connection is made.
- a call made while a healthy socket is still `CLOSING` reconnects as
attempt 1, and the late `close` doesn't reset the new cycle. **Fails on
the first commit of this PR.**
- a socket that closed quickly still counts as failed when the next call
comes 60 s later.
The test "stops proactive reconnects once a reconnect cycle gives up"
now keeps its first socket open for 5 s. That makes the socket count as
healthy, which the test had assumed.
`pnpm check` passes. `pnpm exec vitest run` passes except
`openapi-generator.test.ts` > "names the nearest tsconfig when it does
not include the file, and why", which also fails on `main`.1 parent baa3350 commit f293ef4
3 files changed
Lines changed: 182 additions & 4 deletions
File tree
- apps/content/docs/adapters
- packages/client/src/adapters/websocket
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
323 | 323 | | |
324 | 324 | | |
325 | 325 | | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
680 | 680 | | |
681 | 681 | | |
682 | 682 | | |
| 683 | + | |
683 | 684 | | |
684 | 685 | | |
685 | 686 | | |
686 | 687 | | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
687 | 833 | | |
688 | 834 | | |
689 | 835 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
45 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
| |||
135 | 140 | | |
136 | 141 | | |
137 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
138 | 150 | | |
139 | 151 | | |
140 | 152 | | |
| |||
179 | 191 | | |
180 | 192 | | |
181 | 193 | | |
182 | | - | |
| 194 | + | |
183 | 195 | | |
184 | 196 | | |
185 | 197 | | |
| |||
192 | 204 | | |
193 | 205 | | |
194 | 206 | | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
195 | 211 | | |
196 | 212 | | |
197 | 213 | | |
198 | 214 | | |
199 | 215 | | |
200 | 216 | | |
201 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
202 | 223 | | |
203 | 224 | | |
204 | 225 | | |
| |||
218 | 239 | | |
219 | 240 | | |
220 | 241 | | |
| 242 | + | |
| 243 | + | |
221 | 244 | | |
222 | 245 | | |
223 | 246 | | |
| |||
249 | 272 | | |
250 | 273 | | |
251 | 274 | | |
| 275 | + | |
252 | 276 | | |
253 | 277 | | |
254 | 278 | | |
| |||
258 | 282 | | |
259 | 283 | | |
260 | 284 | | |
| 285 | + | |
261 | 286 | | |
262 | 287 | | |
263 | 288 | | |
| |||
280 | 305 | | |
281 | 306 | | |
282 | 307 | | |
283 | | - | |
284 | | - | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
285 | 313 | | |
286 | 314 | | |
287 | 315 | | |
| |||
0 commit comments