Skip to content

Commit 2b76e36

Browse files
committed
fix(client-runtime): gate queries on active sessions
1 parent 94e8128 commit 2b76e36

2 files changed

Lines changed: 46 additions & 17 deletions

File tree

‎packages/client-runtime/src/state/runtime.test.ts‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from "../connection/model.ts";
2323
import * as EnvironmentRegistry from "../connection/registry.ts";
2424
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
25+
import { EnvironmentRpcUnavailableError } from "../rpc/client.ts";
2526
import type * as RpcSession from "../rpc/session.ts";
2627
import {
2728
environmentRpcKey,
@@ -46,6 +47,8 @@ const QUERY_ENVIRONMENT = new PrimaryConnectionTarget({
4647
wsBaseUrl: "wss://query.example.test",
4748
});
4849

50+
const QUERY_RPC_SESSION = {} as RpcSession.RpcSession;
51+
4952
class TestQueryError extends Schema.TaggedErrorClass<TestQueryError>()("TestQueryError", {
5053
message: Schema.String,
5154
}) {}
@@ -78,19 +81,25 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
7881
execute: Effect.Effect<A, E>,
7982
) {
8083
const supervisorState = yield* SubscriptionRef.make(queryConnectionState());
84+
const supervisorSession = yield* SubscriptionRef.make(Option.some(QUERY_RPC_SESSION));
8185
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
8286
target: QUERY_ENVIRONMENT,
8387
state: supervisorState,
84-
session: yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(Option.none()),
88+
session: supervisorSession,
8589
prepared: yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(Option.none()),
8690
connect: Effect.void,
8791
disconnect: Effect.void,
8892
retryNow: Effect.void,
8993
} satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
9094
const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = (_environmentId, effect) =>
9195
Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor);
96+
const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = (
97+
_environmentId,
98+
stream,
99+
) => Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor);
92100
const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({
93101
run,
102+
followStream,
94103
stateChanges: () => SubscriptionRef.changes(supervisorState),
95104
} as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);
96105
const runtime = Atom.runtime(
@@ -104,6 +113,7 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
104113

105114
return {
106115
atom: family({ environmentId: QUERY_ENVIRONMENT.environmentId, input: undefined }),
116+
supervisorSession,
107117
supervisorState,
108118
};
109119
});
@@ -268,17 +278,21 @@ describe("environment query lifecycle", () => {
268278
Effect.scoped(
269279
Effect.gen(function* () {
270280
const firstStarted = Latch.makeUnsafe();
271-
const interruptFirst = Latch.makeUnsafe();
281+
const failFirst = Latch.makeUnsafe();
272282
const firstSettled = Latch.makeUnsafe();
283+
const unavailable = new EnvironmentRpcUnavailableError({
284+
environmentId: QUERY_ENVIRONMENT.environmentId,
285+
message: "Query environment is not connected.",
286+
});
273287
let executions = 0;
274288
const execute = Effect.suspend(() => {
275289
executions += 1;
276290
if (executions > 1) {
277291
return Effect.succeed("recovered");
278292
}
279293
firstStarted.openUnsafe();
280-
return interruptFirst.await.pipe(
281-
Effect.andThen(Effect.interrupt),
294+
return failFirst.await.pipe(
295+
Effect.andThen(Effect.fail(unavailable)),
282296
Effect.ensuring(
283297
Effect.sync(() => {
284298
firstSettled.openUnsafe();
@@ -304,6 +318,14 @@ describe("environment query lifecycle", () => {
304318
);
305319

306320
yield* firstStarted.await;
321+
yield* SubscriptionRef.set(harness.supervisorSession, Option.none());
322+
yield* Effect.yieldNow;
323+
failFirst.openUnsafe();
324+
yield* firstSettled.await;
325+
yield* Effect.yieldNow;
326+
327+
expect(observed.some(AsyncResult.isFailure)).toBe(false);
328+
307329
yield* SubscriptionRef.set(
308330
harness.supervisorState,
309331
queryConnectionState({ phase: "connecting", stage: "preparing" }),
@@ -322,12 +344,8 @@ describe("environment query lifecycle", () => {
322344
}),
323345
);
324346
yield* Effect.yieldNow;
325-
interruptFirst.openUnsafe();
326-
yield* firstSettled.await;
327-
yield* Effect.yieldNow;
328-
329-
expect(observed.some(AsyncResult.isFailure)).toBe(false);
330347

348+
yield* SubscriptionRef.set(harness.supervisorSession, Option.some(QUERY_RPC_SESSION));
331349
yield* SubscriptionRef.set(
332350
harness.supervisorState,
333351
queryConnectionState({ generation: 2 }),

‎packages/client-runtime/src/state/runtime.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Effect from "effect/Effect";
44
import * as Exit from "effect/Exit";
55
import * as Option from "effect/Option";
66
import * as Stream from "effect/Stream";
7+
import * as SubscriptionRef from "effect/SubscriptionRef";
78
import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
89

910
import type { ConnectionAttemptError } from "../connection/model.ts";
@@ -484,11 +485,18 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
484485
readonly environmentId: EnvironmentIdType;
485486
readonly input: Input;
486487
}) => Atom.Atom<AsyncResult.AsyncResult<A, E | ER | Error>> {
487-
const connectionStateAtom = Atom.family((environmentId: EnvironmentIdType) =>
488+
const connectionAtom = Atom.family((environmentId: EnvironmentIdType) =>
488489
runtime.atom(
489-
Stream.unwrap(
490-
EnvironmentRegistry.pipe(
491-
Effect.map((registry) => registry.stateChanges(environmentId).pipe(Stream.changes)),
490+
followStreamInEnvironment(
491+
environmentId,
492+
Stream.unwrap(
493+
EnvironmentSupervisor.pipe(
494+
Effect.map((supervisor) =>
495+
SubscriptionRef.changes(supervisor.state).pipe(
496+
Stream.zipLatest(SubscriptionRef.changes(supervisor.session)),
497+
),
498+
),
499+
),
492500
),
493501
),
494502
{ initialValue: null },
@@ -502,15 +510,18 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
502510
A,
503511
E | ConnectionAttemptError | EnvironmentNotRegisteredError | EnvironmentRpcUnavailableError
504512
>((get) => {
505-
const connectionState = Option.getOrNull(
506-
AsyncResult.value(get(connectionStateAtom(target.environmentId))),
513+
const connection = Option.getOrNull(
514+
AsyncResult.value(get(connectionAtom(target.environmentId))),
507515
);
508-
if (connectionState === null) {
516+
if (connection === null) {
509517
return Effect.never;
510518
}
519+
const [connectionState, session] = connection;
511520
switch (connectionState.phase) {
512521
case "connected":
513-
return runInEnvironment(target.environmentId, options.execute(target.input));
522+
return Option.isSome(session)
523+
? runInEnvironment(target.environmentId, options.execute(target.input))
524+
: Effect.never;
514525
case "connecting":
515526
case "backoff":
516527
return Effect.never;

0 commit comments

Comments
 (0)