Skip to content

Commit

Permalink
sort batch of requests in queue by order of arrival
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Feb 4, 2024
1 parent f0377b7 commit 3329f19
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/wrangler/templates/startDevWorker/ProxyWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export class ProxyWorker implements DurableObject {
requestQueue: Array<{
request: Request;
deferredResponse: DeferredPromise<Response>;
order: number;
}> = [];

#counter = 0;
fetch(request: Request) {
if (isRequestForLiveReloadWebsocket(request)) {
// requests for live-reload websocket
Expand All @@ -57,7 +59,11 @@ export class ProxyWorker implements DurableObject {
// regular requests to be proxied
const deferredResponse = createDeferred<Response>();

this.requestQueue.push({ request, deferredResponse });
this.requestQueue.push({
request,
deferredResponse,
order: this.#counter++,
});
this.processQueue();

return deferredResponse.promise;
Expand Down Expand Up @@ -101,7 +107,13 @@ export class ProxyWorker implements DurableObject {
const { proxyData } = this; // store proxyData at the moment this function was called
if (proxyData === undefined) return;

for (const { request, deferredResponse } of this.requestQueue.splice(0)) {
const queue = this.requestQueue.splice(0);

// we can't guarantee some requests won't get retried in a later batch
// but we can at least order the current batch
queue.sort((a, b) => a.order - b.order);

for (const { request, deferredResponse, order } of queue) {
const userWorkerUrl = new URL(request.url);
const headers = new Headers(request.headers);

Expand Down Expand Up @@ -161,7 +173,7 @@ export class ProxyWorker implements DurableObject {

// if the request can be retried (subset of idempotent requests which have no body), requeue it
else if (request.method === "GET" || request.method === "HEAD") {
this.requestQueue.unshift({ request, deferredResponse });
this.requestQueue.unshift({ request, deferredResponse, order });
}

// if the request cannot be retried, respond with 503 Service Unavailable
Expand Down

0 comments on commit 3329f19

Please sign in to comment.