Skip to content

Commit 1a10428

Browse files
committed
fix(client): compare upgrades against the live connection route
1 parent 8dadeb2 commit 1a10428

2 files changed

Lines changed: 48 additions & 27 deletions

File tree

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,17 +1801,23 @@ describe("EnvironmentSupervisor routes", () => {
18011801
}),
18021802
);
18031803

1804-
it.effect.each(["initial", "late", "withdrawn", "omitted"] as const)(
1804+
it.effect.each(["initial", "late", "withdrawn", "withdrawn-upgrade", "omitted"] as const)(
18051805
"learns the %s direct endpoint over T3 Connect",
18061806
(discovery) =>
18071807
Effect.gen(function* () {
18081808
const relayEntry: ConnectionCatalogEntry =
1809-
discovery === "withdrawn" || discovery === "omitted"
1809+
discovery.startsWith("withdrawn") || discovery === "omitted"
18101810
? {
18111811
target: LAN_ROUTE.target,
18121812
profile: Option.some(
18131813
new BearerConnectionProfile({
18141814
...Option.getOrThrow(LAN_ROUTE.profile),
1815+
...(discovery === "withdrawn-upgrade"
1816+
? {
1817+
httpBaseUrl: "https://old.example.test/",
1818+
wsBaseUrl: "wss://old.example.test/",
1819+
}
1820+
: {}),
18151821
learned: true,
18161822
authorization: "t3-connect",
18171823
}),
@@ -1899,6 +1905,12 @@ describe("EnvironmentSupervisor routes", () => {
18991905
yield* Deferred.await(discoveryStarted);
19001906
}
19011907
yield* Deferred.succeed(finishDiscovery, undefined);
1908+
if (discovery === "withdrawn-upgrade") {
1909+
yield* Deferred.await(discoveryProcessed);
1910+
yield* TestClock.adjust("60 seconds");
1911+
expect((yield* SubscriptionRef.get(supervisor.state)).generation).toBe(2);
1912+
return;
1913+
}
19021914
if (discovery === "withdrawn" || discovery === "omitted") {
19031915
yield* Deferred.await(discoveryProcessed);
19041916
expect(yield* Ref.get(learned)).toContain(

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

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
connectionRoutes,
3737
type ConnectionRouteKind,
3838
entryWithRoutes,
39+
insertRoute,
3940
type ReportedEndpoint,
4041
} from "./routes.ts";
4142

@@ -87,6 +88,10 @@ interface PendingRetryTrace {
8788
readonly reason: ConnectionAttemptError["reason"];
8889
}
8990

91+
type ConnectedLease = ConnectionDriver.EnvironmentConnectionLease & {
92+
readonly activeRoute: ConnectionRoute | undefined;
93+
};
94+
9095
interface TracedAttemptFailure {
9196
readonly error: ConnectionAttemptError;
9297
readonly attemptSpan: Option.Option<Tracer.Span>;
@@ -332,12 +337,9 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
332337

333338
const learnRoutesFrom = Effect.fnUntraced(function* (
334339
learn: NonNullable<EnvironmentSupervisorOptions["learnRoutes"]>,
335-
lease: ConnectionDriver.EnvironmentConnectionLease,
340+
lease: ConnectedLease,
336341
) {
337-
const activeId = connectionRouteId(lease.prepared.target);
338-
const activeRoute = (yield* currentRoutes).find(
339-
(route) => connectionRouteId(route.target) === activeId,
340-
);
342+
const activeRoute = lease.activeRoute;
341343
if (activeRoute === undefined) return;
342344
yield* lease.session.subscribeServerConfig().pipe(
343345
Stream.filter((event) => event.type === "snapshot"),
@@ -357,18 +359,20 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
357359
);
358360
});
359361

360-
const routeIndex = (lease: ConnectionDriver.EnvironmentConnectionLease) =>
362+
const routeIndex = (lease: ConnectedLease) =>
361363
currentRoutes.pipe(
362-
Effect.map((routes) =>
363-
routes.findIndex(
364-
(route) => connectionRouteId(route.target) === connectionRouteId(lease.prepared.target),
365-
),
366-
),
364+
Effect.map((routes) => {
365+
const activeId = connectionRouteId(lease.prepared.target);
366+
const index = routes.findIndex((route) => connectionRouteId(route.target) === activeId);
367+
return index === -1 && lease.activeRoute !== undefined
368+
? insertRoute(routes, lease.activeRoute).findIndex(
369+
(route) => connectionRouteId(route.target) === activeId,
370+
)
371+
: index;
372+
}),
367373
);
368374

369-
const checkBetterRoutes = Effect.fnUntraced(function* (
370-
lease: ConnectionDriver.EnvironmentConnectionLease,
371-
) {
375+
const checkBetterRoutes = Effect.fnUntraced(function* (lease: ConnectedLease) {
372376
const latest = yield* Ref.get(currentEntry);
373377
const current = yield* routeIndex(lease);
374378
if (current <= 0) return;
@@ -406,8 +410,8 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
406410
});
407411
});
408412

409-
const betterRouteChecks = yield* Queue.dropping<ConnectionDriver.EnvironmentConnectionLease>(1);
410-
const requestBetterRouteCheck = (lease: ConnectionDriver.EnvironmentConnectionLease) =>
413+
const betterRouteChecks = yield* Queue.dropping<ConnectedLease>(1);
414+
const requestBetterRouteCheck = (lease: ConnectedLease) =>
411415
routeIndex(lease).pipe(
412416
Effect.flatMap((index) =>
413417
index > 0 ? Queue.offer(betterRouteChecks, lease).pipe(Effect.asVoid) : Effect.void,
@@ -673,7 +677,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
673677
// event, and "alive" once the socket delivers a frame. A slow or
674678
// unreachable host never ends it.
675679
const awaitSilentSocket = Effect.fn("EnvironmentSupervisor.awaitSilentSocket")(function* (
676-
lease: ConnectionDriver.EnvironmentConnectionLease,
680+
lease: ConnectedLease,
677681
routeKind: ConnectionRouteKind | undefined,
678682
) {
679683
const startedAt = yield* Clock.monotonicTimeNanos;
@@ -706,9 +710,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
706710
// all probe the live session instead of replacing it, so a healthy socket is
707711
// not torn down (the offline report is often wrong, for example for a
708712
// loopback server).
709-
const monitorConnectedLease = Effect.fnUntraced(function* (
710-
lease: ConnectionDriver.EnvironmentConnectionLease,
711-
) {
713+
const monitorConnectedLease = Effect.fnUntraced(function* (lease: ConnectedLease) {
712714
// A probe answers an explicit retry here, so the retry must not also reset
713715
// the backoff of a later, unrelated failure.
714716
const takeSignal = Queue.take(signals).pipe(
@@ -946,11 +948,18 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
946948
} satisfies AttemptOutcome;
947949
}
948950

951+
const connectedLease: ConnectedLease = {
952+
...active.lease,
953+
activeRoute: (yield* currentRoutes).find(
954+
(route) =>
955+
connectionRouteId(route.target) === connectionRouteId(active.lease.prepared.target),
956+
),
957+
};
949958
const connectedAt = yield* Clock.currentTimeMillis;
950-
yield* SubscriptionRef.set(prepared, Option.some(active.lease.prepared));
951-
yield* SubscriptionRef.set(session, Option.some(active.lease.session));
959+
yield* SubscriptionRef.set(prepared, Option.some(connectedLease.prepared));
960+
yield* SubscriptionRef.set(session, Option.some(connectedLease.session));
952961
if (options?.learnRoutes !== undefined) {
953-
yield* learnRoutesFrom(options.learnRoutes, active.lease).pipe(Effect.forkScoped);
962+
yield* learnRoutesFrom(options.learnRoutes, connectedLease).pipe(Effect.forkScoped);
954963
}
955964
yield* setState({
956965
desired: true,
@@ -964,13 +973,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
964973
});
965974

966975
const connectedExit = yield* Effect.raceFirst(
967-
active.lease.session.closed.pipe(
976+
connectedLease.session.closed.pipe(
968977
Effect.mapError((error): TracedAttemptFailure => ({
969978
error,
970979
attemptSpan: active.attemptSpan,
971980
})),
972981
),
973-
monitorConnectedLease(active.lease).pipe(
982+
monitorConnectedLease(connectedLease).pipe(
974983
Effect.mapError((error): TracedAttemptFailure => ({
975984
error,
976985
attemptSpan: active.attemptSpan,

0 commit comments

Comments
 (0)