Skip to content

HPF PR: dev <- Azure:dev #551

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

Merged
merged 7 commits into from
Jun 15, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Handlers
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;

/// <summary>
/// A basic retry handler.
/// </summary>
Expand Down Expand Up @@ -56,20 +56,21 @@ public class RetryHandler : DelegatingHandler
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
for (int attempt = 0; attempt < RetryHandler.MaxAttempts; ++attempt)

for (int attempt = 1; attempt <= RetryHandler.MaxAttempts; ++attempt)
{
try
{
response = await base
.SendAsync(request: request, cancellationToken: cancellationToken)
.ConfigureAwait(continueOnCapturedContext: false);

if (attempt == RetryHandler.MaxAttempts ||
(!response.StatusCode.IsServerFailureRequest() &&
response.StatusCode != HttpStatusCode.RequestTimeout &&
response.StatusCode != HttpStatusCodeExt.TooManyRequests))
{
return response;
break;
}
}
catch (Exception ex)
Expand All @@ -80,6 +81,11 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
}
}

if (response != null)
{
response.Dispose();
}

await Task.Delay(delay: RetryHandler.GetDelay(attempt), cancellationToken: cancellationToken)
.ConfigureAwait(continueOnCapturedContext: false);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,7 @@
<data name="UpdatedResourceGroup" xml:space="preserve">
<value>Updated resource group '{0}' in location '{1}'</value>
</data>
<data name="OperationFailedWithTimeOut" xml:space="preserve">
<value>Operation failed because a request timed out.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.RestClients
{
using System;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;

/// <summary>
/// A base class for Azure clients.
Expand Down Expand Up @@ -219,27 +220,39 @@ protected async Task<HttpResponseMessage> SendRequestAsync(HttpRequestMessage re
{
using (var httpClient = this.httpClientHelper.CreateHttpClient())
{
var response = await httpClient
.SendAsync(request: request, cancellationToken: cancellationToken)
.ConfigureAwait(continueOnCapturedContext: false);

if (!response.StatusCode.IsSuccessfulRequest())
try
{
var errorResponse = await ResourceManagerRestClientBase
.TryReadErrorResponseMessage(response, rewindContentStream: true)
var response = await httpClient
.SendAsync(request: request, cancellationToken: cancellationToken)
.ConfigureAwait(continueOnCapturedContext: false);

var message = await ResourceManagerRestClientBase
.GetErrorMessage(request: request, response: response, errorResponse: errorResponse)
.ConfigureAwait(continueOnCapturedContext: false);
if (!response.StatusCode.IsSuccessfulRequest())
{
var errorResponse = await ResourceManagerRestClientBase
.TryReadErrorResponseMessage(response, rewindContentStream: true)
.ConfigureAwait(continueOnCapturedContext: false);

throw new ErrorResponseMessageException(
httpStatus: response.StatusCode,
errorResponseMessage: errorResponse,
errorMessage: message);
var message = await ResourceManagerRestClientBase
.GetErrorMessage(request: request, response: response, errorResponse: errorResponse)
.ConfigureAwait(continueOnCapturedContext: false);

throw new ErrorResponseMessageException(
httpStatus: response.StatusCode,
errorResponseMessage: errorResponse,
errorMessage: message);
}

return response;
}
catch (Exception exception)
{
if (exception is OperationCanceledException && !cancellationToken.IsCancellationRequested)
{
throw new Exception(ProjectResources.OperationFailedWithTimeOut);
}

return response;
throw;
}
}
}

Expand Down