using Microsoft.Extensions.Logging;
namespace Infrastructure.StockData;
/// data.go.kr 호출 공통 — 간단 3회 재시도 (2s/4s 백오프)
internal static class DataGoKrHttp
{
public const string ClientName = "DataGoKr";
private const int MaxAttempts = 3;
public static async Task GetStringWithRetryAsync(HttpClient client, string url, ILogger logger, CancellationToken ct)
{
for (var attempt = 1; ; attempt++)
{
try
{
using var response = await client.GetAsync(url, ct);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync(ct);
}
catch (Exception ex) when (ex is not OperationCanceledException && attempt < MaxAttempts)
{
logger.LogWarning(ex, "[DataGoKr] HTTP 실패 — 재시도 {Attempt}/{Max}", attempt, MaxAttempts);
await Task.Delay(TimeSpan.FromSeconds(2 * attempt), ct);
}
}
}
}