Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

connection-runtime.md

60 lines3.3 KB

1# Connection runtime
2​
3Web, the desktop renderer, and mobile share one connection owner per environment
4in `packages/client-runtime`. Platform code supplies storage, credentials, network
5signals, and application lifecycle events. React views consume the runtime.
6Keeping retries and session lifetime here prevents competing reconnect loops when
7several views need the same environment.
8​
9## One retry owner
10​
11The [supervisor](../../packages/client-runtime/src/connection/supervisor.ts) owns
12retry policy; resolving an endpoint and opening an RPC session are single
13attempts. Transient failures retry with capped backoff. Offline states and
14authentication failures wait for a wakeup instead of spending attempts on
15unchanged conditions.
16​
17Foregrounding needs different treatment depending on the connection's state.
18It wakes a retry immediately, leaves an ordinary in-flight attempt alone, and
19probes an established session before replacing it. A long mobile background
20suspension forces replacement because the OS can kill a socket without reporting
21closure. Treating every foreground event as a reconnect delays healthy attempts;
22treating every resume as harmless leaves suspended sockets stuck.
23​
24The [registry](../../packages/client-runtime/src/connection/registry.ts) scopes
25connections by environment. An involuntary disconnect retains the registration
26and cached data. Explicit removal closes the scope and clears credentials,
27projections, and platform-owned state such as drafts. Cloud-account changes apply
28to relay registrations; they must not discard directly paired environments.
29​
30## Transport health and data freshness are separate
31​
32A socket opening is insufficient evidence that the environment is usable. The
33[RPC session](../../packages/client-runtime/src/rpc/session.ts) waits for the
34initial server configuration before becoming ready. Shell and thread data then
35have their own synchronization state. A failed shell subscription can coexist
36with a healthy connection; labeling that state "reconnecting" promises a
37transport retry that will never happen.
38​
39Cached projections remain readable offline. They must neither imply a live
40connection nor overwrite newer live data during a reconnect. Loading and
41resuming snapshots belongs to the shared state services, so every view agrees
42on which data is current.
43​
44[Thread detail](../../packages/client-runtime/src/state/threads.ts) separates
45subscription lifetime from cache lifetime. Mounted consumers share one live
46stream, which stops when the last consumer unmounts; hidden mounted routes still
47count. A registry-local cache retains state and its replay cursor for five idle
48minutes so back navigation can resume without another snapshot download.
49​
50Retain state and cursor together only after an update finishes. Cancellation must
51not advance the cached cursor beyond the applied data, and an old scope must not
52overwrite its successor's cache. Preserve pagination data on reuse, but clear
53canceled loading state.
54​
55The [RPC boundary](../../packages/client-runtime/src/rpc/client.ts) resolves
56requests against the current session at execution time. Durable subscriptions
57follow replacement sessions. After a transport failure they wait for the
58supervisor; an expected domain failure may resubscribe on the same healthy
59session. Reconnection does not automatically replay mutations, whose retry and
60idempotency rules belong to the operation.