diff --git a/.changeset/envd-connection-limit-env-var.md b/.changeset/envd-connection-limit-env-var.md new file mode 100644 index 0000000000..87bf348dbb --- /dev/null +++ b/.changeset/envd-connection-limit-env-var.md @@ -0,0 +1,5 @@ +--- +"e2b": patch +--- + +Expose `E2B_ENVD_CONNECTIONS` environment variable to configure the envd REST file API connection pool limit (default remains 10). Previously the connection count was hardcoded with no runtime override, unlike the RPC transport which already supported `E2B_ENVD_RPC_CONNECTIONS`. diff --git a/packages/js-sdk/src/envd/http2.ts b/packages/js-sdk/src/envd/http2.ts index 1de93db57c..271fa7a2a3 100644 --- a/packages/js-sdk/src/envd/http2.ts +++ b/packages/js-sdk/src/envd/http2.ts @@ -90,9 +90,8 @@ export function createEnvdFetch(proxy?: string): typeof fetch { return cached } - // Keep one origin connection for short envd REST calls. If ALPN falls back - // to h1, this favors connection pressure over per-sandbox throughput. const envdFetch = createEnvdFetchForRuntime(runtime, { + connectionLimit: getEnvdConnectionLimit(), inflightLimit: getEnvdInflightLimit(), proxy, }) @@ -119,6 +118,13 @@ export function createEnvdRpcFetch(proxy?: string): typeof fetch { return envdRpcFetch } +export function getEnvdConnectionLimit(): number { + return parsePositiveIntEnv( + 'E2B_ENVD_CONNECTIONS', + DEFAULT_ENVD_CONNECTION_LIMIT + ) +} + export function getEnvdRpcConnectionLimit(): number { return parsePositiveIntEnv( 'E2B_ENVD_RPC_CONNECTIONS', diff --git a/packages/js-sdk/tests/envd/http2.test.ts b/packages/js-sdk/tests/envd/http2.test.ts index 01ca903553..39251961f5 100644 --- a/packages/js-sdk/tests/envd/http2.test.ts +++ b/packages/js-sdk/tests/envd/http2.test.ts @@ -5,6 +5,7 @@ afterEach(() => { vi.resetModules() vi.doUnmock('undici') vi.doUnmock('../../src/utils') + delete process.env.E2B_ENVD_CONNECTIONS delete process.env.E2B_ENVD_RPC_CONNECTIONS delete process.env.E2B_ENVD_INFLIGHT_REQUESTS delete process.env.E2B_ENVD_RPC_INFLIGHT_REQUESTS @@ -158,6 +159,22 @@ test('can create a bounded dispatcher for RPC streams', async () => { expect(agents).toEqual([{ allowH2: true, connections: 100 }]) }) +test('reads envd connection limit from env', async () => { + process.env.E2B_ENVD_CONNECTIONS = '50' + + const { getEnvdConnectionLimit } = await import('../../src/envd/http2') + + expect(getEnvdConnectionLimit()).toBe(50) +}) + +test('getEnvdConnectionLimit throws on malformed env value', async () => { + process.env.E2B_ENVD_CONNECTIONS = 'bogus' + + const { getEnvdConnectionLimit } = await import('../../src/envd/http2') + + expect(() => getEnvdConnectionLimit()).toThrow(/E2B_ENVD_CONNECTIONS/) +}) + test('reads RPC stream dispatcher connection limit from env', async () => { process.env.E2B_ENVD_RPC_CONNECTIONS = '200'