Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions infra/relay/src/environments/ManagedEndpointProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ describe("ManagedEndpointProvider", () => {
}).pipe(Effect.provide(layer));
});

it.effect("does no Cloudflare work when the registered origin is unchanged", () => {
it.effect("only confirms the tunnel exists when the registered origin is unchanged", () => {
const tunnelCalls: TunnelCall[] = [];
const layer = layerProvider(makePersistentTunnelClient(tunnelCalls));

Expand All @@ -1137,7 +1137,31 @@ describe("ManagedEndpointProvider", () => {
endpoint: provisioned.endpoint,
}),
).toBe("ready");
expect(tunnelCalls).toEqual([]);
expect(tunnelCalls).toEqual([{ operation: "get", input: "tunnel-id" }]);
}).pipe(Effect.provide(layer));
});

it.effect("asks for recovery when the recorded tunnel was deleted", () => {
const tunnelCalls: TunnelCall[] = [];
const layer = layerProvider(makePersistentTunnelClient(tunnelCalls));

return Effect.gen(function* () {
const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
const key = { userId: "user_ABC", environmentId: "env_ABC" } as const;
const origin = { localHttpHost: "127.0.0.1", localHttpPort: 3773 } as const;
const provisioned = yield* provider.provision({ ...key, origin });
// A shutdown release deletes the tunnel but keeps the recorded id; the
// host was killed before it dropped its stored config.
expect(yield* provider.release(key)).toBe(true);

expect(
yield* provider.reconcileOrigin({
...key,
tunnelId: provisioned.runtime.tunnelId!,
origin,
endpoint: provisioned.endpoint,
}),
).toBe("recovery_required");
}).pipe(Effect.provide(layer));
});

Expand All @@ -1163,6 +1187,7 @@ describe("ManagedEndpointProvider", () => {
}),
).toBe("ready");
expect(tunnelCalls).toEqual([
{ operation: "get", input: "tunnel-id" },
{
operation: "putConfiguration",
input: {
Expand Down
26 changes: 26 additions & 0 deletions infra/relay/src/environments/ManagedEndpointProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const ManagedEndpointProvisioningStage = Schema.Literals([
"mark-allocation-ready",
"load-allocation",
"verify-endpoint",
"verify-tunnel",
"sync-origin",
]);

Expand Down Expand Up @@ -208,6 +209,7 @@ export interface ManagedEndpointTunnel {
readonly status?: string | null;
readonly createdAt?: string | null;
readonly connsInactiveAt?: string | null;
readonly deletedAt?: string | null;
}

export interface ManagedEndpointTunnelListRequest {
Expand Down Expand Up @@ -581,6 +583,30 @@ export const make = Effect.gen(function* () {
hostname: allocation.hostname,
});
}
// A release keeps the recorded tunnel id, so the record alone cannot
// tell a live tunnel from one deleted by a shutdown whose host was
// killed before it dropped its config. Ask Cloudflare, or the host
// starts a connector that can never connect.
const recorded = yield* tunnels.get(input.tunnelId).pipe(
Effect.asSome,
Effect.catchTags({
ManagedEndpointTunnelClientError: (cause) =>
isManagedEndpointNotFound(cause.cause)
? Effect.succeedNone
: Effect.fail(
new ManagedEndpointProvisioningFailed({
userId: input.userId,
environmentId: input.environmentId,
stage: "verify-tunnel",
tunnelId: input.tunnelId,
cause,
}),
),
}),
);
if (Option.isNone(recorded) || recorded.value.deletedAt) {
return "recovery_required";
}
if (
allocation.origin?.localHttpHost === input.origin.localHttpHost &&
allocation.origin.localHttpPort === input.origin.localHttpPort
Expand Down
Loading