Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: create new retryer configuration for each service (#14)
Browse files Browse the repository at this point in the history
* fix: create new retryer configuration for each service

fixes issue described in sdk docs where a global retry token bucket was being shared across all services https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/retries-timeouts/#customizing-behavior

* chore: address PR comment
  • Loading branch information
James Quigley committed Mar 14, 2021
1 parent a949e28 commit f633eff
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ type Provider struct {
db *database.Database
config Config
Logger hclog.Logger
retryOpt config.LoadOptionsFunc
regions []string
}

Expand Down Expand Up @@ -142,6 +141,12 @@ var tablesArr = [][]interface{}{
sns.TopicTables,
}

func (p *Provider) NewRetryer() func() aws.Retryer {
return func() aws.Retryer {
return retry.AddWithMaxBackoffDelay(retry.AddWithMaxAttempts(retry.NewStandard(), p.config.MaxRetries), time.Second*time.Duration(p.config.MaxBackoff))
}
}

func (p *Provider) Init(driver string, dsn string, verbose bool) error {
var err error
p.db, err = database.Open(driver, dsn)
Expand Down Expand Up @@ -212,6 +217,7 @@ func (p *Provider) fetchAccount(accountID string, awsCfg aws.Config, svc *sts.Cl

innerLog := p.Logger.With("account_id", accountID)
for serviceName, newFunc := range globalServices {
awsCfg.Retryer = p.NewRetryer()
resourceClients[serviceName] = newFunc(awsCfg,
p.db, innerLog, accountID, "us-east-1")
}
Expand All @@ -231,6 +237,7 @@ func (p *Provider) fetchAccount(accountID string, awsCfg aws.Config, svc *sts.Cl

innerLog := p.Logger.With("account_id", accountID, "region", region)
for serviceName, newFunc := range regionalServices {
awsCfg.Retryer = p.NewRetryer()
resourceClients[serviceName] = newFunc(awsCfg,
p.db, innerLog, accountID, region)
}
Expand Down Expand Up @@ -303,30 +310,28 @@ func (p *Provider) Fetch(data []byte) error {
})
}
p.Logger.Info("Configuring SDK retryer", "retry_attempts", p.config.MaxRetries, "max_backoff", p.config.MaxBackoff)
p.retryOpt = config.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxBackoffDelay(retry.AddWithMaxAttempts(retry.NewStandard(), p.config.MaxRetries), time.Second*time.Duration(p.config.MaxBackoff))
})

g := errgroup.Group{}
for _, account := range p.config.Accounts {
var err error
if account.ID != "default" && account.RoleARN != "" {
// assume role if specified (SDK takes it from default or env var: AWS_PROFILE)
awsCfg, err = config.LoadDefaultConfig(ctx, p.retryOpt)
awsCfg, err = config.LoadDefaultConfig(ctx)
if err != nil {
_ = g.Wait()
return err
}
awsCfg.Credentials = stscreds.NewAssumeRoleProvider(sts.NewFromConfig(awsCfg), account.RoleARN)
} else if account.ID != "default" {
awsCfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(account.ID), p.retryOpt)
awsCfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(account.ID))
} else {
awsCfg, err = config.LoadDefaultConfig(ctx, p.retryOpt)
awsCfg, err = config.LoadDefaultConfig(ctx)
}
if err != nil {
_ = g.Wait()
return err
}
awsCfg.Retryer = p.NewRetryer()
svc := sts.NewFromConfig(awsCfg)
output, err := svc.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}, func(o *sts.Options) {
o.Region = "us-east-1"
Expand Down

0 comments on commit f633eff

Please sign in to comment.