fix(files): 修复 304 缓存未销毁流导致的 S3 连接池泄漏与图片挂起卡死
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import http from 'node:http'
|
||||
import https from 'node:https'
|
||||
import { Readable } from 'node:stream'
|
||||
|
||||
import {
|
||||
@@ -10,6 +12,7 @@ import {
|
||||
PutObjectCommand,
|
||||
S3Client,
|
||||
} from '@aws-sdk/client-s3'
|
||||
import { NodeHttpHandler } from '@smithy/node-http-handler'
|
||||
|
||||
import { runtimeConfig } from '../../config/runtime.js'
|
||||
import { createHttpError } from '../../utils/http.js'
|
||||
@@ -65,6 +68,12 @@ export class ObjectStorage {
|
||||
accessKeyId: String(config.accessKeyId || '').trim(),
|
||||
secretAccessKey: String(config.secretAccessKey || '').trim(),
|
||||
},
|
||||
requestHandler: new NodeHttpHandler({
|
||||
connectionTimeout: 5000,
|
||||
requestTimeout: 15000,
|
||||
httpAgent: new http.Agent({ maxSockets: 200, keepAlive: true }),
|
||||
httpsAgent: new https.Agent({ maxSockets: 200, keepAlive: true }),
|
||||
}),
|
||||
// 默认的 CRC32 请求校验头不被 OSS 的 S3 兼容层支持,关闭以保持兼容。
|
||||
requestChecksumCalculation: 'WHEN_REQUIRED',
|
||||
responseChecksumValidation: 'WHEN_REQUIRED',
|
||||
@@ -175,6 +184,12 @@ export class ObjectStorage {
|
||||
return
|
||||
}
|
||||
|
||||
// 阿里云 OSS 桶由控制台预先创建,跳过 HeadBucketCommand 避免 RAM 权限受限或挂起阻塞
|
||||
if (!this.pathStyle) {
|
||||
this.bucketReady = true
|
||||
return
|
||||
}
|
||||
|
||||
if (!this.bucketReadyPromise) {
|
||||
this.bucketReadyPromise = (async () => {
|
||||
try {
|
||||
@@ -183,11 +198,6 @@ export class ObjectStorage {
|
||||
if (!isNotFoundError(error)) {
|
||||
throw error
|
||||
}
|
||||
if (!this.pathStyle) {
|
||||
throw new Error(`OSS bucket ${this.bucket} 不存在,请先在阿里云控制台创建`, {
|
||||
cause: error,
|
||||
})
|
||||
}
|
||||
await this.client.send(new CreateBucketCommand({ Bucket: this.bucket }))
|
||||
}
|
||||
this.bucketReady = true
|
||||
@@ -303,7 +313,9 @@ function toObjectStorageConfig(config: {
|
||||
|
||||
export function normalizeStorageEndpoint(rawEndpoint: unknown) {
|
||||
const endpoint = String(rawEndpoint || '').trim()
|
||||
const parsed = new URL(/^https?:\/\//i.test(endpoint) ? endpoint : `http://${endpoint}`)
|
||||
const isOss = endpoint.toLowerCase().includes('.aliyuncs.com')
|
||||
const defaultScheme = isOss ? 'https://' : 'http://'
|
||||
const parsed = new URL(/^https?:\/\//i.test(endpoint) ? endpoint : `${defaultScheme}${endpoint}`)
|
||||
const port = parsed.port ? Number(parsed.port) : parsed.protocol === 'https:' ? 443 : 80
|
||||
|
||||
return {
|
||||
|
||||
@@ -110,4 +110,3 @@ export function resolveCollectSubmittedFieldValues(workOrder: WorkOrderRow) {
|
||||
: {}
|
||||
return normalizeSubmittedFields(collect.fields)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user