英文:
AWS SDK JS httpOptions timeout intermittently not working
问题
我已部署了一个AWS Lambda函数来读取和提供S3文件。AWS SDK JS使用以下选项进行初始化:
awsS3CustomConfig = {
maxRetries: parseInt(1),
httpOptions: {
timeout: parseInt(1000),
},
}
虽然有时代码运行正常,但Lambda日志中出现了以下超时异常:
{"response":{"statusCode":500,"headers":{},"body":"{\"status\":500,\"timestamp\":\"2023-06-12T23:52:40.865Z\",\"description\":\"Connection timed out after 1000ms\",\"message\":\"generalError\",\"subErrors\":[\"TimeoutError\"]}"}}
大多数情况下,我从SDK获取到以下日志:
{"message":"[AWS s3 200 130.776s 1 retries] getObject({\n Bucket: 'aue1p1z1s3bd*****',\n Key: 'bright/listing/802315843012.json',\n VersionId: undefined\n})"}
这里有两个问题:
- 为什么SDK即使超过130秒(超时时间仅为1秒)仍未超时?
- 为什么SDK记录200状态而不是引发超时错误?
英文:
I have deployed an AWS Lambda function for reading and serving S3 files. AWS SDK JS is initialized with these options
awsS3CustomConfig = {
maxRetries: parseInt(1),
httpOptions: {
timeout: parseInt(1000),
},
}
While sometimes the code is working fine an I am getting this timeout exception in Lambda logs
{"response":{"statusCode":500,"headers":{},"body":"{\"status\":500,\"timestamp\":\"2023-06-12T23:52:40.865Z\",\"description\":\"Connection timed out after 1000ms\",\"message\":\"generalError\",\"subErrors\":[\"TimeoutError\"]}"}}
Mostly I am getting this log from SDK
{"message":"[AWS s3 200 130.776s 1 retries] getObject({\\n Bucket: 'aue1p1z1s3bd*****',\\n Key: 'bright/listing/802315843012.json',\\n VersionId: undefined\\n})"}`
There are two problems here
- Why SDK didn't timeout even after more than 130 seconds (timeout is just 1 sec)?
- Why SDK logging 200 status instead of raising timeout error?
答案1
得分: 1
我在使用aws-sdk并实现了httpOptions.timeout后遇到了相同的问题,在javascript中使用setTimeout方法来实现abort方法修复了这个问题。
const s3 = new AWS.S3({ maxRetries: 1 })
async function putObjectInS3(Bucket: string, Key: string, Body?: string, Tagging?: string): Promise<AWS.S3.Types.PutObjectOutput> {
const params: AWS.S3.Types.PutObjectRequest = { Bucket, Key, Body, Tagging }
const putObjectReq = s3.putObject(params)
setTimeout(putObjectReq.abort.bind(putObjectReq), 3000) // 3秒超时
return putObjectReq.promise()
}
请注意,尽管abort
的实现比httpOptions.timeout
要好得多,但它也有可能失败。在我的系统中,我注意到它99.999%的时间都有效,这对我来说是令人满意的。
英文:
I was facing the same issue after implementing httpOptions.timeout with aws-sdk, implementing abort method with setTimeout method of javascript fixed this issue for me.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const s3 = new AWS.S3({ maxRetries: 1 })
async function putObjectInS3(Bucket: string, Key: string, Body?: string, Tagging?: string): Promise<AWS.S3.Types.PutObjectOutput> {
const params: AWS.S3.Types.PutObjectRequest = { Bucket, Key, Body, Tagging }
const putObjectReq = s3.putObject(params)
setTimeout(putObjectReq.abort.bind(putObjectReq), 3000) // 3 seconds timeout
return putObjectReq.promise()
}
<!-- end snippet -->
Please note that even though abort
implementation works much better as compared to httpOptions.timeout
, it also has chances to fail. I have noticed in my system it works 99.999% of times which is satisfactory for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论