AWS SDK JS的httpOptions超时间歇性不起作用。

huangapple go评论64阅读模式
英文:

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})"}

这里有两个问题:

  1. 为什么SDK即使超过130秒(超时时间仅为1秒)仍未超时?
  2. 为什么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

  1. Why SDK didn't timeout even after more than 130 seconds (timeout is just 1 sec)?
  2. 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&lt;AWS.S3.Types.PutObjectOutput&gt; {
  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.

huangapple
  • 本文由 发表于 2023年6月13日 14:57:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定