fix(server): release preview hosts after unanswered requests (#11381)
Co-authored-by: yashranaway <[email protected]>
This commit is contained in:
1 parent
24b711b7f9
commit
7235701de0
2 files changed
+139
-1
No files matched your search
@@ -15,10 +15,13 @@ import {
|
||||
type PreviewAutomationStreamEvent,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Deferred from "effect/Deferred";
|
||||
import * as Fiber from "effect/Fiber";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Stream from "effect/Stream";
|
||||
import * as TestClock from "effect/testing/TestClock";
|
||||
|
||||
import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";
|
||||
|
||||
@@ -1105,3 +1108,132 @@ it.effect("accepts responses only from the host that received the request", () =
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("evicts an unanswered host and lets later calls use a healthy runtime", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const broker = yield* makeBroker;
|
||||
const connected = yield* Deferred.make<string>();
|
||||
const received = yield* Deferred.make<RoutedRequest>();
|
||||
const otherReceived = yield* Deferred.make<void>();
|
||||
const otherCompleted = yield* Deferred.make<void>();
|
||||
const oldTab = PreviewTabId.make("tab-on-frozen-host");
|
||||
const events = yield* broker.connect(makeHost());
|
||||
const consumer = yield* Stream.runForEach(events, (event) => {
|
||||
if (event.type === "connected") return Deferred.succeed(connected, event.connectionId);
|
||||
const request = { ...event.request, connectionId: event.connectionId };
|
||||
if (request.operation === "open") {
|
||||
return broker.respond({
|
||||
clientId: "client-1",
|
||||
connectionId: event.connectionId,
|
||||
requestId: request.requestId,
|
||||
ok: true,
|
||||
result: { tabId: oldTab },
|
||||
});
|
||||
}
|
||||
return request.operation === "snapshot"
|
||||
? Deferred.succeed(received, request)
|
||||
: Deferred.succeed(otherReceived, undefined);
|
||||
}).pipe(Effect.forkScoped);
|
||||
const connectionId = yield* Deferred.await(connected);
|
||||
yield* broker.invoke({ scope, operation: "open", input: {} });
|
||||
|
||||
const healthyConnected = yield* Deferred.make<void>();
|
||||
const healthyRequests: RoutedRequest[] = [];
|
||||
const healthy = yield* broker.connect(makeHost({ clientId: "healthy" }));
|
||||
yield* Stream.runForEach(healthy, (event) => {
|
||||
if (event.type === "connected") return Deferred.succeed(healthyConnected, undefined);
|
||||
healthyRequests.push({ ...event.request, connectionId: event.connectionId });
|
||||
return broker.respond({
|
||||
clientId: "healthy",
|
||||
connectionId: event.connectionId,
|
||||
requestId: event.request.requestId,
|
||||
ok: true,
|
||||
result: "healthy",
|
||||
});
|
||||
}).pipe(Effect.forkScoped);
|
||||
yield* Deferred.await(healthyConnected);
|
||||
|
||||
const timedOut = yield* broker
|
||||
.invoke<void>({
|
||||
scope,
|
||||
operation: "snapshot",
|
||||
input: {},
|
||||
timeoutMs: 1_000,
|
||||
})
|
||||
.pipe(Effect.flip, Effect.forkScoped);
|
||||
const lateRequest = yield* Deferred.await(received);
|
||||
const other = yield* broker
|
||||
.invoke<void>({
|
||||
scope,
|
||||
operation: "evaluate",
|
||||
input: {},
|
||||
timeoutMs: 10_000,
|
||||
})
|
||||
.pipe(
|
||||
Effect.flip,
|
||||
Effect.tap(() => Deferred.succeed(otherCompleted, undefined)),
|
||||
Effect.forkScoped,
|
||||
);
|
||||
yield* Deferred.await(otherReceived);
|
||||
yield* TestClock.adjust(1_000);
|
||||
expect(yield* Fiber.join(timedOut)).toMatchObject({ _tag: "PreviewAutomationTimeoutError" });
|
||||
expect(yield* Deferred.isDone(otherCompleted)).toBe(true);
|
||||
expect(yield* Fiber.join(other)).toMatchObject({
|
||||
_tag: "PreviewAutomationClientDisconnectedError",
|
||||
});
|
||||
const consumerExit = yield* Fiber.await(consumer);
|
||||
expect(Exit.isFailure(consumerExit)).toBe(true);
|
||||
if (Exit.isFailure(consumerExit)) {
|
||||
expect(Cause.hasInterruptsOnly(consumerExit.cause)).toBe(true);
|
||||
}
|
||||
|
||||
// Late traffic from the evicted connection cannot restore its assignment.
|
||||
yield* broker.respond({
|
||||
clientId: "client-1",
|
||||
connectionId,
|
||||
requestId: lateRequest.requestId,
|
||||
ok: true,
|
||||
result: { tabId: oldTab },
|
||||
});
|
||||
yield* broker.focusHost({
|
||||
clientId: "client-1",
|
||||
connectionId,
|
||||
environmentId: scope.environmentId,
|
||||
focused: true,
|
||||
});
|
||||
expect(yield* broker.invoke({ scope, operation: "status", input: {} })).toBe("healthy");
|
||||
expect(healthyRequests).toHaveLength(1);
|
||||
expect(healthyRequests[0]?.tabId).toBeUndefined();
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("keeps a host that responds with an operation timeout", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const broker = yield* makeBroker;
|
||||
const connected = yield* Deferred.make<void>();
|
||||
const events = yield* broker.connect(makeHost());
|
||||
yield* Stream.runForEach(events, (event) => {
|
||||
if (event.type === "connected") return Deferred.succeed(connected, undefined);
|
||||
return broker.respond({
|
||||
clientId: "client-1",
|
||||
connectionId: event.connectionId,
|
||||
requestId: event.request.requestId,
|
||||
...(event.request.operation === "waitFor"
|
||||
? {
|
||||
ok: false,
|
||||
error: { _tag: "PreviewAutomationTimeoutError", message: "Selector timed out" },
|
||||
}
|
||||
: { ok: true, result: "responsive" }),
|
||||
});
|
||||
}).pipe(Effect.forkScoped);
|
||||
yield* Deferred.await(connected);
|
||||
expect(
|
||||
yield* broker.invoke<void>({ scope, operation: "waitFor", input: {} }).pipe(Effect.flip),
|
||||
).toMatchObject({ _tag: "PreviewAutomationTimeoutError" });
|
||||
expect(yield* broker.invoke({ scope, operation: "status", input: {} })).toBe("responsive");
|
||||
}),
|
||||
),
|
||||
);
|
||||
@@ -575,7 +575,13 @@ export const make = Effect.gen(function* PreviewAutomationBrokerMake() {
|
||||
}
|
||||
const result = yield* Deferred.await(deferred).pipe(Effect.timeoutOption(timeoutMs));
|
||||
return yield* Option.match(result, {
|
||||
onNone: () => Effect.fail(new PreviewAutomationTimeoutError(requestContext)),
|
||||
onNone: () =>
|
||||
Effect.gen(function* () {
|
||||
// An unanswered request invalidates this connection. Do not replay
|
||||
// actions: the client may have applied them before becoming unreachable.
|
||||
yield* disconnect(connection.clientId, connection.queue);
|
||||
return yield* new PreviewAutomationTimeoutError(requestContext);
|
||||
}),
|
||||
onSome: (value) => Effect.succeed(value as A),
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user