Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capability to set timeout for ec2metadata's http client #4280

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ type Config struct {
//
EC2MetadataDisableTimeoutOverride *bool

// Set this to override duration of the EC2Metadata client
// This is helpful if you want to have different timout value between
// custom HTTP client and ec2metadata client
EC2MetadataTimeout *time.Duration

// Instructs the endpoint to be generated for a service client to
// be the dual stack endpoint. The dual stack endpoint will support
// both IPv4 and IPv6 addressing.
Expand Down Expand Up @@ -429,6 +434,13 @@ func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
return c
}

// WithEC2MetadataTimeout sets a config EC2MetadataTimeout value
// returning a Config pointer for chaining.
func (c *Config) WithEC2MetadataTimeout(timeout time.Duration) *Config {
c.EC2MetadataTimeout = &timeout
return c
}

// WithSleepDelay overrides the function used to sleep while waiting for the
// next retry. Defaults to time.Sleep.
func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
Expand Down Expand Up @@ -573,6 +585,10 @@ func mergeInConfig(dst *Config, other *Config) {
dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
}

if other.EC2MetadataTimeout != nil {
dst.EC2MetadataTimeout = other.EC2MetadataTimeout
}

if other.SleepDelay != nil {
dst.SleepDelay = other.SleepDelay
}
Expand Down
7 changes: 6 additions & 1 deletion aws/ec2metadata/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ func New(p client.ConfigProvider, cfgs ...*aws.Config) *EC2Metadata {
// To disable this set Config.EC2MetadataDisableTimeoutOverride to false. Enabled by default.
func NewClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegion string, opts ...func(*client.Client)) *EC2Metadata {
if !aws.BoolValue(cfg.EC2MetadataDisableTimeoutOverride) && httpClientZero(cfg.HTTPClient) {
timeout := 1 * time.Second
if cfg.EC2MetadataTimeout != nil {
timeout = *cfg.EC2MetadataTimeout
}

// If the http client is unmodified and this feature is not disabled
// set custom timeouts for EC2Metadata requests.
cfg.HTTPClient = &http.Client{
// use a shorter timeout than default because the metadata
// service is local if it is running, and to fail faster
// if not running on an ec2 instance.
Timeout: 1 * time.Second,
Timeout: timeout,
}
// max number of retries on the client operation
cfg.MaxRetries = aws.Int(2)
Expand Down
9 changes: 9 additions & 0 deletions aws/ec2metadata/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func TestClientNotOverrideDefaultHTTPClientTimeout(t *testing.T) {
}
}

func TestClientHTTPClientTimeout(t *testing.T) {
cfg := aws.NewConfig().WithEC2MetadataTimeout(time.Second * 5)
svc := ec2metadata.New(unit.Session, cfg)

if e, a := *cfg.EC2MetadataTimeout, *svc.Config.EC2MetadataTimeout; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}

func TestClientDisableOverrideDefaultHTTPClientTimeout(t *testing.T) {
svc := ec2metadata.New(unit.Session, aws.NewConfig().WithEC2MetadataDisableTimeoutOverride(true))

Expand Down