LoginSignup
1
0

More than 3 years have passed since last update.

AWSのAPIでスロットリングされたらリトライするようにする

Posted at

e.code === 'Throttling'でエラーがthrowされるので、これを拾ってリトライするように実装すればとりあえずOK。
ただし再帰呼び出しになるので、リトライ数を記録して無限ループにならないようにする必要はある。

// Wait処理
const sleep = async (time = 1000) => {
  return new Promise(resolve => setTimeout(resolve, time))
}

// CloudFrontのupdate処理
const worker = async (targetID, retryCount = 0) => {
  try {
    const dist = await cloudfront.getDistribution({
      Id: targetID
    }).promise()
    const distributionConfig = dist.Distribution.DistributionConfig
    distributionConfig.Enabled = false
    await cloudfront.updateDistribution({
      Id: dist.Distribution.Id,
      IfMatch: dist.ETag,
      DistributionConfig: distributionConfig
    }).promise()
  } catch (e) {
    if (e.code === 'Throttling') {
      if (retryCount < 2) {
        await sleep(2000)
        return worker(targetID, retryCount + 1)
      }
    }
    throw e
  }
}

const distIds = ['a', 'b', 'c']
Promise.all(distIds.map(async id => worker(id))

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0