Skip to content

fix(cloudflare/workers): scope Worker read route discovery to known zones - #944

Merged
sam-goodwin merged 1 commit into
alchemy-run:mainfrom
benpsnyder:fix/worker-read-zone-fanout
Jul 27, 2026
Merged

sam-goodwin merged 1 commit into
alchemy-run:mainfrom
benpsnyder:fix/worker-read-zone-fanout

Conversation

@benpsnyder

Copy link
Copy Markdown
Contributor

Worker read discovered zone routes by enumerating every zone on the account and then listing routes in each one. Routes are zone-scoped with no account-level enumeration API, so that is O(zones) requests on every read, issued at concurrency: "unbounded". On an account with a few hundred zones a couple of plan/deploy cycles exhausts Cloudflare's per-user API budget and unrelated calls start coming back 429 / code 971 (#926).

reconcileRoutes already avoids the sweep — it scopes itself to the zones implied by desired ∪ previous. read now uses the same bounded zone set.

-const readWorkerRoutes = (scriptName: string) =>
-  Effect.gen(function* () {
-    const { accountId } = yield* yield* CloudflareEnvironment;
-    const accountZones = yield* listAllZones(accountId).pipe(
-      Effect.catch(() => Effect.succeed([])),
-    );
-    return yield* listWorkerRoutesInZones(
-      scriptName,
-      accountZones.map((zone) => zone.id),
-    );
-  });
+const readWorkerRoutes = (
+  scriptName: string,
+  knownRoutes: Worker["Attributes"]["routes"] | undefined,
+) =>
+  listWorkerRoutesInZones(
+    scriptName,
+    (knownRoutes ?? []).map((route) => route.zoneId),
+  );

listWorkerRoutesInZones is also bounded now, since it still fans out one request per zone on the reconcile path.

This is complementary to #932 rather than a replacement. That PR gates the sweep on whether Alchemy manages routes for the Worker, which removes the cost for a plain workers.dev Worker; this removes the cost for a Worker that actually declares routes, where the gate passes and the account-wide fan-out still runs unchanged.

Trade-off

A route attached out of band in a zone this Worker has never had a route in is no longer discovered by read. That is the blind spot reconcileRoutes already has, so the two paths are now consistent rather than one of them paying for account-wide discovery on every read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants