Skip to content

Commit

Permalink
Update operationTimeout to handle unset/zero DefaultTimeout
Browse files Browse the repository at this point in the history
Updates operationTimeout so that if DefaultTimeout is unset (aka zero)
operationTimeout will not set a default timeout on the context.
  • Loading branch information
jasdel committed Oct 6, 2021
1 parent dccd10a commit 4eba5da
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 4 additions & 1 deletion feature/ec2/imds/request_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ const (
// stack was called with does not have a deadline. The next middleware must
// complete before the timeout, or the context will be canceled.
//
// If DefaultTimeout is zero, no default timeout will be used if the Context
// does not have a timeout.
//
// The next middleware must also ensure that any resources that are also
// canceled by the stack's context are completely consumed before returning.
// Otherwise the timeout cleanup will race the resource being consumed
Expand All @@ -242,7 +245,7 @@ func (m *operationTimeout) HandleInitialize(
) (
output middleware.InitializeOutput, metadata middleware.Metadata, err error,
) {
if _, ok := ctx.Deadline(); !ok {
if _, ok := ctx.Deadline(); !ok && m.DefaultTimeout != 0 {
var cancelFn func()
ctx, cancelFn = context.WithTimeout(ctx, m.DefaultTimeout)
defer cancelFn()
Expand Down
26 changes: 25 additions & 1 deletion feature/ec2/imds/request_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ func TestOperationTimeoutMiddleware(t *testing.T) {
) (
out middleware.InitializeOutput, metadata middleware.Metadata, err error,
) {
if err := sdk.SleepWithContext(ctx, time.Second); err != nil {
if _, ok := ctx.Deadline(); !ok {
return out, metadata, fmt.Errorf("expect context deadline to be set")
}

if err := sdk.SleepWithContext(ctx, time.Millisecond); err != nil {
return out, metadata, err
}

Expand All @@ -151,6 +155,26 @@ func TestOperationTimeoutMiddleware(t *testing.T) {
}
}

func TestOperationTimeoutMiddleware_noDefaultTimeout(t *testing.T) {
m := &operationTimeout{}

_, _, err := m.HandleInitialize(context.Background(), middleware.InitializeInput{},
middleware.InitializeHandlerFunc(func(
ctx context.Context, input middleware.InitializeInput,
) (
out middleware.InitializeOutput, metadata middleware.Metadata, err error,
) {
if t, ok := ctx.Deadline(); ok {
return out, metadata, fmt.Errorf("expect no context deadline, got %v", t)
}

return out, metadata, nil
}))
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
}

func TestOperationTimeoutMiddleware_withCustomDeadline(t *testing.T) {
m := &operationTimeout{
DefaultTimeout: time.Nanosecond,
Expand Down

0 comments on commit 4eba5da

Please sign in to comment.