0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

aws sdk goでAPIスロットリングした時のリトライ回数を指定する

Last updated at Posted at 2020-02-24

aws-sdk-go を利用してAPIから ThrottlingException: Rate exceeded が発生した際のデバッグ方法とリトライ試行回数の指定方法

package main

import (
	"context"
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
	"github.com/aws/aws-sdk-go/service/ec2"
)

func main() {
	mySession := session.Must(session.NewSession())
	loop := 10
	var wg sync.WaitGroup
	wg.Add(loop)
        
	// 意図的にスロットリングを発生させるために並列実行している
	for i := 0; i < loop; i++ {  
		go func() {
			getLogs(mySession)
			wg.Done()
		}()
	}
	wg.Wait()
}

func getLogs(sess *session.Session) {
	svc := cloudwatchlogs.New(sess, aws.NewConfig().WithMaxRetries(10)) // リトライ試行回数を指定
	t := time.Now().Add(-time.Hour).Unix() * 1000
	ctx := context.Background()
	for i := 0; i < 100; i++ {
		res, err := svc.FilterLogEventsWithContext(ctx, &cloudwatchlogs.FilterLogEventsInput{
			FilterPattern: aws.String(""),
			LogGroupName:  aws.String("/aws/apigateway/welcome"),
			StartTime:     aws.Int64(t),
		},
			request.WithLogLevel(aws.LogDebugWithRequestErrors),  // リクエストエラーのデバッグ表示(リトライの度に表示される)
		)
		if err != nil {
			e, ok := err.(awserr.Error)
			if ok {
				log.Printf("i=%d FilterLogEvents code:%s message:%s", i, e.Code(), e.Message())
				return
			}
			log.Printf("i=%d FilterLogEvents err:%s", i, err)
			return
		}

		if len(res.Events) > 0 {
			log.Printf("len(events)=%d", len(res.Events))
		}
	}
}
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?