forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws/request: Add support for context.Context to SDK API operation req…
…uests (aws#1132) Adds support for context.Context to the SDK by adding `WithContext` methods for each API operation, Paginators and Waiters. e.g `PutObjectWithContext`. This change also adds the ability to provide request functional options to the method calls instead of requiring you to use the `Request` API operation method (e.g `PutObjectRequest`). The SDK will use Context for cancelation of requests, and retry delays for both requests and waiters. For methods that take a Context, the Context is most not be nil. If the Context is nil a panic will occur. This change also creates a `aws.Context` interface type which is a copy of Go 1.7's context.Context interface type. This is done to allow Go 1.6 SDK users using to take advantage of the Context while also preventing the SDK from forking its request and retry logic by Go version. ### Go 1.6 and below: SDK requests on Go 1.6 and 1.5 will assign the Context's `Done()` return to `http.Request.Cancel`. ### Go 1.6 and above: SDK requests on Go 1.7 and above will use `http.Request.WithContext` to assign the context to the http.Request. The SDK will also use the Context internally to interrupt request and retry delays. In addition, the value returned by `aws.BackgroundContext` is equal to that returned by `context.Background()` ## Usage Examples ```go func (c *S3) PutObjectWithContext(ctx aws.Context, input *PutObjectInput, opts ...request.Option)(*PutObjectOutput, error) ``` ### New Method: ```go result, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{ Bucket: aws.String("myBucket"), Key: aws.String("myKey"), Body: object, }) // Optionally also add request options result, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{ Bucket: aws.String("myBucket"), Key: aws.String("myKey"), Body: object, }, request.WithLogLevel(aws.LogDebugWithHTTPBody)) ``` ### Old Method: ```go req, result := svc.PutObjectRequest(&s3.PutObjectInput{ Bucket: aws.String("myBucket"), Key: aws.String("myKey"), Body: object, }) req.HTTPRequest = req.HTTPRequest.WithContext(ctx) req.Config.LogLevel = aws.LogLevel(aws.LogDebugWithHTTPBody) err := req.Send() ``` ## Additional Changes * Adds a `Complete` Request handler list that will get called ever time a request is completed. This includes both success and failure. Complete will only be called once per API operation request. * `private/waiter` package moved from the private group to `aws/request/waiter` and made publicly available. * Adds Context support to all API operations, Waiters(WaitUntil) and Paginators(Pages) methods. * Adds Context support for s3manager and s3crypto clients. Fix aws#861, aws#455, aws#454, aws#667, aws#1137
- Loading branch information
Showing
253 changed files
with
69,945 additions
and
15,466 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package aws | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Context is an copy of the Go v1.7 stdlib's context.Context interface. | ||
// It is represented as a SDK interface to enable you to use the "WithContext" | ||
// API methods with Go v1.6 and a Context type such as golang.org/x/net/context. | ||
// | ||
// See https://golang.org/pkg/context on how to use contexts. | ||
type Context interface { | ||
// Deadline returns the time when work done on behalf of this context | ||
// should be canceled. Deadline returns ok==false when no deadline is | ||
// set. Successive calls to Deadline return the same results. | ||
Deadline() (deadline time.Time, ok bool) | ||
|
||
// Done returns a channel that's closed when work done on behalf of this | ||
// context should be canceled. Done may return nil if this context can | ||
// never be canceled. Successive calls to Done return the same value. | ||
Done() <-chan struct{} | ||
|
||
// Err returns a non-nil error value after Done is closed. Err returns | ||
// Canceled if the context was canceled or DeadlineExceeded if the | ||
// context's deadline passed. No other values for Err are defined. | ||
// After Done is closed, successive calls to Err return the same value. | ||
Err() error | ||
|
||
// Value returns the value associated with this context for key, or nil | ||
// if no value is associated with key. Successive calls to Value with | ||
// the same key returns the same result. | ||
// | ||
// Use context values only for request-scoped data that transits | ||
// processes and API boundaries, not for passing optional parameters to | ||
// functions. | ||
Value(key interface{}) interface{} | ||
} | ||
|
||
// BackgroundContext returns a context that will never be canceled, has no | ||
// values, and no deadline. This context is used by the SDK to provide | ||
// backwards compatibility with non-context API operations and functionality. | ||
// | ||
// Go 1.6 and before: | ||
// This context function is equivalent to context.Background in the Go stdlib. | ||
// | ||
// Go 1.7 and later: | ||
// The context returned will be the value returned by context.Background() | ||
// | ||
// See https://golang.org/pkg/context for more information on Contexts. | ||
func BackgroundContext() Context { | ||
return backgroundCtx | ||
} | ||
|
||
// SleepWithContext will wait for the timer duration to expire, or the context | ||
// is canceled. Which ever happens first. If the context is canceled the Context's | ||
// error will be returned. | ||
// | ||
// Expects Context to always return a non-nil error if the Done channel is closed. | ||
func SleepWithContext(ctx Context, dur time.Duration) error { | ||
t := time.NewTimer(dur) | ||
defer t.Stop() | ||
|
||
select { | ||
case <-t.C: | ||
break | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// +build !go1.7 | ||
|
||
package aws | ||
|
||
import "time" | ||
|
||
// An emptyCtx is a copy of the the Go 1.7 context.emptyCtx type. This | ||
// is copied to provide a 1.6 and 1.5 safe version of context that is compatible | ||
// with Go 1.7's Context. | ||
// | ||
// An emptyCtx is never canceled, has no values, and has no deadline. It is not | ||
// struct{}, since vars of this type must have distinct addresses. | ||
type emptyCtx int | ||
|
||
func (*emptyCtx) Deadline() (deadline time.Time, ok bool) { | ||
return | ||
} | ||
|
||
func (*emptyCtx) Done() <-chan struct{} { | ||
return nil | ||
} | ||
|
||
func (*emptyCtx) Err() error { | ||
return nil | ||
} | ||
|
||
func (*emptyCtx) Value(key interface{}) interface{} { | ||
return nil | ||
} | ||
|
||
func (e *emptyCtx) String() string { | ||
switch e { | ||
case backgroundCtx: | ||
return "aws.BackgroundContext" | ||
} | ||
return "unknown empty Context" | ||
} | ||
|
||
var ( | ||
backgroundCtx = new(emptyCtx) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// +build go1.7 | ||
|
||
package aws | ||
|
||
import "context" | ||
|
||
var ( | ||
backgroundCtx = context.Background() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package aws_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/awstesting" | ||
) | ||
|
||
func TestSleepWithContext(t *testing.T) { | ||
ctx := &awstesting.FakeContext{DoneCh: make(chan struct{})} | ||
|
||
err := aws.SleepWithContext(ctx, 1*time.Millisecond) | ||
if err != nil { | ||
t.Errorf("expect context to not be canceled, got %v", err) | ||
} | ||
} | ||
|
||
func TestSleepWithContext_Canceled(t *testing.T) { | ||
ctx := &awstesting.FakeContext{DoneCh: make(chan struct{})} | ||
|
||
expectErr := fmt.Errorf("context canceled") | ||
|
||
ctx.Error = expectErr | ||
close(ctx.DoneCh) | ||
|
||
err := aws.SleepWithContext(ctx, 1*time.Millisecond) | ||
if err == nil { | ||
t.Fatalf("expect error, did not get one") | ||
} | ||
|
||
if e, a := expectErr, err; e != a { | ||
t.Errorf("expect %v error, got %v", e, a) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.