fix(mobile): preserve startup network path changes
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
68b85b9050
commit
eff1bf76aa
6 files changed
+143
-57
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
|
||||
import {
|
||||
observeNetworkPath,
|
||||
seedNetworkPathBaseline,
|
||||
UNKNOWN_NETWORK_PATH,
|
||||
} from "./network-path-change";
|
||||
|
||||
describe("network path change detection", () => {
|
||||
it("does not probe when the listener matches the seeded interface", () => {
|
||||
const seeded = seedNetworkPathBaseline(UNKNOWN_NETWORK_PATH, "WIFI");
|
||||
|
||||
expect(observeNetworkPath(seeded, "WIFI")).toEqual({
|
||||
baseline: { known: true, type: "WIFI" },
|
||||
shouldProbe: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("probes when a known interface changes", () => {
|
||||
const seeded = seedNetworkPathBaseline(UNKNOWN_NETWORK_PATH, "WIFI");
|
||||
|
||||
expect(observeNetworkPath(seeded, "CELLULAR").shouldProbe).toBe(true);
|
||||
});
|
||||
|
||||
it("probes when the listener wins the race with the async seed", () => {
|
||||
const observed = observeNetworkPath(UNKNOWN_NETWORK_PATH, "CELLULAR");
|
||||
|
||||
expect(observed.shouldProbe).toBe(true);
|
||||
expect(seedNetworkPathBaseline(observed.baseline, "WIFI")).toEqual({
|
||||
known: true,
|
||||
type: "CELLULAR",
|
||||
});
|
||||
});
|
||||
|
||||
it("probes on the first known interface after an unknown observation", () => {
|
||||
const seeded = seedNetworkPathBaseline(UNKNOWN_NETWORK_PATH, "WIFI");
|
||||
const unknown = observeNetworkPath(seeded, null);
|
||||
|
||||
expect(unknown.shouldProbe).toBe(false);
|
||||
expect(observeNetworkPath(unknown.baseline, "CELLULAR").shouldProbe).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
export interface NetworkPathBaseline {
|
||||
readonly known: boolean;
|
||||
readonly type: string | null;
|
||||
}
|
||||
|
||||
export const UNKNOWN_NETWORK_PATH: NetworkPathBaseline = {
|
||||
known: false,
|
||||
type: null,
|
||||
};
|
||||
|
||||
export function seedNetworkPathBaseline(
|
||||
baseline: NetworkPathBaseline,
|
||||
type: string | null,
|
||||
): NetworkPathBaseline {
|
||||
if (baseline.known || type === null) {
|
||||
return baseline;
|
||||
}
|
||||
return { known: true, type };
|
||||
}
|
||||
|
||||
export function observeNetworkPath(
|
||||
baseline: NetworkPathBaseline,
|
||||
type: string | null,
|
||||
): {
|
||||
readonly baseline: NetworkPathBaseline;
|
||||
readonly shouldProbe: boolean;
|
||||
} {
|
||||
if (type === null) {
|
||||
return {
|
||||
baseline: UNKNOWN_NETWORK_PATH,
|
||||
shouldProbe: false,
|
||||
};
|
||||
}
|
||||
return {
|
||||
baseline: { known: true, type },
|
||||
// If the initial async seed has not landed, the first listener event may
|
||||
// itself be the WiFi/cellular transition. A cheap advisory probe is safer
|
||||
// than losing that transition and waiting for the socket ping timeout.
|
||||
shouldProbe: !baseline.known || baseline.type !== type,
|
||||
};
|
||||
}
|
||||
@@ -31,6 +31,11 @@ import { appAtomRegistry } from "../state/atom-registry";
|
||||
import { clearThreadOutboxEnvironment } from "../state/thread-outbox";
|
||||
import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts";
|
||||
import { mobileApplicationActiveWakeup } from "./app-state-wakeups";
|
||||
import {
|
||||
observeNetworkPath,
|
||||
seedNetworkPathBaseline,
|
||||
UNKNOWN_NETWORK_PATH,
|
||||
} from "./network-path-change";
|
||||
import { connectionStorageLayer } from "./storage";
|
||||
|
||||
function networkStatus(state: Network.NetworkState): "unknown" | "offline" | "online" {
|
||||
@@ -110,20 +115,17 @@ const wakeupsLayer = Wakeups.layer({
|
||||
// it, which keeps flapping paths cheap. Seed the current type so
|
||||
// the first flip after startup is detected; the listener only
|
||||
// reports changes.
|
||||
let networkType: string | null = null;
|
||||
let networkPath = UNKNOWN_NETWORK_PATH;
|
||||
void Network.getNetworkStateAsync()
|
||||
.then((current) => {
|
||||
networkType ??= current.type ?? null;
|
||||
networkPath = seedNetworkPathBaseline(networkPath, current.type ?? null);
|
||||
})
|
||||
.catch(() => undefined);
|
||||
const networkSubscription = Network.addNetworkStateListener((state) => {
|
||||
const nextType = state.type ?? null;
|
||||
const previousType = networkType;
|
||||
networkType = nextType;
|
||||
const observation = observeNetworkPath(networkPath, state.type ?? null);
|
||||
networkPath = observation.baseline;
|
||||
if (
|
||||
previousType !== null &&
|
||||
nextType !== null &&
|
||||
nextType !== previousType &&
|
||||
observation.shouldProbe &&
|
||||
state.isConnected === true &&
|
||||
AppState.currentState === "active"
|
||||
) {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
export const bunWebSocketOptions = {
|
||||
// Negotiate permessage-deflate with clients that offer it. A dedicated
|
||||
// compressor keeps a per-connection sliding window, while the shared
|
||||
// decompressor avoids uWebSockets' dedicated-decompressor aborts.
|
||||
// Negotiate permessage-deflate with clients that offer it; clients that
|
||||
// don't still get uncompressed frames on their connection. A dedicated
|
||||
// compressor keeps a per-connection sliding window (context takeover) so the
|
||||
// compression dictionary is shared across server-to-client frames.
|
||||
// Decompression uses the shared decompressor: uWebSockets' dedicated
|
||||
// decompressor path can abort connections (close 1006) on valid DEFLATE
|
||||
// input — see https://github.com/uNetworking/uWebSockets.js/issues/633.
|
||||
perMessageDeflate: {
|
||||
compress: "dedicated",
|
||||
decompress: "shared",
|
||||
|
||||
@@ -5,6 +5,7 @@ import * as Deferred from "effect/Deferred";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Queue from "effect/Queue";
|
||||
import * as Ref from "effect/Ref";
|
||||
import * as Stream from "effect/Stream";
|
||||
import * as SubscriptionRef from "effect/SubscriptionRef";
|
||||
@@ -122,13 +123,10 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
|
||||
const prepareCount = yield* Ref.make(0);
|
||||
const sessionCount = yield* Ref.make(0);
|
||||
const releaseCount = yield* Ref.make(0);
|
||||
const wakeups = yield* SubscriptionRef.make<{
|
||||
readonly sequence: number;
|
||||
const wakeups = yield* Queue.unbounded<{
|
||||
readonly reason: ConnectionWakeups.ConnectionWakeup;
|
||||
}>({
|
||||
sequence: 0,
|
||||
reason: "application-active",
|
||||
});
|
||||
readonly consumed: Deferred.Deferred<void>;
|
||||
}>();
|
||||
const closedSessions = yield* Ref.make<
|
||||
ReadonlyArray<Deferred.Deferred<never, ConnectionTransientError>>
|
||||
>([]);
|
||||
@@ -180,8 +178,8 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
|
||||
Layer.succeed(
|
||||
ConnectionWakeups.ConnectionWakeups,
|
||||
ConnectionWakeups.ConnectionWakeups.of({
|
||||
changes: SubscriptionRef.changes(wakeups).pipe(
|
||||
Stream.drop(1),
|
||||
changes: Stream.fromQueue(wakeups).pipe(
|
||||
Stream.tap((event) => Deferred.succeed(event.consumed, undefined)),
|
||||
Stream.map((event) => event.reason),
|
||||
),
|
||||
}),
|
||||
@@ -198,11 +196,13 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
|
||||
sessionCount,
|
||||
releaseCount,
|
||||
setNetworkStatus: (status: NetworkStatus) => SubscriptionRef.set(networkStatus, status),
|
||||
wake: (reason: ConnectionWakeups.ConnectionWakeup) =>
|
||||
SubscriptionRef.update(wakeups, (event) => ({
|
||||
sequence: event.sequence + 1,
|
||||
reason,
|
||||
})),
|
||||
wake: Effect.fn("TestConnectionHarness.wake")(function* (
|
||||
reason: ConnectionWakeups.ConnectionWakeup,
|
||||
) {
|
||||
const consumed = yield* Deferred.make<void>();
|
||||
yield* Queue.offer(wakeups, { reason, consumed });
|
||||
yield* Deferred.await(consumed);
|
||||
}),
|
||||
closeLatestSession: Effect.fn("TestConnectionHarness.closeLatestSession")(function* (
|
||||
error = transient("Session closed."),
|
||||
) {
|
||||
@@ -1071,8 +1071,12 @@ describe("EnvironmentSupervisor", () => {
|
||||
|
||||
it.effect("uses the full tolerance window for a stalled desktop foreground probe", () =>
|
||||
Effect.gen(function* () {
|
||||
const probeStarted = yield* Deferred.make<void>();
|
||||
const harness = yield* makeHarness({
|
||||
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
|
||||
probe: (attempt) =>
|
||||
attempt === 1
|
||||
? Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never))
|
||||
: Effect.void,
|
||||
});
|
||||
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
||||
initiallyDesired: true,
|
||||
@@ -1080,6 +1084,7 @@ describe("EnvironmentSupervisor", () => {
|
||||
|
||||
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
||||
yield* harness.wake("application-active");
|
||||
yield* Deferred.await(probeStarted);
|
||||
yield* TestClock.adjust("14999 millis");
|
||||
expect(yield* Ref.get(harness.sessionCount)).toBe(1);
|
||||
yield* TestClock.adjust("1 milli");
|
||||
@@ -1094,8 +1099,12 @@ describe("EnvironmentSupervisor", () => {
|
||||
|
||||
it.effect("quickly times out a stalled mobile foreground liveness probe", () =>
|
||||
Effect.gen(function* () {
|
||||
const probeStarted = yield* Deferred.make<void>();
|
||||
const harness = yield* makeHarness({
|
||||
probe: (attempt) => (attempt === 1 ? Effect.never : Effect.void),
|
||||
probe: (attempt) =>
|
||||
attempt === 1
|
||||
? Deferred.succeed(probeStarted, undefined).pipe(Effect.andThen(Effect.never))
|
||||
: Effect.void,
|
||||
});
|
||||
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
|
||||
initiallyDesired: true,
|
||||
@@ -1103,6 +1112,7 @@ describe("EnvironmentSupervisor", () => {
|
||||
|
||||
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
|
||||
yield* harness.wake("application-active-probe");
|
||||
yield* Deferred.await(probeStarted);
|
||||
yield* TestClock.adjust("3 seconds");
|
||||
// The timed-out wake probe reconnects immediately without a backoff
|
||||
// sleep: no further clock advance is needed.
|
||||
|
||||
@@ -252,14 +252,13 @@ describe("environment shell synchronization", () => {
|
||||
const events = yield* Queue.unbounded<OrchestrationShellStreamItem>();
|
||||
const wakeups = yield* Queue.unbounded<ConnectionWakeups.ConnectionWakeup>();
|
||||
const loaderCalls = yield* Ref.make(0);
|
||||
const capturedAfterSequences = yield* Ref.make<ReadonlyArray<number | undefined>>([]);
|
||||
const subscriptionReceipts = yield* Queue.unbounded<number | undefined>();
|
||||
const client = {
|
||||
[ORCHESTRATION_WS_METHODS.subscribeShell]: (input: { readonly afterSequence?: number }) =>
|
||||
Stream.unwrap(
|
||||
Ref.update(capturedAfterSequences, (captured) => [
|
||||
...captured,
|
||||
input.afterSequence,
|
||||
]).pipe(Effect.as(Stream.fromQueue(events))),
|
||||
Queue.offer(subscriptionReceipts, input.afterSequence).pipe(
|
||||
Effect.as(Stream.fromQueue(events)),
|
||||
),
|
||||
),
|
||||
} as unknown as WsRpcProtocolClient;
|
||||
const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
|
||||
@@ -306,11 +305,7 @@ describe("environment shell synchronization", () => {
|
||||
);
|
||||
|
||||
// A new session starts from an authoritative HTTP snapshot.
|
||||
for (let attempt = 0; attempt < 100; attempt += 1) {
|
||||
if ((yield* Ref.get(capturedAfterSequences)).length >= 1) break;
|
||||
yield* Effect.yieldNow;
|
||||
}
|
||||
expect(yield* Ref.get(capturedAfterSequences)).toEqual([10]);
|
||||
expect(yield* Queue.take(subscriptionReceipts)).toBe(10);
|
||||
yield* Queue.offer(events, { kind: "synchronized" });
|
||||
yield* SubscriptionRef.changes(shellState).pipe(
|
||||
Stream.filter((value) => value.status === "live"),
|
||||
@@ -330,35 +325,27 @@ describe("environment shell synchronization", () => {
|
||||
);
|
||||
|
||||
yield* Queue.offer(wakeups, "application-active");
|
||||
for (let attempt = 0; attempt < 100; attempt += 1) {
|
||||
if ((yield* Ref.get(capturedAfterSequences)).length >= 2) break;
|
||||
yield* Effect.yieldNow;
|
||||
}
|
||||
expect(yield* Ref.get(capturedAfterSequences)).toEqual([10, 40]);
|
||||
expect(yield* Queue.take(subscriptionReceipts)).toBe(40);
|
||||
yield* Queue.offer(events, { kind: "synchronized" });
|
||||
|
||||
yield* Queue.offer(wakeups, "application-active-probe");
|
||||
for (let attempt = 0; attempt < 100; attempt += 1) {
|
||||
if ((yield* Ref.get(capturedAfterSequences)).length >= 3) break;
|
||||
yield* Effect.yieldNow;
|
||||
}
|
||||
expect(yield* Ref.get(capturedAfterSequences)).toEqual([10, 40, 40]);
|
||||
|
||||
yield* Queue.offer(wakeups, "application-active-reconnect");
|
||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
||||
yield* Effect.yieldNow;
|
||||
}
|
||||
expect((yield* Ref.get(capturedAfterSequences)).length).toBe(3);
|
||||
expect(yield* Queue.take(subscriptionReceipts)).toBe(40);
|
||||
expect(yield* Ref.get(loaderCalls)).toBe(1);
|
||||
|
||||
// Replacing the session performs another authoritative refresh.
|
||||
yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
|
||||
for (let attempt = 0; attempt < 100; attempt += 1) {
|
||||
if ((yield* Ref.get(capturedAfterSequences)).length >= 4) break;
|
||||
yield* Effect.yieldNow;
|
||||
}
|
||||
expect(yield* Ref.get(capturedAfterSequences)).toEqual([10, 40, 40, 20]);
|
||||
expect(yield* Queue.take(subscriptionReceipts)).toBe(20);
|
||||
expect(yield* Ref.get(loaderCalls)).toBe(2);
|
||||
}),
|
||||
);
|
||||
|
||||
it("only resubscribes for foreground wakeups that keep the current session", () => {
|
||||
expect(ConnectionWakeups.shouldResubscribeAfterWakeup("application-active")).toBe(true);
|
||||
expect(ConnectionWakeups.shouldResubscribeAfterWakeup("application-active-probe")).toBe(true);
|
||||
expect(ConnectionWakeups.shouldResubscribeAfterWakeup("application-active-reconnect")).toBe(
|
||||
false,
|
||||
);
|
||||
expect(ConnectionWakeups.shouldResubscribeAfterWakeup("network-path-changed")).toBe(false);
|
||||
expect(ConnectionWakeups.shouldResubscribeAfterWakeup("credentials-changed")).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user