| | | 1 | | using System.Net; |
| | | 2 | | |
| | | 3 | | namespace Intentum.AI.Http; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Shared HTTP retry handler for embedding providers. Retries on 429 (Too Many Requests) |
| | | 7 | | /// with exponential backoff and Retry-After header support. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class EmbeddingHttpRetryHandler |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Sends an HTTP request with retry logic for rate limiting (429). |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="sendRequest">Factory that produces the HTTP request for each attempt.</param> |
| | | 15 | | /// <param name="onRateLimitExhausted">Called when all retries are exhausted on 429. Should throw a provider-specifi |
| | | 16 | | /// <param name="maxAttempts">Maximum number of retry attempts.</param> |
| | | 17 | | /// <param name="maxWaitSeconds">Maximum wait time in seconds per retry.</param> |
| | | 18 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | | 19 | | /// <returns>A successful HttpResponseMessage.</returns> |
| | | 20 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Maintainability", "S3966:Objects should not be disposed more than |
| | | 21 | | public static async Task<HttpResponseMessage> SendWithRetryAsync( |
| | | 22 | | Func<CancellationToken, Task<HttpResponseMessage>> sendRequest, |
| | | 23 | | Action<double?, string?> onRateLimitExhausted, |
| | | 24 | | int maxAttempts = 5, |
| | | 25 | | int maxWaitSeconds = 90, |
| | | 26 | | CancellationToken cancellationToken = default) |
| | 13 | 27 | | { |
| | 13 | 28 | | HttpResponseMessage? response = null; |
| | | 29 | | |
| | 30 | 30 | | for (var attempt = 1; attempt <= maxAttempts; attempt++) |
| | 15 | 31 | | { |
| | 15 | 32 | | response?.Dispose(); |
| | 15 | 33 | | response = await sendRequest(cancellationToken); |
| | | 34 | | |
| | 15 | 35 | | if (response.IsSuccessStatusCode) |
| | 10 | 36 | | return response; |
| | | 37 | | |
| | 5 | 38 | | if (response.StatusCode == HttpStatusCode.TooManyRequests && attempt == maxAttempts) |
| | 1 | 39 | | { |
| | 1 | 40 | | var retryAfterSec = response.Headers.RetryAfter?.Delta?.TotalSeconds; |
| | 1 | 41 | | var body = await response.Content.ReadAsStringAsync(cancellationToken); |
| | 1 | 42 | | response.Dispose(); |
| | 1 | 43 | | onRateLimitExhausted(retryAfterSec, body); |
| | 0 | 44 | | } |
| | | 45 | | |
| | 4 | 46 | | if (response.StatusCode != HttpStatusCode.TooManyRequests) |
| | 2 | 47 | | response.EnsureSuccessStatusCode(); |
| | | 48 | | |
| | 2 | 49 | | var delay = TimeSpan.FromSeconds(5 * attempt); |
| | 2 | 50 | | if (response.Headers.RetryAfter?.Delta is { } retryAfter) |
| | 1 | 51 | | delay = TimeSpan.FromSeconds(Math.Min(retryAfter.TotalSeconds, maxWaitSeconds)); |
| | | 52 | | |
| | 2 | 53 | | await Task.Delay(delay, cancellationToken); |
| | 2 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | response!.EnsureSuccessStatusCode(); |
| | 0 | 57 | | return response; |
| | 10 | 58 | | } |
| | | 59 | | } |