diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-28 08:44:02 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-06-28 08:44:02 -0500 |
| commit | 695ec2a026efe87423cce1f3a6c737fac82a9cf0 (patch) | |
| tree | f3c357cbf99d05fa36148c5a05f1ef953585d87d | |
| parent | 49b4b712e59e1591f636201988e45b3ff87c2406 (diff) | |
| download | slskd-695ec2a026efe87423cce1f3a6c737fac82a9cf0.tar.gz | |
stub Retry lib
| -rw-r--r-- | src/slskd/Common/Retry.cs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs new file mode 100644 index 00000000..c0e1bd83 --- /dev/null +++ b/src/slskd/Common/Retry.cs @@ -0,0 +1,73 @@ +// <copyright file="Retry.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System; + using System.Collections.Generic; + using System.Threading.Tasks; + + public class Retry + { + public static async Task Do(Func<Task> task, TimeSpan retryInterval, int maxAttemptCount = 3) + { + var exceptions = new List<Exception>(); + for (int attempted = 0; attempted < maxAttemptCount; attempted++) + { + try + { + if (attempted > 0) + { + await Task.Delay(retryInterval); + } + + await task(); + return; + } + catch (Exception ex) + { + exceptions.Add(ex); + } + } + + throw new AggregateException(exceptions); + } + + public static async Task<T> Do<T>(Func<Task<T>> task, TimeSpan retryInterval, int maxAttemptCount = 3) + { + var exceptions = new List<Exception>(); + for (int attempted = 0; attempted < maxAttemptCount; attempted++) + { + try + { + if (attempted > 0) + { + await Task.Delay(retryInterval); + } + + return await task(); + } + catch (Exception ex) + { + exceptions.Add(ex); + } + } + + throw new AggregateException(exceptions); + } + } +}
\ No newline at end of file |
