StooqHttp.cs 1.1 KB

1234567891011121314151617181920212223242526272829
  1. using Microsoft.Extensions.Logging;
  2. namespace Infrastructure.StockData;
  3. /// <summary>Stooq(stooq.com) 무인증 CSV 시세 호출 공통 — 인증 헤더 없음 + 간단 3회 재시도 (2s/4s 백오프).</summary>
  4. internal static class StooqHttp
  5. {
  6. public const string ClientName = "Stooq";
  7. private const int MaxAttempts = 3;
  8. public static async Task<string> GetStringWithRetryAsync(HttpClient client, string url, ILogger logger, CancellationToken ct)
  9. {
  10. for (var attempt = 1; ; attempt++)
  11. {
  12. try
  13. {
  14. using var response = await client.GetAsync(url, ct);
  15. response.EnsureSuccessStatusCode();
  16. return await response.Content.ReadAsStringAsync(ct);
  17. }
  18. catch (Exception ex) when (ex is not OperationCanceledException && attempt < MaxAttempts)
  19. {
  20. logger.LogWarning(ex, "[Stooq] HTTP 실패 — 재시도 {Attempt}/{Max}", attempt, MaxAttempts);
  21. await Task.Delay(TimeSpan.FromSeconds(2 * attempt), ct);
  22. }
  23. }
  24. }
  25. }