From 50fbf0ad52b9e5336b89658ce36bdace36eeb4a8 Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 23 Oct 2023 10:11:02 +0200 Subject: [PATCH] Replaces retry-time with maxNumberOfRetries in timesets. --- Framework/Core/Http.cs | 2 +- Framework/Core/TimeSet.cs | 12 ++++++------ Framework/Utils/Time.cs | 30 ++++++++++++++++++------------ Tools/CodexNetDeployer/Deployer.cs | 4 ++-- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Framework/Core/Http.cs b/Framework/Core/Http.cs index 60e52db..18fc5c7 100644 --- a/Framework/Core/Http.cs +++ b/Framework/Core/Http.cs @@ -196,7 +196,7 @@ namespace Core private T Retry(Func operation, string description) { - return Time.Retry(operation, timeSet.HttpCallRetryTime(), timeSet.HttpCallRetryDelay(), description); + return Time.Retry(operation, timeSet.HttpMaxNumberOfRetries(), timeSet.HttpCallRetryDelay(), description); } private HttpClient GetClient() diff --git a/Framework/Core/TimeSet.cs b/Framework/Core/TimeSet.cs index 2ca047f..3acd852 100644 --- a/Framework/Core/TimeSet.cs +++ b/Framework/Core/TimeSet.cs @@ -3,7 +3,7 @@ public interface ITimeSet { TimeSpan HttpCallTimeout(); - TimeSpan HttpCallRetryTime(); + int HttpMaxNumberOfRetries(); TimeSpan HttpCallRetryDelay(); TimeSpan WaitForK8sServiceDelay(); TimeSpan K8sOperationTimeout(); @@ -13,12 +13,12 @@ { public TimeSpan HttpCallTimeout() { - return TimeSpan.FromMinutes(5); + return TimeSpan.FromMinutes(3); } - public TimeSpan HttpCallRetryTime() + public int HttpMaxNumberOfRetries() { - return TimeSpan.FromMinutes(1); + return 3; } public TimeSpan HttpCallRetryDelay() @@ -44,9 +44,9 @@ return TimeSpan.FromHours(2); } - public TimeSpan HttpCallRetryTime() + public int HttpMaxNumberOfRetries() { - return TimeSpan.FromHours(5); + return 1; } public TimeSpan HttpCallRetryDelay() diff --git a/Framework/Utils/Time.cs b/Framework/Utils/Time.cs index bf0e116..3a53b72 100644 --- a/Framework/Utils/Time.cs +++ b/Framework/Utils/Time.cs @@ -46,33 +46,35 @@ public static void Retry(Action action, string description) { - Retry(action, TimeSpan.FromMinutes(1), description); + Retry(action, 1, description); } public static T Retry(Func action, string description) { - return Retry(action, TimeSpan.FromMinutes(1), description); + return Retry(action, 1, description); } - public static void Retry(Action action, TimeSpan timeout, string description) + public static void Retry(Action action, int maxRetries, string description) { - Retry(action, timeout, TimeSpan.FromSeconds(1), description); + Retry(action, maxRetries, TimeSpan.FromSeconds(1), description); } - public static T Retry(Func action, TimeSpan timeout, string description) + public static T Retry(Func action, int maxRetries, string description) { - return Retry(action, timeout, TimeSpan.FromSeconds(1), description); + return Retry(action, maxRetries, TimeSpan.FromSeconds(1), description); } - public static void Retry(Action action, TimeSpan timeout, TimeSpan retryTime, string description) + public static void Retry(Action action, int maxRetries, TimeSpan retryTime, string description) { var start = DateTime.UtcNow; + var retries = 0; var exceptions = new List(); while (true) { - if (DateTime.UtcNow - start > timeout) + if (retries > maxRetries) { - throw new TimeoutException($"Retry '{description}' of {timeout.TotalSeconds} seconds timed out.", new AggregateException(exceptions)); + var duration = DateTime.UtcNow - start; + throw new TimeoutException($"Retry '{description}' timed out after {maxRetries} tries over {Time.FormatDuration(duration)}.", new AggregateException(exceptions)); } try @@ -83,21 +85,24 @@ catch (Exception ex) { exceptions.Add(ex); + retries++; } Sleep(retryTime); } } - public static T Retry(Func action, TimeSpan timeout, TimeSpan retryTime, string description) + public static T Retry(Func action, int maxRetries, TimeSpan retryTime, string description) { var start = DateTime.UtcNow; + var retries = 0; var exceptions = new List(); while (true) { - if (DateTime.UtcNow - start > timeout) + if (retries > maxRetries) { - throw new TimeoutException($"Retry '{description}' of {timeout.TotalSeconds} seconds timed out.", new AggregateException(exceptions)); + var duration = DateTime.UtcNow - start; + throw new TimeoutException($"Retry '{description}' timed out after {maxRetries} tries over {Time.FormatDuration(duration)}.", new AggregateException(exceptions)); } try @@ -107,6 +112,7 @@ catch (Exception ex) { exceptions.Add(ex); + retries++; } Sleep(retryTime); diff --git a/Tools/CodexNetDeployer/Deployer.cs b/Tools/CodexNetDeployer/Deployer.cs index 796fca8..29f4348 100644 --- a/Tools/CodexNetDeployer/Deployer.cs +++ b/Tools/CodexNetDeployer/Deployer.cs @@ -180,9 +180,9 @@ namespace CodexNetDeployer return TimeSpan.FromSeconds(2); } - public TimeSpan HttpCallRetryTime() + public int HttpMaxNumberOfRetries() { - return TimeSpan.FromSeconds(2); + return 2; } public TimeSpan HttpCallTimeout()