Skip to content
Myr

Read the front page's listing in each request that needs it, never joining another request's read

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The Project DO adapter kept its listing of made projects with Effect.cached, built once per isolate. Every call that met a project name not yet known while the first read was under way joined that read's fiber, which ran on the first request's I/O. On Workers a request that awaits another request's I/O is taken for hung and cancelled, so a fresh isolate hit by a fan-out (the Code tab's per-path log reads, about fifteen at once) cancelled every request but the first with "code had hung and would never generate a response", and the web's folder page answered 500. A first request whose client left would have left the run unfinished for the isolate's life. Each request now reads the listing itself until one has kept it; the kept names still spare the probe, as before. The test holds the first read open forever and asks again: before the fix the second call waited on it.

authored by oscabriel · committed by oscabriel · parent ef0ddbd · Browse files · 2 files+82 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+82 added−17 removed

  • tests:+58 added−1 removed
  • other:+24 added−16 removed

2 files+82 added−17 removed

modified:apps/platform/src/adapters/do-projects.ts+24 added-16 removed
136 unmodified lines
137 Projects,138 Effect.gen(function* () {139 const frontPage = yield* FrontPage;140 const known = yield* Ref.make<ReadonlySet<ProjectName>>(new Set());141 const seeded = yield* Ref.make(false);142142 // Every project the front page lists was made, so its listing, read once143 // per isolate on the first name not yet known, stands in for a probe of144 // each: a fan-out over every listed project then wakes each DO once, for145 // its own call alone. A name the listing lacks is probed, as before.146 const listed = yield* Effect.cached(147 Effect.map(148 frontPage.projects(),149 (listing) => new Set(listing.map(({ project }) => project))150 )151 );143 // Every project the front page lists was made, so its listing, kept once144 // per isolate when a name is not yet known, stands in for a probe of each:145 // a fan-out over every listed project then wakes each DO once, for its own146 // call alone. A name the listing lacks is probed, as before. Each request147 // reads the listing itself until one has kept it, and never joins another148 // request's read: the runtime takes a request that awaits another's I/O149 // for hung and cancels it, and a request whose client leaves is cancelled150 // where it stands, so a read it started may never end.151 const listed = Effect.gen(function* () {152 if (yield* Ref.get(seeded)) {153 return yield* Ref.get(known);154 }155156 const listing = yield* frontPage.projects();157158 yield* Ref.set(seeded, true);159160 return yield* Ref.updateAndGet(161 known,162 (all) => new Set([...all, ...listing.map(({ project }) => project)])163 );164 });165166 const isMade = (project: ProjectName) =>167 Effect.gen(function* () {168 if ((yield* Ref.get(known)).has(project)) {169 return true;170 }171172 const names = yield* listed;173161 const seeded = yield* Ref.updateAndGet(162 known,163 (all) => new Set([...all, ...names])164 );165166 if (seeded.has(project)) {174 if (names.has(project)) {175 return true;176 }177178 const found = yield* remote(namespace, project)("listAsks", [
29 unmodified lines
modified:apps/platform/test/do-projects.test.ts+58 added-1 removed
1import { describe, expect, expectTypeOf, it } from "@effect/vitest";2import { LogRef } from "@repo/checks/evidence-store";3import { envSchemaMethod } from "@repo/project/deciders";4import { DraftResult } from "@repo/project/draft-runs";5import { FrontPage } from "@repo/project/front-page";6import { FrontPageMemory } from "@repo/project/front-page/memory";7import { LandingCommand } from "@repo/project/landing";8import type { ProjectApi } from "@repo/project/project";9import { Projects } from "@repo/project/projects";
15 unmodified lines
25 ProjectExists,26 UnknownProject,27} from "@repo/protocol/refusal";28import { advanceTo } from "@repo/testkit/clock";28import { Effect, Layer, Option, Redacted, Schema } from "effect";29import { Effect, Fiber, Layer, Option, Redacted, Ref, Schema } from "effect";3031import { DoProjects } from "../src/adapters/do-projects.ts";32import type { ProjectNamespace } from "../src/adapters/do-projects.ts";33import {
299 unmodified lines
333 ]);334 }).pipe(Effect.provide(world))335 );336337 // On Workers a request that awaits another request's I/O is cancelled as hung,338 // and one whose client left is cancelled where it stands: its read never ends.339 it.live(340 "never has a request wait on the listing another request is reading",341 () =>342 Effect.gen(function* () {343 const { namespace } = yield* namespaceOf;344 const reads = yield* Ref.make(0);345346 const listing = Layer.effect(347 FrontPage,348 Effect.gen(function* () {349 const page = yield* FrontPage;350351 return FrontPage.of({352 ...page,353 projects: () =>354 Effect.flatMap(355 Ref.getAndUpdate(reads, (count) => count + 1),356 (count) => (count === 0 ? Effect.never : page.projects())357 ),358 });359 })360 ).pipe(361 Layer.provide(362 FrontPageMemory.layer({363 credits: [],364 entries: [],365 projects: [{ openAsks: 0, posture: "open", project }],366 })367 )368 );369370 const outcome = yield* Effect.gen(function* () {371 const projects = yield* Projects;372 const abandoned = yield* Effect.forkChild(projects.get(project));373374 yield* Effect.yieldNow;375376 const later = yield* Effect.timeoutOption(377 Effect.as(projects.get(project), "answered"),378 "2 seconds"379 );380381 yield* Fiber.interrupt(abandoned);382383 return later;384 }).pipe(385 Effect.provide(DoProjects(namespace).pipe(Layer.provide(listing)))386 );387388 expect(outcome).toStrictEqual(Option.some("answered"));389 expect(yield* Ref.get(reads)).toBe(2);390 }).pipe(Effect.provide(world))391 );392393 it.effect(394 "answers every ProjectApi method as the project's service does in process",395 () =>396 Effect.gen(function* () {
29 unmodified lines