feat : 로그인 실패시 재시도 로직 추가

This commit is contained in:
qornwh1
2026-03-04 16:12:45 +09:00
parent 18fd8a0737
commit c8ce36a624

View File

@@ -11,16 +11,24 @@ public class RestApi : Singleton<RestApi>
private const string VERIFY_URL = "https://a301.api.tolelom.xyz/api/auth/verify"; private const string VERIFY_URL = "https://a301.api.tolelom.xyz/api/auth/verify";
private readonly HttpClient httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) }; private readonly HttpClient httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
// 토큰 검증 - 성공 시 username 반환, 실패(401/타임아웃 등) 시 null 반환 private const int MAX_RETRY = 3;
private static readonly TimeSpan RETRY_DELAY = TimeSpan.FromSeconds(1);
// 토큰 검증 - 성공 시 username 반환
// 401 → 재시도 없이 즉시 null 반환 (토큰 자체가 무효)
// 타임아웃/네트워크 오류 → 최대 MAX_RETRY회 재시도 후 null 반환
public async Task<string?> VerifyTokenAsync(string token) public async Task<string?> VerifyTokenAsync(string token)
{
for (int attempt = 1; attempt <= MAX_RETRY; attempt++)
{ {
try try
{ {
HttpResponseMessage response = await httpClient.PostAsJsonAsync(VERIFY_URL, new { token }); HttpResponseMessage response = await httpClient.PostAsJsonAsync(VERIFY_URL, new { token });
// 401: 토큰 자체가 무효 → 재시도해도 같은 결과, 즉시 반환
if (response.StatusCode == HttpStatusCode.Unauthorized) if (response.StatusCode == HttpStatusCode.Unauthorized)
{ {
Log.Warning("[RestApi] 인증 실패 (401)"); Log.Warning("[RestApi] 토큰 인증 실패 (401)");
return null; return null;
} }
@@ -29,13 +37,22 @@ public class RestApi : Singleton<RestApi>
AuthVerifyResponse? result = await response.Content.ReadFromJsonAsync<AuthVerifyResponse>(); AuthVerifyResponse? result = await response.Content.ReadFromJsonAsync<AuthVerifyResponse>();
return result?.Username; return result?.Username;
} }
catch (Exception ex) when (attempt < MAX_RETRY)
{
// 일시적 장애 (타임아웃, 네트워크 오류 등) → 재시도
Log.Warning("[RestApi] 통신 실패 (시도 {Attempt}/{Max}): {Message}", attempt, MAX_RETRY, ex.Message);
await Task.Delay(RETRY_DELAY);
}
catch (Exception ex) catch (Exception ex)
{ {
Log.Error("[RestApi] 웹서버 통신 실패: {Message}", ex.Message); // 마지막 시도까지 실패
return null; Log.Error("[RestApi] 최종 통신 실패 ({Max}회 시도): {Message}", MAX_RETRY, ex.Message);
} }
} }
return null;
}
private sealed class AuthVerifyResponse private sealed class AuthVerifyResponse
{ {
[JsonPropertyName("username")] [JsonPropertyName("username")]