Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ 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. |

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

👉 <https://gha-cache-server.falcondev.io/getting-started> 👈
1 change: 1 addition & 0 deletions lib/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 >= 2 = 50',
'AWS_REGION': "string = 'us-east-1'",
'AWS_ENDPOINT_URL?': 'string.url',
'AWS_ACCESS_KEY_ID?': 'string',
Expand Down
16 changes: 11 additions & 5 deletions lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -880,16 +881,21 @@ function accumulateFolder(
class S3Adapter implements StorageAdapter {
static async fromEnv(env: Extract<Env, { STORAGE_DRIVER: 's3' }>) {
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,
}),
})
Expand Down
45 changes: 45 additions & 0 deletions tests/s3-socket-pool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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<Env, { STORAGE_DRIVER: 's3' }>

// 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 = 2
const storage = await Storage.fromEnv()
const folder = `socket-pool-${randomUUID()}`
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: 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[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)
}
})
})
1 change: 1 addition & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading