Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/EfficientDynamoDb/Configs/Retries/IRetryStrategies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ public class RetryStrategies
/// Retry strategy for <see cref="ThrottlingException"/>
/// </summary>
public IRetryStrategy ThrottlingStrategy { get; set; } = RetryStrategyFactory.Jitter();

/// <summary>
/// Retry strategy for <see ref="System.IO.IOException"/> or <see ref="System.Net.Http.HttpIOException"/> or <see cref="System.Net.Sockets.SocketException"/>.
/// </summary>
public IRetryStrategy IoExceptionStrategy { get; set; } = RetryStrategyFactory.Linear(maxRetriesCount: 10);
}
}
22 changes: 19 additions & 3 deletions src/EfficientDynamoDb/Internal/HttpApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Net.Http;
using System.Net.Sockets;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -40,7 +42,9 @@ public async ValueTask<HttpResponseMessage> SendAsync(DynamoDbContextConfig conf
provisionedThroughputExceededRetries = 0,
requestLimitExceededRetries = 0,
serviceUnavailableRetries = 0,
throttlingRetries = 0;
throttlingRetries = 0,
ioRetries = 0;

while (true)
{
using var request = new HttpRequestMessage(HttpMethod.Post, config.RegionEndpoint.RequestUri);
Expand All @@ -49,7 +53,7 @@ public async ValueTask<HttpResponseMessage> SendAsync(DynamoDbContextConfig conf
try
{
var httpClient = _httpClientFactory.CreateHttpClient();
var stream = await httpContent.ReadAsStreamAsync().ConfigureAwait(false);
var stream = await httpContent.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
var credentials = await config.CredentialsProvider.GetCredentialsAsync(cancellationToken).ConfigureAwait(false);

var metadata = new SigningMetadata(config.RegionEndpoint, credentials, DateTime.UtcNow, httpClient.DefaultRequestHeaders, httpClient.BaseAddress);
Expand All @@ -76,6 +80,18 @@ public async ValueTask<HttpResponseMessage> SendAsync(DynamoDbContextConfig conf
return (null, error);
}
}
catch (HttpRequestException ex) when (ex.InnerException is IOException or SocketException)
{
if (config.RetryStrategies.IoExceptionStrategy.TryGetRetryDelay(ioRetries++, out var delay))
{
await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
}
else
{
// Exception is not caused by DDB but by network issues so we're not returning DdbException here.
throw;
}
}
finally
{
request.Content = null;
Expand All @@ -92,7 +108,7 @@ public async ValueTask<TResponse> SendAsync<TResponse>(DynamoDbContextConfig con
{
using var response = await SendAsync(config, httpContent, cancellationToken).ConfigureAwait(false);

await using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
return (await JsonSerializer.DeserializeAsync<TResponse>(responseStream,
new JsonSerializerOptions {Converters = {new DdbEnumJsonConverterFactory(), new UnixDateTimeJsonConverter()}}, cancellationToken).ConfigureAwait(false))!;
}
Expand Down