diff --git a/src/Testcontainers.Couchbase/CouchbaseBuilder.cs b/src/Testcontainers.Couchbase/CouchbaseBuilder.cs
index d5892a0ee..c89968f11 100644
--- a/src/Testcontainers.Couchbase/CouchbaseBuilder.cs
+++ b/src/Testcontainers.Couchbase/CouchbaseBuilder.cs
@@ -186,7 +186,7 @@ private async Task ConfigureCouchbaseAsync(IContainer container, CancellationTok
await WaitStrategy.WaitUntilAsync(() => WaitUntilNodeIsReady.UntilAsync(container), TimeSpan.FromSeconds(2), TimeSpan.FromMinutes(5), ct)
.ConfigureAwait(false);
- using (var httpClient = new HttpClient())
+ using (var httpClient = new HttpClient(new RetryHandler()))
{
httpClient.BaseAddress = new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPort(MgmtPort)).Uri;
@@ -540,4 +540,47 @@ public CreateBucketRequest(CouchbaseBucket bucket)
Content = new FormUrlEncodedContent(content);
}
}
+
+ ///
+ /// An HTTP retry handler that sends an HTTP request until it succeeds.
+ ///
+ ///
+ /// Sending an HTTP request to Couchbase's API sometimes fails with the following
+ /// error: System.Net.Http.HttpIOException: The response ended prematurely (ResponseEnded).
+ /// The HTTP status code 504 indicates an issue with the Couchbase backend.
+ /// It is likely that the API is not yet ready to process HTTP requests.
+ /// Typically, trying it again resolves the issue.
+ ///
+ private sealed class RetryHandler : DelegatingHandler
+ {
+ private const int MaxRetries = 5;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public RetryHandler()
+ : base(new HttpClientHandler())
+ {
+ }
+
+ ///
+ protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+ {
+ for (var _ = 0; _ < MaxRetries; _++)
+ {
+ try
+ {
+ return await base.SendAsync(request, cancellationToken)
+ .ConfigureAwait(false);
+ }
+ catch (HttpRequestException)
+ {
+ await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken)
+ .ConfigureAwait(false);
+ }
+ }
+
+ throw new Exception($"Unable to configure Couchbase. The HTTP request '{request.RequestUri}' did not complete successfully.");
+ }
+ }
}
\ No newline at end of file