Skip to content
Closed
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
52 changes: 40 additions & 12 deletions apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,17 @@ describe("DesktopLocalEnvironmentAuth", () => {
},
]),
} as unknown as DesktopBackendPool.DesktopBackendPool["Service"]);
const makeExchange = (failures: number, failure: () => Response) =>
const makeExchange = (failures: number, failure: () => Effect.Effect<Response>) =>
Effect.gen(function* () {
const requestCount = yield* Ref.make(0);
const layerHttpClient = Layer.succeed(
HttpClient.HttpClient,
HttpClient.make((request) =>
Ref.updateAndGet(requestCount, (count) => count + 1).pipe(
Effect.map((count) =>
Effect.flatMap((count) =>
count <= failures
? HttpClientResponse.fromWeb(request, failure())
: tokenResponse(request),
? Effect.map(failure(), (response) => HttpClientResponse.fromWeb(request, response))
: Effect.succeed(tokenResponse(request)),
),
),
),
Expand All @@ -195,17 +195,45 @@ describe("DesktopLocalEnvironmentAuth", () => {
return { auth, requestCount };
});

it.effect("keeps retrying while the backend is too busy to answer", () =>
Effect.gen(function* () {
const { auth, requestCount } = yield* makeExchange(3, () => Effect.never);

const fiber = yield* auth.getBearerToken.pipe(Effect.forkChild);
yield* TestClock.adjust(Duration.seconds(40));
const token = yield* Fiber.join(fiber);

assert.strictEqual(token, "desktop-bearer-token");
assert.strictEqual(yield* Ref.get(requestCount), 4);
}).pipe(Effect.provide(TestClock.layer())),
);

it.effect("gives up after a minute when the backend never answers", () =>
Effect.gen(function* () {
const { auth, requestCount } = yield* makeExchange(Infinity, () => Effect.never);

const fiber = yield* auth.getBearerToken.pipe(Effect.flip, Effect.forkChild);
yield* TestClock.adjust(Duration.seconds(60));
const error = yield* Fiber.join(fiber);

assert.strictEqual(error._tag, "DesktopLocalEnvironmentAuthSessionBootstrapError");
assert.isAbove(yield* Ref.get(requestCount), 1);
}).pipe(Effect.provide(TestClock.layer())),
);

it.effect("does not retry a rejected bootstrap credential", () =>
Effect.gen(function* () {
const { auth, requestCount } = yield* makeExchange(1, () =>
Response.json(
{
_tag: "EnvironmentAuthInvalidError",
code: "auth_invalid",
reason: "invalid_credential",
traceId: "trace-1",
},
{ status: 401 },
Effect.succeed(
Response.json(
{
_tag: "EnvironmentAuthInvalidError",
code: "auth_invalid",
reason: "invalid_credential",
traceId: "trace-1",
},
{ status: 401 },
),
),
);

Expand Down
5 changes: 4 additions & 1 deletion apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import * as HttpClient from "effect/http/HttpClient";

import * as DesktopBackendPool from "./DesktopBackendPool.ts";

const BOOTSTRAP_TRANSIENT_RETRY_TIMEOUT = Duration.seconds(15);
// Each token request gives up after 10 s, and a backend that just became ready
// can stall for half a minute on a large state database. The budget has to
// cover several of those attempts, or a slow startup becomes a fatal error.
const BOOTSTRAP_TRANSIENT_RETRY_TIMEOUT = Duration.seconds(60);
const BOOTSTRAP_TRANSIENT_RETRY_INTERVAL = Duration.millis(500);
const TRANSIENT_BOOTSTRAP_STATUS_CODES = new Set([502, 503, 504]);

Expand Down
Loading