Skip to content

Commit ed154eb

Browse files
WC-3584 Refactor routing to asset or user worker
Centralizing some of these pieces will make it easier to add free tier limiting
1 parent 590d69b commit ed154eb

File tree

1 file changed

+42
-60
lines changed
  • packages/workers-shared/router-worker/src

1 file changed

+42
-60
lines changed

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

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,39 @@ export default {
7777

7878
const maybeSecondRequest = request.clone();
7979

80+
let asset: "static_routing" | "ignored" | true | false = false;
81+
82+
const routeToUserWorker = async () => {
83+
if (!config.has_user_worker) {
84+
throw new Error(
85+
"Fetch for user worker without having a user worker binding"
86+
);
87+
}
88+
analytics.setData({ dispatchtype: DISPATCH_TYPE.WORKER });
89+
userWorkerInvocation = true;
90+
return env.JAEGER.enterSpan("dispatch_worker", async (span) => {
91+
span.setTags({
92+
hasUserWorker: true,
93+
asset: asset,
94+
dispatchType: DISPATCH_TYPE.WORKER,
95+
});
96+
return env.USER_WORKER.fetch(maybeSecondRequest);
97+
});
98+
};
99+
100+
const routeToAssets = async () => {
101+
analytics.setData({ dispatchtype: DISPATCH_TYPE.ASSETS });
102+
return await env.JAEGER.enterSpan("dispatch_assets", async (span) => {
103+
span.setTags({
104+
hasUserWorker: config.has_user_worker,
105+
asset: asset,
106+
dispatchType: DISPATCH_TYPE.ASSETS,
107+
});
108+
109+
return env.ASSET_WORKER.fetch(maybeSecondRequest);
110+
});
111+
};
112+
80113
if (config.static_routing) {
81114
// evaluate "exclude" rules
82115
const excludeRulesMatcher = generateStaticRoutingRuleMatcher(
@@ -89,18 +122,10 @@ export default {
89122
) {
90123
// direct to asset worker
91124
analytics.setData({
92-
dispatchtype: DISPATCH_TYPE.ASSETS,
93125
staticRoutingDecision: STATIC_ROUTING_DECISION.ROUTED,
94126
});
95-
return await env.JAEGER.enterSpan("dispatch_assets", async (span) => {
96-
span.setTags({
97-
hasUserWorker: config.has_user_worker,
98-
asset: "static_routing",
99-
dispatchType: DISPATCH_TYPE.ASSETS,
100-
});
101-
102-
return env.ASSET_WORKER.fetch(maybeSecondRequest);
103-
});
127+
asset = "static_routing";
128+
return await routeToAssets();
104129
}
105130
// evaluate "include" rules
106131
const includeRulesMatcher = generateStaticRoutingRuleMatcher(
@@ -118,19 +143,10 @@ export default {
118143
}
119144
// direct to user worker
120145
analytics.setData({
121-
dispatchtype: DISPATCH_TYPE.WORKER,
122146
staticRoutingDecision: STATIC_ROUTING_DECISION.ROUTED,
123147
});
124-
return await env.JAEGER.enterSpan("dispatch_worker", async (span) => {
125-
span.setTags({
126-
hasUserWorker: true,
127-
asset: "static_routing",
128-
dispatchType: DISPATCH_TYPE.WORKER,
129-
});
130-
131-
userWorkerInvocation = true;
132-
return env.USER_WORKER.fetch(maybeSecondRequest);
133-
});
148+
asset = "static_routing";
149+
return await routeToUserWorker();
134150
}
135151

136152
analytics.setData({
@@ -143,53 +159,19 @@ export default {
143159
// User's configuration indicates they want user-Worker to run ahead of any
144160
// assets. Do not provide any fallback logic.
145161
if (config.invoke_user_worker_ahead_of_assets) {
146-
if (!config.has_user_worker) {
147-
throw new Error(
148-
"Fetch for user worker without having a user worker binding"
149-
);
150-
}
151-
152-
analytics.setData({ dispatchtype: DISPATCH_TYPE.WORKER });
153-
return await env.JAEGER.enterSpan("dispatch_worker", async (span) => {
154-
span.setTags({
155-
hasUserWorker: true,
156-
asset: "ignored",
157-
dispatchType: DISPATCH_TYPE.WORKER,
158-
});
159-
160-
userWorkerInvocation = true;
161-
return env.USER_WORKER.fetch(maybeSecondRequest);
162-
});
162+
asset = "ignored";
163+
return await routeToUserWorker();
163164
}
164165

165166
// If we have a user-Worker, but no assets, dispatch to Worker script
166167
const assetsExist = await env.ASSET_WORKER.unstable_canFetch(request);
168+
asset = assetsExist;
167169
if (config.has_user_worker && !assetsExist) {
168-
analytics.setData({ dispatchtype: DISPATCH_TYPE.WORKER });
169-
170-
return await env.JAEGER.enterSpan("dispatch_worker", async (span) => {
171-
span.setTags({
172-
hasUserWorker: config.has_user_worker,
173-
asset: assetsExist,
174-
dispatchType: DISPATCH_TYPE.WORKER,
175-
});
176-
177-
userWorkerInvocation = true;
178-
return env.USER_WORKER.fetch(maybeSecondRequest);
179-
});
170+
return await routeToUserWorker();
180171
}
181172

182173
// Otherwise, we either don't have a user worker, OR we have matching assets and should fetch from the assets binding
183-
analytics.setData({ dispatchtype: DISPATCH_TYPE.ASSETS });
184-
return await env.JAEGER.enterSpan("dispatch_assets", async (span) => {
185-
span.setTags({
186-
hasUserWorker: config.has_user_worker,
187-
asset: assetsExist,
188-
dispatchType: DISPATCH_TYPE.ASSETS,
189-
});
190-
191-
return env.ASSET_WORKER.fetch(maybeSecondRequest);
192-
});
174+
return await routeToAssets();
193175
} catch (err) {
194176
if (userWorkerInvocation) {
195177
// Don't send user Worker errors to sentry; we have no way to distinguish between

0 commit comments

Comments
 (0)