Skip to content

Commit e216a76

Browse files
Router worker performs free tier limiting (#9661)
* WC-3584 Refactor routing to asset or user worker Centralizing some of these pieces will make it easier to add free tier limiting * WC-3584 Add eyeball routing config Provided via a new optional param binding. When provided, it contains info about free tier limiting for this account. * WC-3584 Deny requests to the user worker when over free tier limits Returns an html page (included as direct module, via esbuild) with a 429 status code, mirroring the current free tier limiting behavior. It also sets a new field in our analytics so we can observe free tier invocation denials. This page can evolve as we need it to, this is just my first pass at the response. * WC-3584 Add changeset * WC-3584 Move the limited response page to be inlined There's no loader configured for html files by default in wrangler it seems. So rather than mess with loaders or a custom build, inlining this page is easy enough. This pages copies what fl returns exactly, and there's a few templated things to reproduce that. Namely: the hostname in the title of the page, the ip address of the eyeball, and the current date (with some formatting)
1 parent 3d32a4e commit e216a76

File tree

8 files changed

+1763
-113
lines changed

8 files changed

+1763
-113
lines changed

.changeset/tidy-bobcats-lose.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/workers-shared": minor
3+
---
4+
5+
Limit free tier requests in the Router worker

packages/workers-shared/router-worker/src/analytics.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type Data = {
3535
staticRoutingDecision?: STATIC_ROUTING_DECISION;
3636
// double7 - Whether the request was blocked by abuse mitigation or not
3737
abuseMitigationBlocked?: boolean;
38+
// double8 - User worker invocation denied due to free tier limiting
39+
userWorkerFreeTierLimiting?: boolean;
3840

3941
// -- Blobs --
4042
// blob1 - Hostname of the request
@@ -93,6 +95,7 @@ export class Analytics {
9395
: Number(this.data.userWorkerAhead),
9496
this.data.staticRoutingDecision ?? STATIC_ROUTING_DECISION.NOT_PROVIDED, // double6
9597
this.data.abuseMitigationBlocked ? 1 : 0, // double7
98+
this.data.userWorkerFreeTierLimiting ? 1 : 0, // double8
9699
],
97100
blobs: [
98101
this.data.hostname?.substring(0, 256), // blob1 - trim to 256 bytes

packages/workers-shared/router-worker/src/configuration.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import type { RouterConfig } from "../../utils/types";
1+
import type { EyeballRouterConfig, RouterConfig } from "../../utils/types";
22

3-
export const applyConfigurationDefaults = (
3+
type RequiredEyeballRouterConfig = Required<Exclude<EyeballRouterConfig, null>>;
4+
5+
export const applyRouterConfigDefaults = (
46
configuration?: RouterConfig
57
): Required<RouterConfig> => {
68
return {
@@ -15,3 +17,11 @@ export const applyConfigurationDefaults = (
1517
},
1618
};
1719
};
20+
21+
export const applyEyeballConfigDefaults = (
22+
eyeballConfiguration?: EyeballRouterConfig
23+
): RequiredEyeballRouterConfig => {
24+
return {
25+
limitedAssetsOnly: eyeballConfiguration?.limitedAssetsOnly ?? false,
26+
};
27+
};

0 commit comments

Comments
 (0)