Skip to content

Migrate to AWS SDK for Go v2 - #15

Merged
mash merged 1 commit into
mainfrom
refactor/aws-sdk-go-v2
Oct 28, 2026
Merged

mash merged 1 commit into
mainfrom
refactor/aws-sdk-go-v2

Conversation

@mash

@mash mash commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

目的

firequeue の AWS SDK for Go を v1 (github.com/aws/aws-sdk-go) から v2 (github.com/aws/aws-sdk-go-v2) に移す。
外向きの挙動、ログ、既存テストの期待値は変えない。retry する応答と retry しない応答の組も v1 と同じにする。

公開 API の旧→新対応

旧 (v1) 新 (v2)
New(fh firehoseiface.FirehoseAPI, ...) New(fh FirehoseAPI, ...)
firehoseiface.FirehoseAPI FirehoseAPI(*firehose.Client の PutRecordBatch だけを持つ interface)
(*Queue).Enqueue(r *firehose.Record) (*Queue).Enqueue(r types.Record)
ErrorHandler(fn func(error, []*firehose.Record)) ErrorHandler(fn func(error, []types.Record))

types.Record は値型にした。v2 の PutRecordBatchInput.Records が []types.Record なので、firequeue は変換せずにそのまま渡せる。Data の []byte は v1 と同じく呼び出し元と共有する。

retry 判定の新旧対応

isRetryable は v1 の request.IsErrorRetryable・request.IsErrorThrottle・HTTP status の判定を v2 のエラー型で書き直した。

v1 のエラー v1 の判定 v2 のエラー v2 の判定
awserr.RequestFailure の status 429/500/502/503/504 retry する *smithyhttp.ResponseError の status 429/500/502/503/504 retry する
retryableCodes・credsExpiredCodes・throttleCodes のエラーコード retry する smithy.APIError の同じエラーコード (retryableErrorCodes) retry する
上記以外のエラーコード (例: InvalidArgumentException, 501) retry しない 上記以外の smithy.APIError retry しない
RequestCanceled retry しない *smithy.CanceledError, *aws.RequestCanceledError, context.Canceled retry しない
RequestError (cause を shouldRetryError で判定) cause による *smithyhttp.RequestSendError (cause を同じ規則で判定) cause による
SerializationError (cause を判定、cause が無ければ retry しない) cause による *smithy.DeserializationError (同じ規則) cause による
parameter validation のエラー、認証情報が無いエラー retry しない *smithy.InvalidParamsError など retry しない
read: connection reset by peer を含むエラー retry する 同じ文字列を含むエラー retry する

v2 の client が retry を使い切ったときに返す *retry.MaxAttemptsError は、包んだエラーで判定する。

aws-sdk-go v1.35.33 で旧 isRetryable に 24 ケースのエラーを渡し、retry_test.go の TestIsRetryable で対応するケースの期待値と一致することを確かめた。

呼び出し元への注意

SDK 内部の retry 回数

v1 の DefaultRetryer は MaxRetries=3 で、1 回の PutRecordBatch に最大 4 回の attempt を使う。v2 の標準 retryer は最大 3 回の attempt を使う。firequeue は client を受け取るだけなので、この回数は client を作る呼び出し元が揃える(例: retry.AddWithMaxAttempts(retry.NewStandard(), 4))。

SDK 内部の retry quota

v2 の標準 retryer は token bucket で retry の回数を制限する。容量は 500 token である。retry 1 回に 5 token(timeout のときは 10 token)を使い、1 回目の attempt で成功すると 1 token を戻す。token が尽きると、client は retry をやめて、すぐに *retry.QuotaExceededError を返す。v1 にはこの制限が無い。

firequeue は QuotaExceededError が包んだエラーで判定し、retry 対象なら自分の backoff で送り直す。このため、レコードは失わない。ただし、throttle が続く間は、client の中の retry が v1 より早く止まる。v1 と揃えるには、呼び出し元が client の retryer に RateLimiter = ratelimit.None を設定する。

エラー文の形式

v2 の client が返すエラーは、v1 と文の形式が違う。ErrorHandler を渡さないとき、firequeue はこのエラーを log に出すので、log の文も変わる。

  • v1: InvalidArgumentException: ...\n\tstatus code: 400, request id: ...
  • v2: operation error Firehose: PutRecordBatch, https response error StatusCode: 400, RequestID: ..., InvalidArgumentException: ...

呼び出し元は次の 2 点を確かめる。

  1. log の検索や監視が、v1 のエラー文で絞っていないか。
  2. ErrorHandler の中で、エラーを v1 の型(awserr.Error など)に変換していないか。v2 のエラーはこの型に当たらないので、判定が常に外れる。

go directive

go directive を 1.14 から 1.24 にした。1.24 は aws-sdk-go-v2・service/firehose・smithy-go が要求する最小の版である。

既知の問題(範囲外)

attemptToSendBatch は、PutRecordBatch が err == nil で FailedPutCount > 0 を返したとき、失敗したレコードを retry しない。retry するのは err が retry 対象のときだけである。v1 のときから同じ挙動なので、この PR では直さない。

テスト

  • go build ./...、go vet ./...、gofmt -l . が通ることを手元で確かめた。
  • go test は CI (GitHub Actions の test ワークフロー) で確かめる。

- Replace firehoseiface.FirehoseAPI with FirehoseAPI, an interface that
  has only PutRecordBatch of *firehose.Client (v2).
- Use types.Record (value) in place of *firehose.Record for Enqueue and
  ErrorHandler.
- Rewrite isRetryable with smithy error types. It keeps the set of
  retryable errors of v1 (request.IsErrorRetryable, IsErrorThrottle,
  HTTP 429/500/502/503/504, and "read: connection reset by peer").
- Set the go directive to 1.24 (the minimum of aws-sdk-go-v2) and drop github.com/aws/aws-sdk-go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Khu7nnDtvrt5NV5H7kvCn6
@mash
mash marked this pull request as ready for review October 27, 2026 16:04
@mash
mash requested review from handlename and soh335 October 27, 2026 16:04

@handlename handlename left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@mash
mash merged commit f5df875 into main Oct 28, 2026
8 checks passed
@mash
mash deleted the refactor/aws-sdk-go-v2 branch October 28, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants