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

Update SDK API operation request send to required Context #265

Merged
merged 7 commits into from Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
50 changes: 2 additions & 48 deletions aws/context.go
@@ -1,62 +1,16 @@
package aws

import (
"context"
"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 {
func SleepWithContext(ctx context.Context, dur time.Duration) error {
t := time.NewTimer(dur)
defer t.Stop()

Expand Down
41 changes: 0 additions & 41 deletions aws/context_1_6.go

This file was deleted.

9 changes: 0 additions & 9 deletions aws/context_1_7.go

This file was deleted.

3 changes: 2 additions & 1 deletion aws/defaults/handlers_1_8_test.go
Expand Up @@ -3,6 +3,7 @@
package defaults_test

import (
"context"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestSendHandler_HEADNoBody(t *testing.T) {
t.Fatalf("expect %T request body, got %T", e, a)
}

_, err := req.Send()
_, err := req.Send(context.Background())
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions aws/defaults/handlers_test.go
Expand Up @@ -356,7 +356,7 @@ func TestBuildContentLength_ZeroBody(t *testing.T) {
Key: aws.String("keyname"),
})

if _, err := req.Send(); err != nil {
if _, err := req.Send(context.Background()); err != nil {
t.Errorf("expect no error, got %v", err)
}
}
Expand All @@ -376,7 +376,7 @@ func TestBuildContentLength_NegativeBody(t *testing.T) {

req.HTTPRequest.Header.Set("Content-Length", "-1")

if _, err := req.Send(); err != nil {
if _, err := req.Send(context.Background()); err != nil {
t.Errorf("expect no error, got %v", req.Error)
}
}
Expand All @@ -395,7 +395,7 @@ func TestBuildContentLength_WithBody(t *testing.T) {
Body: bytes.NewReader(make([]byte, 1024)),
})

if _, err := req.Send(); err != nil {
if _, err := req.Send(context.Background()); err != nil {
t.Errorf("expect no error, got %v", err)
}
}
5 changes: 3 additions & 2 deletions aws/example_test.go
@@ -1,6 +1,7 @@
package aws_test

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -36,7 +37,7 @@ func ExampleEndpointResolverFunc() {
Bucket: aws.String("myBucket"),
Key: aws.String("myObjectKey"),
})
objResp, err := objReq.Send()
objResp, err := objReq.Send(context.Background())
if err != nil {
panic("S3 Get Object error, " + err.Error())
}
Expand All @@ -51,7 +52,7 @@ func ExampleEndpointResolverFunc() {
msgReq := sqsSvc.ReceiveMessageRequest(&sqs.ReceiveMessageInput{
QueueUrl: aws.String("my-queue-url"),
})
msgResp, err := msgReq.Send()
msgResp, err := msgReq.Send(context.Background())
if err != nil {
panic("SQS Receive Message error, " + err.Error())
}
Expand Down
13 changes: 7 additions & 6 deletions aws/request.go
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"bytes"
"context"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -58,7 +59,7 @@ type Request struct {
LastSignedAt time.Time
DisableFollowRedirects bool

context Context
context context.Context

built bool

Expand Down Expand Up @@ -194,12 +195,12 @@ func (r *Request) ApplyOptions(opts ...Option) {
}

// Context will always returns a non-nil context. If Request does not have a
// context BackgroundContext will be returned.
func (r *Request) Context() Context {
// context the context.Background will be returned.
func (r *Request) Context() context.Context {
if r.context != nil {
return r.context
}
return BackgroundContext()
return context.Background()
}

// SetContext adds a Context to the current request that can be used to cancel
Expand All @@ -218,11 +219,11 @@ func (r *Request) Context() Context {
// The http.Request.WithContext will be used to set the context on the underlying
// http.Request. This will create a shallow copy of the http.Request. The SDK
// may create sub contexts in the future for nested requests such as retries.
func (r *Request) SetContext(ctx Context) {
func (r *Request) SetContext(ctx context.Context) {
if ctx == nil {
panic("context cannot be nil")
}
setRequestContext(r, ctx)
setRequestContext(ctx, r)
}

// WillRetry returns if the request's can be retried.
Expand Down
6 changes: 3 additions & 3 deletions aws/request_context.go
@@ -1,12 +1,12 @@
// +build go1.7

package aws

import "context"

// setContext updates the Request to use the passed in context for cancellation.
// Context will also be used for request retry delay.
//
// Creates shallow copy of the http.Request with the WithContext method.
func setRequestContext(r *Request, ctx Context) {
func setRequestContext(ctx context.Context, r *Request) {
r.context = ctx
r.HTTPRequest = r.HTTPRequest.WithContext(ctx)
}
12 changes: 0 additions & 12 deletions aws/request_context_1_6.go

This file was deleted.

7 changes: 4 additions & 3 deletions aws/request_pagination.go
@@ -1,6 +1,7 @@
package aws

import (
"context"
"sync/atomic"

"github.com/aws/aws-sdk-go-v2/internal/awsutil"
Expand Down Expand Up @@ -31,7 +32,7 @@ type Pager struct {
//
// NewRequest should always be built from the same API operations. It is
// undefined if different API operations are returned on subsequent calls.
NewRequest func() (*Request, error)
NewRequest func(context.Context) (*Request, error)

started bool
nextTokens []interface{}
Expand Down Expand Up @@ -69,12 +70,12 @@ func (p *Pager) CurrentPage() interface{} {
// to be cast to the API operation's output type.
//
// Use the Err method to determine if an error occurred if Page returns false.
func (p *Pager) Next() bool {
func (p *Pager) Next(ctx context.Context) bool {
if !p.hasNextPage() {
return false
}

req, err := p.NewRequest()
req, err := p.NewRequest(ctx)
if err != nil {
p.err = err
return false
Expand Down