LoginSignup
8
7

More than 5 years have passed since last update.

GoでAWS APIを叩く。 aws-sdk-go の エラー時再試行でエクスポネンシャルバックオフ(exponential backoff)

Last updated at Posted at 2015-02-15

引き続き aws-sdk-go ネタです。

AWS APIのエラー時の再試行には、exponential backoff しなさいと書かれてまして、
goで exponential backoffしてみます。

exponential backoff とは?

再試行時のwait間隔を徐々に(指数関数的に)伸ばす処理のことです。

exponential backoff についてはこちらにわかりやすい説明があります。

goでexponential backoff するパッケージ

https://github.com/cenkalti/backoff
これを使います。

s3.PutObjectのサンプルの PutObjectを exponential backoff化すると以下のような感じ

    var res *s3.PutObjectOutput

    err = backoff.Retry(func() error {
        res, err = S3.PutObject(&req)
        return err
    }, backoff.NewExponentialBackOff())
    if err != nil {
        log.Printf("err:%s", res, err)
    }

    log.Printf("res:%s", res)

エラーの種類を問わず再試行を繰り返してしまうようなかなりの手抜きですが、
こんな感じで簡単に使えます。

ちなみに、aws-sdk-go のdevelopブランチでは、標準でretry機能が実装されているっぽいです。

8
7
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
8
7