Skip to content

Commit 4c94545

Browse files
leomotorsCopilot
andcommitted
fix s3 client
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent ca260f4 commit 4c94545

File tree

5 files changed

+94
-29
lines changed

5 files changed

+94
-29
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ node_modules
22
build
33
.next
44
out
5-
src/generated
5+
src/prisma

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pnpm-lock.yaml
55
coverage
66
node_modules
77
test-results
8+
src/prisma

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@next-auth/prisma-adapter": "1.0.7",
5151
"@next/bundle-analyzer": "15.5.5",
5252
"@prisma/client": "6.17.1",
53+
"@smithy/node-http-handler": "^4.4.5",
5354
"@supabase/realtime-js": "2.15.6",
5455
"@tanstack/react-table": "8.21.3",
5556
"@uiw/codemirror-theme-dracula": "4.25.2",

pnpm-lock.yaml

Lines changed: 64 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/s3Client.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
1+
import * as https from 'https'
2+
13
import { S3 } from '@aws-sdk/client-s3'
4+
import { NodeHttpHandler } from '@smithy/node-http-handler'
25

6+
// Basic validation
37
if (!process.env.BUCKET_KEY_ID || !process.env.BUCKET_KEY_SECRET)
48
throw new Error('Failed to initialize bucket: Environment Variable Missing')
59

10+
// Configure a shared HTTPS agent to reuse sockets and avoid leaking connections.
11+
// Tune via env vars if needed in production.
12+
const MAX_SOCKETS = process.env.S3_MAX_SOCKETS
13+
? parseInt(process.env.S3_MAX_SOCKETS, 10)
14+
: 200
15+
16+
// default socket acquisition warning timeout in ms
17+
const SOCKET_ACQUISITION_WARNING_TIMEOUT = process.env
18+
.S3_SOCKET_ACQUISITION_WARNING_TIMEOUT
19+
? parseInt(process.env.S3_SOCKET_ACQUISITION_WARNING_TIMEOUT, 10)
20+
: 2000
21+
22+
const httpsAgent = new https.Agent({ keepAlive: true, maxSockets: MAX_SOCKETS })
23+
24+
const httpHandler = new NodeHttpHandler({
25+
httpsAgent,
26+
// Warn if acquiring a socket takes too long — reduces silent queueing.
27+
socketAcquisitionWarningTimeout: SOCKET_ACQUISITION_WARNING_TIMEOUT
28+
})
29+
630
export const s3Client = new S3({
731
endpoint: process.env.BUCKET_ENDPOINT,
832
region: process.env.BUCKET_REGION,
933
credentials: {
1034
accessKeyId: process.env.BUCKET_KEY_ID,
1135
secretAccessKey: process.env.BUCKET_KEY_SECRET
12-
}
36+
},
37+
// Use the shared handler so all S3 calls reuse the same socket pool.
38+
requestHandler: httpHandler
1339
})

0 commit comments

Comments
 (0)