Skip to content

Commit 9abaf17

Browse files
committed
Updating Cancel handler to create new cancellation tokens
1 parent 03b1e86 commit 9abaf17

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/Common/Commands.Common.Authentication/Factories/CancelRetryHandler.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@
1919

2020
namespace Microsoft.Azure.Commands.Common.Authentication.Factories
2121
{
22+
/// <summary>
23+
/// Class to enable automatic retry of TaskCanceledExsceptions.
24+
/// Note that this follows the calling pattern in PowerShell, which does not pass cancellation tokens from
25+
/// the base cmdlet
26+
/// </summary>
2227
public class CancelRetryHandler : DelegatingHandler, ICloneable
2328
{
2429
public CancelRetryHandler()
2530
{
2631
WaitInterval = TimeSpan.Zero;
2732
}
28-
public CancelRetryHandler(TimeSpan waitInterval)
33+
public CancelRetryHandler(TimeSpan waitInterval, int maxTries)
2934
{
3035
WaitInterval = waitInterval;
36+
MaxTries = maxTries;
3137
}
3238

33-
public TimeSpan WaitInterval { get; set; }
39+
public TimeSpan WaitInterval { get; set; } = TimeSpan.Zero;
3440

3541
public int MaxTries { get; set; } = 3;
3642

@@ -39,21 +45,24 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
3945
int tries = 0;
4046
do
4147
{
42-
try
48+
using (var source = new CancellationTokenSource())
4349
{
44-
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
45-
}
46-
catch (TaskCanceledException) when (tries++ < MaxTries)
47-
{
48-
Thread.Sleep(WaitInterval);
50+
try
51+
{
52+
return await base.SendAsync(request, source.Token).ConfigureAwait(false);
53+
}
54+
catch (TaskCanceledException) when (tries++ < MaxTries)
55+
{
56+
Thread.Sleep(WaitInterval);
57+
}
4958
}
5059
}
5160
while (true);
5261
}
5362

5463
public object Clone()
5564
{
56-
return new CancelRetryHandler(WaitInterval);
65+
return new CancelRetryHandler(WaitInterval, MaxTries);
5766
}
5867
}
5968
}

src/Common/Commands.Common.Authentication/Factories/ClientFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace Microsoft.Azure.Commands.Common.Authentication.Factories
3232
public class ClientFactory : IClientFactory
3333
{
3434
private static readonly char[] uriPathSeparator = { '/' };
35+
private static readonly CancelRetryHandler DefaultCancelRetryHandler = new CancelRetryHandler(TimeSpan.FromSeconds(10), 3);
3536

3637
private Dictionary<Type, IClientAction> _actions;
3738
private OrderedDictionary _handlers;
@@ -71,7 +72,7 @@ public virtual TClient CreateCustomArmClient<TClient>(params object[] parameters
7172
{
7273
List<Type> types = new List<Type>();
7374
List<object> parameterList = new List<object>();
74-
List<DelegatingHandler> handlerList = new List<DelegatingHandler> { new CancelRetryHandler(TimeSpan.FromSeconds(10))};
75+
List<DelegatingHandler> handlerList = new List<DelegatingHandler> { DefaultCancelRetryHandler.Clone() as CancelRetryHandler};
7576
var customHandlers = GetCustomHandlers();
7677
if (customHandlers != null && customHandlers.Any())
7778
{
@@ -218,7 +219,7 @@ public virtual TClient CreateCustomClient<TClient>(params object[] parameters) w
218219
client.AddHandlerToPipeline(handler);
219220
}
220221

221-
client.AddHandlerToPipeline(new CancelRetryHandler(TimeSpan.FromSeconds(10)));
222+
client.AddHandlerToPipeline(DefaultCancelRetryHandler.Clone() as CancelRetryHandler);
222223
return client;
223224
}
224225

0 commit comments

Comments
 (0)