Skip to content

Commit e279787

Browse files
committed
fix(mobile): reset retries while offline
1 parent 7b02afa commit e279787

2 files changed

Lines changed: 85 additions & 11 deletions

File tree

‎packages/client-runtime/src/connection/supervisor.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,38 @@ describe("EnvironmentSupervisor", () => {
311311
}),
312312
);
313313

314+
it.effect("resets retries when activation arrives before the network returns", () =>
315+
Effect.gen(function* () {
316+
const harness = yield* makeHarness();
317+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
318+
initiallyDesired: true,
319+
}).pipe(Effect.provide(harness.dependencies));
320+
321+
yield* awaitState(supervisor.state, (state) => state.phase === "connected");
322+
yield* harness.closeLatestSession();
323+
yield* awaitState(
324+
supervisor.state,
325+
(state) => state.phase === "backoff" && state.attempt === 1,
326+
);
327+
yield* harness.setNetworkStatus("offline");
328+
yield* awaitState(
329+
supervisor.state,
330+
(state) => state.phase === "offline" && state.attempt === 2,
331+
);
332+
333+
yield* harness.wake("application-active-reconnect");
334+
yield* awaitState(
335+
supervisor.state,
336+
(state) => state.phase === "offline" && state.attempt === 1,
337+
);
338+
yield* harness.setNetworkStatus("online");
339+
yield* awaitState(
340+
supervisor.state,
341+
(state) => state.phase === "connected" && state.generation === 2 && state.attempt === 1,
342+
);
343+
}),
344+
);
345+
314346
it.effect("retries forever with exponential backoff capped at sixteen seconds", () =>
315347
Effect.gen(function* () {
316348
const harness = yield* makeHarness({
@@ -501,6 +533,38 @@ describe("EnvironmentSupervisor", () => {
501533
}).pipe(Effect.provide(TestClock.layer())),
502534
);
503535

536+
it.effect("resets retries when activation wakes a blocked connection", () =>
537+
Effect.gen(function* () {
538+
const harness = yield* makeHarness({
539+
prepare: (attempt) =>
540+
attempt === 1
541+
? Effect.fail(transient())
542+
: attempt === 2
543+
? Effect.fail(blocked())
544+
: Effect.succeed(PREPARED_CONNECTION),
545+
});
546+
const supervisor = yield* EnvironmentSupervisor.make(TARGET_ENTRY, {
547+
initiallyDesired: true,
548+
}).pipe(Effect.provide(harness.dependencies));
549+
550+
yield* awaitState(
551+
supervisor.state,
552+
(state) => state.phase === "backoff" && state.attempt === 1,
553+
);
554+
yield* TestClock.adjust("1 second");
555+
yield* awaitState(
556+
supervisor.state,
557+
(state) => state.phase === "blocked" && state.attempt === 2,
558+
);
559+
560+
yield* harness.wake("application-active-reconnect");
561+
yield* awaitState(
562+
supervisor.state,
563+
(state) => state.phase === "connected" && state.attempt === 1,
564+
);
565+
}).pipe(Effect.provide(TestClock.layer())),
566+
);
567+
504568
it.effect("releases a live session while offline and starts a new generation when online", () =>
505569
Effect.gen(function* () {
506570
const harness = yield* makeHarness();

‎packages/client-runtime/src/connection/supervisor.ts‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -626,20 +626,27 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
626626
);
627627
});
628628

629-
const waitForSignal = Queue.take(signals);
629+
const waitForSignal = Queue.take(signals).pipe(
630+
Effect.map(
631+
(next) => next._tag === "Wakeup" && ConnectionWakeups.isApplicationActiveWakeup(next.reason),
632+
),
633+
);
630634

631635
const run = Effect.fnUntraced(function* () {
632636
let failureCount = 0;
633637
let generation = 0;
634638
let latestFailure: ConnectionAttemptError | null = null;
635639
let pendingRetry = Option.none<PendingRetryTrace>();
640+
const resetRetryLadder = () => {
641+
failureCount = 0;
642+
pendingRetry = Option.none();
643+
};
636644

637645
for (;;) {
638646
const currentIntent = yield* Ref.get(intent);
639647
if (!currentIntent.desired) {
640-
failureCount = 0;
648+
resetRetryLadder();
641649
latestFailure = null;
642-
pendingRetry = Option.none();
643650
yield* clearLease;
644651
yield* setState(availableState(currentIntent, generation));
645652
yield* waitForSignal;
@@ -648,7 +655,10 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
648655
if (currentIntent.network === "offline") {
649656
yield* clearLease;
650657
yield* setState(offlineState(currentIntent, generation, failureCount + 1, latestFailure));
651-
yield* waitForSignal;
658+
const applicationActivated = yield* waitForSignal;
659+
if (applicationActivated) {
660+
resetRetryLadder();
661+
}
652662
continue;
653663
}
654664

@@ -660,15 +670,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
660670
if (outcome.established) {
661671
generation = nextGeneration;
662672
if (outcome.stable) {
663-
failureCount = 0;
673+
resetRetryLadder();
664674
latestFailure = null;
665-
pendingRetry = Option.none();
666675
}
667676
}
668677
if (outcome._tag === "Interrupted") {
669678
if (outcome.resetRetry) {
670-
failureCount = 0;
671-
pendingRetry = Option.none();
679+
resetRetryLadder();
672680
}
673681
continue;
674682
}
@@ -688,7 +696,10 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
688696
lastFailure: error,
689697
retryAt: null,
690698
});
691-
yield* waitForSignal;
699+
const applicationActivated = yield* waitForSignal;
700+
if (applicationActivated) {
701+
resetRetryLadder();
702+
}
692703
continue;
693704
}
694705

@@ -713,8 +724,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
713724
});
714725
const applicationActivated = yield* waitForRetrySignal(delayMs);
715726
if (applicationActivated) {
716-
failureCount = 0;
717-
pendingRetry = Option.none();
727+
resetRetryLadder();
718728
}
719729
}
720730
});

0 commit comments

Comments
 (0)