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