From 695ec2a026efe87423cce1f3a6c737fac82a9cf0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 28 Jun 2021 08:44:02 -0500 Subject: stub Retry lib --- src/slskd/Common/Retry.cs | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/slskd/Common/Retry.cs (limited to 'src') 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 (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/. +// + +namespace slskd +{ + using System; + using System.Collections.Generic; + using System.Threading.Tasks; + + public class Retry + { + public static async Task Do(Func task, TimeSpan retryInterval, int maxAttemptCount = 3) + { + var exceptions = new List(); + 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 Do(Func> task, TimeSpan retryInterval, int maxAttemptCount = 3) + { + var exceptions = new List(); + 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 -- cgit v1.2.3