Skip to content

Commit 13f1306

Browse files
committed
final refactor
1 parent 0aa2a2e commit 13f1306

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

DotNet/DataAcquisition.Domain/Application/Models/Exceptions/TooManyRequestsException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace LantanaGroup.Link.DataAcquisition.Domain.Application.Models.Exceptions
55
{
66
public class TooManyRequestsException : FhirOperationException
77
{
8-
public TimeSpan? RetryAfter { get; }
9-
public TooManyRequestsException(string message, TimeSpan? retryAfter = null) : base(message, System.Net.HttpStatusCode.TooManyRequests)
8+
public TimeSpan RetryAfter { get; }
9+
public TooManyRequestsException(string message, TimeSpan retryAfter) : base(message, System.Net.HttpStatusCode.TooManyRequests)
1010
{
1111
RetryAfter = retryAfter;
1212
}

DotNet/DataAcquisition.Domain/Application/Services/FhirApi/Commands/FhirCommandUtils.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
1616

1717
internal static class FhirCommandUtils
1818
{
19-
private const int DEFAULT_DELAY_SECONDS = 60;
20-
21-
public static TimeSpan ParseRetryAfter(HttpResponseHeaders? headers)
19+
public static TimeSpan ParseRetryAfter(HttpResponseHeaders? headers, TimeSpan defaultDelay = default)
2220
{
21+
defaultDelay = defaultDelay == default ? TimeSpan.FromSeconds(60) : defaultDelay;
22+
2323
if (headers == null || headers.RetryAfter == null)
2424
{
25-
return DateTime.UtcNow.AddSeconds(DEFAULT_DELAY_SECONDS).TimeOfDay;
25+
return defaultDelay;
2626
}
2727

2828
var retryValue = headers.RetryAfter;
29-
TimeSpan? delay = null;
29+
TimeSpan delay;
3030

3131
if (retryValue.Delta.HasValue)
3232
{
@@ -36,12 +36,16 @@ public static TimeSpan ParseRetryAfter(HttpResponseHeaders? headers)
3636
{
3737
delay = retryValue.Date.Value - DateTimeOffset.UtcNow;
3838
}
39+
else
40+
{
41+
return defaultDelay;
42+
}
3943

40-
if (!delay.HasValue || delay.Value <= TimeSpan.Zero)
44+
if (delay <= TimeSpan.Zero)
4145
{
42-
delay = DateTime.UtcNow.AddSeconds(DEFAULT_DELAY_SECONDS).TimeOfDay;
46+
return defaultDelay;
4347
}
4448

45-
return delay.Value;
49+
return delay;
4650
}
4751
}

DotNet/DataAcquisition.Domain/Application/Services/PatientDataService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,9 @@ await _dataAcquisitionLogManager.UpdateAsync(new UpdateDataAcquisitionLogModel
553553

554554
log.RetryAttempts ??= 0;
555555

556-
var delay = ex.RetryAfter ?? TimeSpan.FromSeconds(Math.Min(Math.Pow(2, log.RetryAttempts.Value), 60));
557-
log.ExecutionDate = DateTime.UtcNow.Add(delay);
556+
log.ExecutionDate = DateTime.UtcNow.Add(ex.RetryAfter);
558557
log.Status = RequestStatus.Pending; //Don't count this as a failure
559-
log.Notes.Add($"[{DateTime.UtcNow}] Throttled (429): Retrying after {delay.TotalSeconds}s. Attempt {log.RetryAttempts}.");
558+
log.Notes.Add($"[{DateTime.UtcNow}] Throttled (429): Retrying after {ex.RetryAfter.TotalSeconds}s. Attempt {log.RetryAttempts}.");
560559

561560
await _dataAcquisitionLogManager.UpdateAsync(new UpdateDataAcquisitionLogModel
562561
{

0 commit comments

Comments
 (0)