microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Common/Extensions/TaskExtensions.cs
29lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Microsoft.Teams.Common.Extensions; |
| 5 | |
| 6 | public static class TaskExtensions |
| 7 | { |
| 8 | public static async Task<T> Retry<T>(Func<Task<T>> taskFactory, int max = 3, int delay = 200) |
| 9 | { |
| 10 | try |
| 11 | { |
| 12 | return await taskFactory().ConfigureAwait(false); |
| 13 | } |
| 14 | // don't retry cancelled |
| 15 | catch (OperationCanceledException) when (max > 0) |
| 16 | { |
| 17 | throw; |
| 18 | } |
| 19 | catch (Exception ex) |
| 20 | { |
| 21 | if (max > 0) |
| 22 | { |
| 23 | await Task.Delay(delay); |
| 24 | return await Retry(taskFactory, max - 1, delay * 2).ConfigureAwait(false); |
| 25 | } |
| 26 | throw new Exception(ex.Message, ex); |
| 27 | } |
| 28 | } |
| 29 | } |