Skip to content

Commit 2b711ee

Browse files
committed
Sync bootstrap code
1 parent b6744fe commit 2b711ee

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/bootstrap/feature_flags.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export enum FeatureFlag {
99
ForwardCDNLoop = "edge_functions_bootstrap_forward_cdn_loop",
1010
InvocationTimeout = "edge_functions_bootstrap_invocation_timeout",
1111
ForceHTTP11 = "edge_functions_bootstrap_force_http11",
12+
UseOneClientPoolPerIsolate =
13+
"edge_functions_bootstrap_use_one_client_pool_per_isolate",
1214
}
1315

1416
export const hasFlag = (req: EdgeRequest, flag: FeatureFlag) => {

src/bootstrap/handler.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import { Router } from "./router.ts";
2020
import type { Functions } from "./stage_2.ts";
2121
import { ErrorType, PassthroughError, UserError } from "./util/errors.ts";
2222
import "./globals/types.ts";
23-
import { patchFetchToForceHTTP11 } from "./util/fetch.ts";
23+
import {
24+
patchFetchToForceHTTP11,
25+
patchFetchToHaveItsOwnConnectionPoolPerIsolate,
26+
} from "./util/fetch.ts";
2427

2528
interface HandleRequestOptions {
2629
fetchRewrites?: Map<string, string>;
@@ -68,6 +71,17 @@ export const handleRequest = async (
6871
req.headers.get(InternalHeaders.FeatureFlags),
6972
);
7073

74+
// If the `UseOneClientPoolPerIsolate` feature flag is enabled, we patch the
75+
// fetch to use its own connection pool.
76+
if (featureFlags[FeatureFlag.UseOneClientPoolPerIsolate]) {
77+
// this is not incuded in the `patchGlobals` function because that function
78+
// is invoked before we have access to the feature flags. once this is fully
79+
// rolled out, we will want to move this into `patchGlobals`
80+
globalThis.fetch = patchFetchToHaveItsOwnConnectionPoolPerIsolate(
81+
globalThis.fetch,
82+
);
83+
}
84+
7185
// if ForceHTTP11 is enabled, we patch the fetch to enforce HTTP/1.1
7286
if (featureFlags[FeatureFlag.ForceHTTP11]) {
7387
// this is not incuded in the `patchGlobals` function because that function

src/bootstrap/util/fetch.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,24 @@ export const patchFetchToForceHTTP11 = (
163163
return rawFetch(input, { ...init, client });
164164
};
165165
};
166+
167+
// We currently see issues with some requests on Deno and the current thinking is
168+
// something in the H2 client is broken and the client is entering a weird state
169+
// and either cannot get or lose a connection from the pool. This means that sometimes
170+
// a fetch doesn't reach us at all, it can't establish connection.
171+
// To mitigate this, we patch the fetch to use its own connection pool
172+
// so that it doesn't used the wider shared pool within Deno.
173+
let client: Deno.HttpClient;
174+
export let isClientPatched = false;
175+
export const patchFetchToHaveItsOwnConnectionPoolPerIsolate = (
176+
rawFetch: typeof globalThis.fetch,
177+
) => {
178+
if (isClientPatched) {
179+
return rawFetch;
180+
}
181+
isClientPatched = true;
182+
client = Deno.createHttpClient({});
183+
return (input: URL | Request | string, init?: RequestInit) => {
184+
return rawFetch(input, { ...init, client });
185+
};
186+
};

0 commit comments

Comments
 (0)