Spaces:
Running
Running
Commit ·
ec80222
1
Parent(s): 7fc9995
feat(AI): add retry loop with exponential backoff for transient HTTP status codes (503, 429, 500)
Browse files177 unmodified lines178179180181182183184185186181182183184185186187188189190191192193194195196197198199200201202203204205206207208177 unmodified lines
using var httpClient = new HttpClient(); var jsonPayload = JsonSerializer.Serialize(requestBody); var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(url, content); var responseString = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode) HttpResponseMessage? response = null; string responseString = string.Empty; int maxRetries = 3; int delayMs = 1500; // 1.5s base delay for (int attempt = 1; attempt <= maxRetries; attempt++) { using var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); response = await httpClient.PostAsync(url, content); responseString = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { break; } int statusCode = (int)response.StatusCode; if (attempt < maxRetries && (statusCode == 503 || statusCode == 429 || statusCode == 500)) { await Task.Delay(delayMs * attempt); continue; } throw new Exception($"Gemini API error (Status {response.StatusCode}): {responseString}"); }
FlowAPI.Application/Services/AIService.cs+24-6