From 334c01bff99cfadd601a8d33b4b6f6ed991940b1 Mon Sep 17 00:00:00 2001 From: evalphobia Date: Wed, 21 Jun 2023 11:24:05 +0900 Subject: [PATCH 1/2] [config] Add 'UseConfigCache' into config for preserving creds to avoid sts rate limit --- config/config.go | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index 9c16046..3e8d3b9 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,8 @@ package config import ( + "sync" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" @@ -45,17 +47,39 @@ type Config struct { S3UseARNRegion bool UseDualStack bool - UseMaxRetries bool - MaxRetries int + UseMaxRetries bool + MaxRetries int + UseConfigCache bool + muConfigCache sync.RWMutex + configCache *aws.Config } // Session creates AWS session from the Config values. -func (c Config) Session() (*session.Session, error) { +func (c *Config) Session() (*session.Session, error) { return session.NewSession(c.AWSConfig()) } // AWSConfig creates *aws.Config object from the fields. -func (c Config) AWSConfig() *aws.Config { +func (c *Config) AWSConfig() *aws.Config { + if !c.UseConfigCache { + return c.awsConfig() + } + + c.muConfigCache.RLock() + conf := c.configCache + c.muConfigCache.RUnlock() + + if conf == nil { + conf = c.awsConfig() + c.muConfigCache.Lock() + c.configCache = conf + c.muConfigCache.Unlock() + } + return conf +} + +// awsConfig creates *aws.Config object from the fields. +func (c *Config) awsConfig() *aws.Config { cred := c.awsCredentials() awsConf := &aws.Config{ Credentials: cred, @@ -123,7 +147,7 @@ func (c Config) AWSConfig() *aws.Config { return awsConf } -func (c Config) awsCredentials() *credentials.Credentials { +func (c *Config) awsCredentials() *credentials.Credentials { // from env cred := credentials.NewEnvCredentials() _, err := cred.Get() @@ -145,7 +169,7 @@ func (c Config) awsCredentials() *credentials.Credentials { return nil } -func (c Config) getRegion() string { +func (c *Config) getRegion() string { if c.Region != "" { return c.Region } @@ -156,7 +180,7 @@ func (c Config) getRegion() string { return defaultRegion } -func (c Config) getEndpoint() string { +func (c *Config) getEndpoint() string { if c.Endpoint != "" { return c.Endpoint } From dfe07536b93975855ed5734ec6edea38809f20c2 Mon Sep 17 00:00:00 2001 From: evalphobia Date: Wed, 21 Jun 2023 12:06:43 +0900 Subject: [PATCH 2/2] fix --- config/config.go | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/config/config.go b/config/config.go index 3e8d3b9..ff4d090 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,6 @@ package config import ( - "sync" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" @@ -47,39 +45,35 @@ type Config struct { S3UseARNRegion bool UseDualStack bool - UseMaxRetries bool - MaxRetries int - UseConfigCache bool - muConfigCache sync.RWMutex + UseMaxRetries bool + MaxRetries int + + // Cache aws config for preserving creds to avoid sts rate limit + useConfigCache bool configCache *aws.Config } // Session creates AWS session from the Config values. -func (c *Config) Session() (*session.Session, error) { +func (c Config) Session() (*session.Session, error) { return session.NewSession(c.AWSConfig()) } -// AWSConfig creates *aws.Config object from the fields. -func (c *Config) AWSConfig() *aws.Config { - if !c.UseConfigCache { - return c.awsConfig() - } - - c.muConfigCache.RLock() - conf := c.configCache - c.muConfigCache.RUnlock() +// SetConfigCache caches *aws.Config. +func (c *Config) SetConfigCache() { + c.configCache = c.awsConfig() + c.useConfigCache = true +} - if conf == nil { - conf = c.awsConfig() - c.muConfigCache.Lock() - c.configCache = conf - c.muConfigCache.Unlock() +// AWSConfig creates *aws.Config object from the fields. +func (c Config) AWSConfig() *aws.Config { + if c.useConfigCache { + return c.configCache } - return conf + return c.awsConfig() } // awsConfig creates *aws.Config object from the fields. -func (c *Config) awsConfig() *aws.Config { +func (c Config) awsConfig() *aws.Config { cred := c.awsCredentials() awsConf := &aws.Config{ Credentials: cred, @@ -147,7 +141,7 @@ func (c *Config) awsConfig() *aws.Config { return awsConf } -func (c *Config) awsCredentials() *credentials.Credentials { +func (c Config) awsCredentials() *credentials.Credentials { // from env cred := credentials.NewEnvCredentials() _, err := cred.Get() @@ -169,7 +163,7 @@ func (c *Config) awsCredentials() *credentials.Credentials { return nil } -func (c *Config) getRegion() string { +func (c Config) getRegion() string { if c.Region != "" { return c.Region } @@ -180,7 +174,7 @@ func (c *Config) getRegion() string { return defaultRegion } -func (c *Config) getEndpoint() string { +func (c Config) getEndpoint() string { if c.Endpoint != "" { return c.Endpoint }