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

[WIP] Using the Context from the timeout if provided #315

Merged
merged 4 commits into from
Sep 28, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autorest/adal/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (sf SenderFunc) Do(r *http.Request) (*http.Response, error) {
return sf(r)
}

// SendDecorator takes and possibily decorates, by wrapping, a Sender. Decorators may affect the
// SendDecorator takes and possibly decorates, by wrapping, a Sender. Decorators may affect the
// http.Request and pass it along or, first, pass the http.Request along then react to the
// http.Response result.
type SendDecorator func(Sender) Sender
Expand Down
14 changes: 11 additions & 3 deletions autorest/azure/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,17 @@ func (f Future) WaitForCompletion(ctx context.Context, client autorest.Client) e
// running operation has completed, the provided context is cancelled, or the client's
// polling duration has been exceeded. It will retry failed polling attempts based on
// the retry value defined in the client up to the maximum retry attempts.
func (f *Future) WaitForCompletionRef(ctx context.Context, client autorest.Client) error {
ctx, cancel := context.WithTimeout(ctx, client.PollingDuration)
defer cancel()
func (f *Future) WaitForCompletionRef(inputCtx context.Context, client autorest.Client) error {

var ctx context.Context
if d := client.PollingDuration; d != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In hindsight this method should never have relied on these client settings, I should have only used them in the legacy implementation DoPollForAsynchronous. :( While we could make such a change in a non-breaking way it would be a change in behavior that might cause unexpected problems.

var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(inputCtx, d)
defer cancel()
} else {
ctx = inputCtx
}

done, err := f.Done(client)
for attempts := 0; !done; done, err = f.Done(client) {
if attempts >= client.RetryAttempts {
Expand Down
6 changes: 3 additions & 3 deletions autorest/azure/rp.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func register(client autorest.Client, originalReq *http.Request, re RequestError
}

// poll for registered provisioning state
now := time.Now()
for err == nil && time.Since(now) < client.PollingDuration {
registrationStartTime := time.Now()
for err == nil && (client.PollingDuration == 0 || (client.PollingDuration != 0 && time.Since(registrationStartTime) < client.PollingDuration)) {
// taken from the resources SDK
// https://github.com/Azure/azure-sdk-for-go/blob/9f366792afa3e0ddaecdc860e793ba9d75e76c27/arm/resources/resources/providers.go#L45
preparer := autorest.CreatePreparer(
Expand Down Expand Up @@ -183,7 +183,7 @@ func register(client autorest.Client, originalReq *http.Request, re RequestError
return originalReq.Context().Err()
}
}
if !(time.Since(now) < client.PollingDuration) {
if client.PollingDuration != 0 && !(time.Since(registrationStartTime) < client.PollingDuration) {
return errors.New("polling for resource provider registration has exceeded the polling duration")
}
return err
Expand Down
1 change: 1 addition & 0 deletions autorest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ type Client struct {
PollingDelay time.Duration

// PollingDuration sets the maximum polling time after which an error is returned.
// if zero, the timeout from the Context is used
PollingDuration time.Duration

// RetryAttempts sets the default number of retry attempts for client.
Expand Down