Replaces retry-time with maxNumberOfRetries in timesets.

This commit is contained in:
benbierens 2023-10-23 10:11:02 +02:00
parent 45fbd699a9
commit 50fbf0ad52
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 27 additions and 21 deletions

View File

@ -196,7 +196,7 @@ namespace Core
private T Retry<T>(Func<T> operation, string description) private T Retry<T>(Func<T> operation, string description)
{ {
return Time.Retry(operation, timeSet.HttpCallRetryTime(), timeSet.HttpCallRetryDelay(), description); return Time.Retry(operation, timeSet.HttpMaxNumberOfRetries(), timeSet.HttpCallRetryDelay(), description);
} }
private HttpClient GetClient() private HttpClient GetClient()

View File

@ -3,7 +3,7 @@
public interface ITimeSet public interface ITimeSet
{ {
TimeSpan HttpCallTimeout(); TimeSpan HttpCallTimeout();
TimeSpan HttpCallRetryTime(); int HttpMaxNumberOfRetries();
TimeSpan HttpCallRetryDelay(); TimeSpan HttpCallRetryDelay();
TimeSpan WaitForK8sServiceDelay(); TimeSpan WaitForK8sServiceDelay();
TimeSpan K8sOperationTimeout(); TimeSpan K8sOperationTimeout();
@ -13,12 +13,12 @@
{ {
public TimeSpan HttpCallTimeout() 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() public TimeSpan HttpCallRetryDelay()
@ -44,9 +44,9 @@
return TimeSpan.FromHours(2); return TimeSpan.FromHours(2);
} }
public TimeSpan HttpCallRetryTime() public int HttpMaxNumberOfRetries()
{ {
return TimeSpan.FromHours(5); return 1;
} }
public TimeSpan HttpCallRetryDelay() public TimeSpan HttpCallRetryDelay()

View File

@ -46,33 +46,35 @@
public static void Retry(Action action, string description) public static void Retry(Action action, string description)
{ {
Retry(action, TimeSpan.FromMinutes(1), description); Retry(action, 1, description);
} }
public static T Retry<T>(Func<T> action, string description) public static T Retry<T>(Func<T> 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<T>(Func<T> action, TimeSpan timeout, string description) public static T Retry<T>(Func<T> 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 start = DateTime.UtcNow;
var retries = 0;
var exceptions = new List<Exception>(); var exceptions = new List<Exception>();
while (true) 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 try
@ -83,21 +85,24 @@
catch (Exception ex) catch (Exception ex)
{ {
exceptions.Add(ex); exceptions.Add(ex);
retries++;
} }
Sleep(retryTime); Sleep(retryTime);
} }
} }
public static T Retry<T>(Func<T> action, TimeSpan timeout, TimeSpan retryTime, string description) public static T Retry<T>(Func<T> action, int maxRetries, TimeSpan retryTime, string description)
{ {
var start = DateTime.UtcNow; var start = DateTime.UtcNow;
var retries = 0;
var exceptions = new List<Exception>(); var exceptions = new List<Exception>();
while (true) 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 try
@ -107,6 +112,7 @@
catch (Exception ex) catch (Exception ex)
{ {
exceptions.Add(ex); exceptions.Add(ex);
retries++;
} }
Sleep(retryTime); Sleep(retryTime);

View File

@ -180,9 +180,9 @@ namespace CodexNetDeployer
return TimeSpan.FromSeconds(2); return TimeSpan.FromSeconds(2);
} }
public TimeSpan HttpCallRetryTime() public int HttpMaxNumberOfRetries()
{ {
return TimeSpan.FromSeconds(2); return 2;
} }
public TimeSpan HttpCallTimeout() public TimeSpan HttpCallTimeout()