Skip to content

Commit 339aeb6

Browse files
authored
Merge pull request #2412 from TianoMS/tiano-d2
Dispose response messages when we retry sending requests.
2 parents 33bdb20 + c591214 commit 339aeb6

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Handlers/RetryHandler.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Handlers
1616
{
17-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
18-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
19-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
2017
using System;
2118
using System.Net;
2219
using System.Net.Http;
2320
using System.Threading;
2421
using System.Threading.Tasks;
25-
22+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
23+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
24+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
25+
2626
/// <summary>
2727
/// A basic retry handler.
2828
/// </summary>
@@ -56,20 +56,21 @@ public class RetryHandler : DelegatingHandler
5656
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
5757
{
5858
HttpResponseMessage response = null;
59-
for (int attempt = 0; attempt < RetryHandler.MaxAttempts; ++attempt)
59+
60+
for (int attempt = 1; attempt <= RetryHandler.MaxAttempts; ++attempt)
6061
{
6162
try
6263
{
6364
response = await base
6465
.SendAsync(request: request, cancellationToken: cancellationToken)
6566
.ConfigureAwait(continueOnCapturedContext: false);
66-
67+
6768
if (attempt == RetryHandler.MaxAttempts ||
6869
(!response.StatusCode.IsServerFailureRequest() &&
6970
response.StatusCode != HttpStatusCode.RequestTimeout &&
7071
response.StatusCode != HttpStatusCodeExt.TooManyRequests))
7172
{
72-
return response;
73+
break;
7374
}
7475
}
7576
catch (Exception ex)
@@ -80,6 +81,11 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
8081
}
8182
}
8283

84+
if (response != null)
85+
{
86+
response.Dispose();
87+
}
88+
8389
await Task.Delay(delay: RetryHandler.GetDelay(attempt), cancellationToken: cancellationToken)
8490
.ConfigureAwait(continueOnCapturedContext: false);
8591
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,7 @@
396396
<data name="UpdatedResourceGroup" xml:space="preserve">
397397
<value>Updated resource group '{0}' in location '{1}'</value>
398398
</data>
399+
<data name="OperationFailedWithTimeOut" xml:space="preserve">
400+
<value>Operation failed because a request timed out.</value>
401+
</data>
399402
</root>

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/RestClients/ResourceManagerRestClientBase.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.RestClients
1616
{
17+
using System;
18+
using System.Net.Http;
19+
using System.Text;
20+
using System.Threading;
21+
using System.Threading.Tasks;
1722
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1823
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
1924
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2025
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
2126
using Newtonsoft.Json;
2227
using Newtonsoft.Json.Linq;
23-
using System;
24-
using System.Net.Http;
25-
using System.Text;
26-
using System.Threading;
27-
using System.Threading.Tasks;
28+
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
2829

2930
/// <summary>
3031
/// A base class for Azure clients.
@@ -219,27 +220,39 @@ protected async Task<HttpResponseMessage> SendRequestAsync(HttpRequestMessage re
219220
{
220221
using (var httpClient = this.httpClientHelper.CreateHttpClient())
221222
{
222-
var response = await httpClient
223-
.SendAsync(request: request, cancellationToken: cancellationToken)
224-
.ConfigureAwait(continueOnCapturedContext: false);
225-
226-
if (!response.StatusCode.IsSuccessfulRequest())
223+
try
227224
{
228-
var errorResponse = await ResourceManagerRestClientBase
229-
.TryReadErrorResponseMessage(response, rewindContentStream: true)
225+
var response = await httpClient
226+
.SendAsync(request: request, cancellationToken: cancellationToken)
230227
.ConfigureAwait(continueOnCapturedContext: false);
231228

232-
var message = await ResourceManagerRestClientBase
233-
.GetErrorMessage(request: request, response: response, errorResponse: errorResponse)
234-
.ConfigureAwait(continueOnCapturedContext: false);
229+
if (!response.StatusCode.IsSuccessfulRequest())
230+
{
231+
var errorResponse = await ResourceManagerRestClientBase
232+
.TryReadErrorResponseMessage(response, rewindContentStream: true)
233+
.ConfigureAwait(continueOnCapturedContext: false);
235234

236-
throw new ErrorResponseMessageException(
237-
httpStatus: response.StatusCode,
238-
errorResponseMessage: errorResponse,
239-
errorMessage: message);
235+
var message = await ResourceManagerRestClientBase
236+
.GetErrorMessage(request: request, response: response, errorResponse: errorResponse)
237+
.ConfigureAwait(continueOnCapturedContext: false);
238+
239+
throw new ErrorResponseMessageException(
240+
httpStatus: response.StatusCode,
241+
errorResponseMessage: errorResponse,
242+
errorMessage: message);
243+
}
244+
245+
return response;
240246
}
247+
catch (Exception exception)
248+
{
249+
if (exception is OperationCanceledException && !cancellationToken.IsCancellationRequested)
250+
{
251+
throw new Exception(ProjectResources.OperationFailedWithTimeOut);
252+
}
241253

242-
return response;
254+
throw;
255+
}
243256
}
244257
}
245258

0 commit comments

Comments
 (0)