From d95a8f92686b1d15e35f4eb612803e4dc1743eb7 Mon Sep 17 00:00:00 2001 From: Rui Chen Date: Sat, 24 Oct 2026 16:15:58 -0400 Subject: [PATCH 1/2] fix(s3): make the socket pool size configurable and apply it to http endpoints The S3 client's agent pool was hard-coded at 50 sockets, and the agent was passed only as `httpsAgent`. NodeHttpHandler picks the agent by endpoint scheme, so an http:// endpoint (MinIO, RustFS, an in-cluster gateway) silently fell back to the SDK's default agent and ignored the configured pool. Local tests and benchmarks against RustFS therefore never exercised the pool an https deployment uses. Add STORAGE_S3_MAX_SOCKETS (default 50, the previous value) and build both the http and the https agent from the same options. tests/s3-socket-pool.test.ts holds one GetObject body unread with a pool of one socket and checks that a second GetObject waits for it; against an http:// endpoint it did not wait before this change. Signed-off-by: Rui Chen --- README.md | 8 +++++++ lib/schemas.ts | 1 + lib/storage.ts | 16 +++++++++----- tests/s3-socket-pool.test.ts | 41 ++++++++++++++++++++++++++++++++++++ tests/setup.ts | 1 + 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 tests/s3-socket-pool.test.ts diff --git a/README.md b/README.md index 90307017..b276ff33 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,14 @@ volumes: cache-data: ``` +## Resource limits + +All optional. + +| Variable | Default | Effect | +| ------------------------ | ------- | ----------------------------------------------------------------------------- | +| `STORAGE_S3_MAX_SOCKETS` | `50` | Sockets per S3 connection pool, for `http://` and `https://` endpoints alike. | + ## Documentation 👉 👈 diff --git a/lib/schemas.ts b/lib/schemas.ts index bbae5f53..1fa283fe 100644 --- a/lib/schemas.ts +++ b/lib/schemas.ts @@ -5,6 +5,7 @@ export const envStorageDriverSchema = type.or( 'STORAGE_DRIVER': type.unit('s3'), 'STORAGE_S3_BUCKET': 'string', 'STORAGE_S3_SOCKET_TIMEOUT_MS': 'number.integer >= 0 = 10000', + 'STORAGE_S3_MAX_SOCKETS': 'number.integer >= 1 = 50', 'AWS_REGION': "string = 'us-east-1'", 'AWS_ENDPOINT_URL?': 'string.url', 'AWS_ACCESS_KEY_ID?': 'string', diff --git a/lib/storage.ts b/lib/storage.ts index d40ce618..383e262e 100644 --- a/lib/storage.ts +++ b/lib/storage.ts @@ -8,7 +8,8 @@ import { randomUUID } from 'node:crypto' import { once } from 'node:events' import { createReadStream, createWriteStream } from 'node:fs' import fs from 'node:fs/promises' -import { Agent } from 'node:https' +import { Agent as HttpAgent } from 'node:http' +import { Agent as HttpsAgent } from 'node:https' import path from 'node:path' import { PassThrough, Readable } from 'node:stream' import { pipeline } from 'node:stream/promises' @@ -880,16 +881,21 @@ function accumulateFolder( class S3Adapter implements StorageAdapter { static async fromEnv(env: Extract) { const bucket = env.STORAGE_S3_BUCKET - const agent = new Agent({ + // Both protocols get the configured pool: NodeHttpHandler picks the agent by + // endpoint scheme, and an http:// endpoint (MinIO, RustFS, an in-cluster + // gateway) would otherwise fall back to the SDK's default agent and ignore + // STORAGE_S3_MAX_SOCKETS. + const agentOptions = { keepAlive: true, - maxSockets: 50, + maxSockets: env.STORAGE_S3_MAX_SOCKETS, keepAliveMsecs: 1000, - }) + } const s3 = new S3Client({ forcePathStyle: true, region: env.AWS_REGION, requestHandler: new NodeHttpHandler({ - httpsAgent: agent, + httpAgent: new HttpAgent(agentOptions), + httpsAgent: new HttpsAgent(agentOptions), socketTimeout: env.STORAGE_S3_SOCKET_TIMEOUT_MS, }), }) diff --git a/tests/s3-socket-pool.test.ts b/tests/s3-socket-pool.test.ts new file mode 100644 index 00000000..864f9374 --- /dev/null +++ b/tests/s3-socket-pool.test.ts @@ -0,0 +1,41 @@ +import type { Env } from '~/lib/schemas' +import { randomUUID } from 'node:crypto' +import { Readable } from 'node:stream' +import { setTimeout as sleep } from 'node:timers/promises' + +import { afterEach, describe, expect, test } from 'vitest' +import { env } from '~/lib/env' +import { Storage } from '~/lib/storage' + +const s3Env = env as Extract + +// The test endpoint is http://, so this also covers the agent NodeHttpHandler +// picks for plain-http endpoints (MinIO, RustFS, in-cluster gateways). +describe.skipIf(process.env.VITEST_STORAGE_DRIVER !== 's3')('s3 socket pool', () => { + const originalMaxSockets = s3Env.STORAGE_S3_MAX_SOCKETS + afterEach(() => { + s3Env.STORAGE_S3_MAX_SOCKETS = originalMaxSockets + }) + + test('the configured pool size bounds the sockets used for an http endpoint', async () => { + s3Env.STORAGE_S3_MAX_SOCKETS = 1 + const storage = await Storage.fromEnv() + const folder = `socket-pool-${randomUUID()}` + await storage.adapter.uploadStream(`${folder}/a`, Readable.from([Buffer.from('a')])) + await storage.adapter.uploadStream(`${folder}/b`, Readable.from([Buffer.from('b')])) + + try { + // An unread body keeps its socket: the only one in the pool. + const held = await storage.adapter.createDownloadStream(`${folder}/a`) + const second = storage.adapter.createDownloadStream(`${folder}/b`) + const queued = await Promise.race([second.then(() => false), sleep(1000, true)]) + expect(queued, 'second GET queued for the only socket').toBe(true) + + held.stream.destroy() + const { stream } = await second + expect(Buffer.concat(await stream.toArray()).toString()).toBe('b') + } finally { + await storage.adapter.deleteFolder(folder) + } + }) +}) diff --git a/tests/setup.ts b/tests/setup.ts index 5ca471ec..75f8c9ed 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -85,6 +85,7 @@ const TESTING_ENV_BY_STORAGE_DRIVER = { AWS_REGION: 'us-east-1', STORAGE_S3_BUCKET: 'vitest', STORAGE_S3_SOCKET_TIMEOUT_MS: 10_000, + STORAGE_S3_MAX_SOCKETS: 50, AWS_ACCESS_KEY_ID: 'rustfsadmin', AWS_SECRET_ACCESS_KEY: 'rustfsadmin', AWS_ENDPOINT_URL: 'http://localhost:9000', From 2e34e04555b73d0563c302b46ed13c20b77a7682 Mon Sep 17 00:00:00 2001 From: Rui Chen Date: Sat, 24 Oct 2026 17:08:57 -0400 Subject: [PATCH 2/2] fix(s3): require at least two sockets in the shared pool A streaming merge holds a part GetObject while its multipart upload waits for a socket from the same pool, so a pool of one stalls the first restore of any unmerged entry. Reject it, note in the README that a shared pool smaller than the number of concurrent merges can stall them, and exercise the bound with two held reads. Signed-off-by: Rui Chen --- README.md | 2 ++ lib/schemas.ts | 2 +- tests/s3-socket-pool.test.ts | 26 +++++++++++++++----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b276ff33..aef595a1 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ All optional. | ------------------------ | ------- | ----------------------------------------------------------------------------- | | `STORAGE_S3_MAX_SOCKETS` | `50` | Sockets per S3 connection pool, for `http://` and `https://` endpoints alike. | +Reads and writes share this pool. A streaming merge holds a part read while its upload waits for a socket, so the minimum is 2, and a pool smaller than the number of concurrent merges can stall them. + ## Documentation 👉 👈 diff --git a/lib/schemas.ts b/lib/schemas.ts index 1fa283fe..0e507fda 100644 --- a/lib/schemas.ts +++ b/lib/schemas.ts @@ -5,7 +5,7 @@ export const envStorageDriverSchema = type.or( 'STORAGE_DRIVER': type.unit('s3'), 'STORAGE_S3_BUCKET': 'string', 'STORAGE_S3_SOCKET_TIMEOUT_MS': 'number.integer >= 0 = 10000', - 'STORAGE_S3_MAX_SOCKETS': 'number.integer >= 1 = 50', + 'STORAGE_S3_MAX_SOCKETS': 'number.integer >= 2 = 50', 'AWS_REGION': "string = 'us-east-1'", 'AWS_ENDPOINT_URL?': 'string.url', 'AWS_ACCESS_KEY_ID?': 'string', diff --git a/tests/s3-socket-pool.test.ts b/tests/s3-socket-pool.test.ts index 864f9374..fc0ca274 100644 --- a/tests/s3-socket-pool.test.ts +++ b/tests/s3-socket-pool.test.ts @@ -18,22 +18,26 @@ describe.skipIf(process.env.VITEST_STORAGE_DRIVER !== 's3')('s3 socket pool', () }) test('the configured pool size bounds the sockets used for an http endpoint', async () => { - s3Env.STORAGE_S3_MAX_SOCKETS = 1 + s3Env.STORAGE_S3_MAX_SOCKETS = 2 const storage = await Storage.fromEnv() const folder = `socket-pool-${randomUUID()}` - await storage.adapter.uploadStream(`${folder}/a`, Readable.from([Buffer.from('a')])) - await storage.adapter.uploadStream(`${folder}/b`, Readable.from([Buffer.from('b')])) + for (const name of ['a', 'b', 'c']) + await storage.adapter.uploadStream(`${folder}/${name}`, Readable.from([Buffer.from(name)])) try { - // An unread body keeps its socket: the only one in the pool. - const held = await storage.adapter.createDownloadStream(`${folder}/a`) - const second = storage.adapter.createDownloadStream(`${folder}/b`) - const queued = await Promise.race([second.then(() => false), sleep(1000, true)]) - expect(queued, 'second GET queued for the only socket').toBe(true) + // An unread body keeps its socket: two of them fill the pool. + const held = [ + await storage.adapter.createDownloadStream(`${folder}/a`), + await storage.adapter.createDownloadStream(`${folder}/b`), + ] + const third = storage.adapter.createDownloadStream(`${folder}/c`) + const queued = await Promise.race([third.then(() => false), sleep(1000, true)]) + expect(queued, 'third GET queued for a full pool').toBe(true) - held.stream.destroy() - const { stream } = await second - expect(Buffer.concat(await stream.toArray()).toString()).toBe('b') + held[0]!.stream.destroy() + const { stream } = await third + expect(Buffer.concat(await stream.toArray()).toString()).toBe('c') + held[1]!.stream.destroy() } finally { await storage.adapter.deleteFolder(folder) }