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

connection-runtime.md

74 lines4.0 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 transport retry owner
10​
11The [supervisor](../../packages/client-runtime/src/connection/supervisor.ts) owns
12transport retry 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## HTTP authorization
31​
32RPC sessions authenticate at socket upgrade, while HTTP requests need current
33credentials from the
34[authorization service](../../packages/client-runtime/src/authorization/service.ts).
35Replacing a healthy socket for HTTP renewal would interrupt conversations and
36change the transport generation without a transport failure. Credential expiry
37does not close the socket, and refresh failure belongs to the HTTP operation.
38​
39Session listings must retain unrevoked connected sessions after credential expiry
40so an open connection does not disappear from connection management. This does
41not extend the credential's lifetime. New HTTP requests and socket upgrades still
42require valid credentials.
43​
44## Transport health and data freshness are separate
45​
46A socket opening is insufficient evidence that the environment is usable. The
47[RPC session](../../packages/client-runtime/src/rpc/session.ts) waits for the
48initial server configuration before becoming ready. Shell and thread data then
49have their own synchronization state. A failed shell subscription can coexist
50with a healthy connection; labeling that state "reconnecting" promises a
51transport retry that will never happen.
52​
53Cached projections remain readable offline. They must neither imply a live
54connection nor overwrite newer live data during a reconnect. Loading and
55resuming snapshots belongs to the shared state services, so every view agrees
56on which data is current.
57​
58[Thread detail](../../packages/client-runtime/src/state/threads.ts) separates
59subscription lifetime from cache lifetime. Mounted consumers share one live
60stream, which stops when the last consumer unmounts; hidden mounted routes still
61count. A registry-local cache retains state and its replay cursor for five idle
62minutes so back navigation can resume without another snapshot download.
63​
64Retain state and cursor together only after an update finishes. Cancellation must
65not advance the cached cursor beyond the applied data, and an old scope must not
66overwrite its successor's cache. Preserve pagination data on reuse, but clear
67canceled loading state.
68​
69The [RPC boundary](../../packages/client-runtime/src/rpc/client.ts) resolves
70requests against the current session at execution time. Durable subscriptions
71follow replacement sessions. After a transport failure they wait for the
72supervisor; an expected domain failure may resubscribe on the same healthy
73session. Reconnection does not automatically replay mutations, whose retry and
74idempotency rules belong to the operation.