|
1 | 1 | package sopt.comfit.report.infra.webclient; |
2 | 2 |
|
| 3 | +import io.github.resilience4j.circuitbreaker.CircuitBreaker; |
| 4 | +import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; |
| 5 | +import io.github.resilience4j.retry.annotation.Retry; |
| 6 | +import io.github.resilience4j.timelimiter.annotation.TimeLimiter; |
3 | 7 | import lombok.RequiredArgsConstructor; |
4 | 8 | import lombok.extern.slf4j.Slf4j; |
5 | 9 | import org.springframework.stereotype.Component; |
6 | 10 | import org.springframework.web.reactive.function.client.WebClient; |
7 | 11 | import reactor.core.publisher.Mono; |
| 12 | +import sopt.comfit.global.exception.BaseException; |
| 13 | +import sopt.comfit.report.exception.AIReportErrorCode; |
8 | 14 | import sopt.comfit.report.infra.dto.CreateReportAiRequestDto; |
9 | 15 | import sopt.comfit.report.infra.dto.CreateReportAiResponseDto; |
10 | 16 |
|
|
14 | 20 | public class AiWebClient { |
15 | 21 |
|
16 | 22 | private final WebClient webClient; |
| 23 | + private final CircuitBreakerRegistry circuitBreakerRegistry; |
17 | 24 |
|
| 25 | + @io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker(name = "openai", fallbackMethod = "createReportFallback") |
| 26 | + @Retry(name = "openai") |
| 27 | + @TimeLimiter(name = "openai") |
18 | 28 | public Mono<CreateReportAiResponseDto> createReport(CreateReportAiRequestDto request){ |
19 | 29 | long startTIme = System.currentTimeMillis(); |
20 | 30 |
|
21 | 31 | return webClient.post() |
22 | 32 | .uri("/v1/chat/completions") |
23 | 33 | .bodyValue(request) |
24 | 34 | .retrieve() |
| 35 | + .onStatus(status -> status.value() == 401, |
| 36 | + response -> Mono.error(BaseException.type(AIReportErrorCode.AI_AUTH_FAILED))) |
| 37 | + .onStatus(status -> status.value() == 429, |
| 38 | + response -> Mono.error(BaseException.type(AIReportErrorCode.AI_RATE_LIMITED))) |
| 39 | + .onStatus(status -> status.value() >= 500, |
| 40 | + response -> Mono.error(BaseException.type(AIReportErrorCode.AI_SERVER_ERROR))) |
25 | 41 | .bodyToMono(CreateReportAiResponseDto.class) |
26 | 42 | .doOnSubscribe(subscription -> |
27 | | - log.info("OpenAI API 호출 시작 (WebClient)")) |
| 43 | + log.info("OpenAI API 호출 시작")) |
28 | 44 | .doOnSuccess(response -> |
29 | | - log.info("OpenAI API 호출 완료(WebClient) - duration : {} ms", |
| 45 | + log.info("OpenAI API 호출 완료 - duration: {}ms", |
30 | 46 | System.currentTimeMillis() - startTIme)) |
31 | 47 | .doOnError(error -> |
32 | | - log.error("OpenAI API 호출 실패 (WebClient)", error)); |
| 48 | + log.error("OpenAI API 호출 실패", error)); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + private Mono<CreateReportAiResponseDto> createReportFallback( |
| 53 | + CreateReportAiRequestDto request, Throwable t) { |
| 54 | + |
| 55 | + CircuitBreaker cb = circuitBreakerRegistry.circuitBreaker("openai"); |
| 56 | + |
| 57 | + if (cb.getState() == CircuitBreaker.State.OPEN) { |
| 58 | + return Mono.error(BaseException.type(AIReportErrorCode.AI_SERVICE_UNAVAILABLE)); |
| 59 | + } |
| 60 | + |
| 61 | + if (t instanceof BaseException) { |
| 62 | + return Mono.error(t); // 401, 429 등 그대로 |
| 63 | + } |
| 64 | + |
| 65 | + log.warn("OpenAI fallback 실행 - 원인: {}", t.getMessage()); |
| 66 | + return Mono.error(BaseException.type(AIReportErrorCode.AI_SERVICE_UNAVAILABLE)); |
33 | 67 | } |
34 | 68 | } |
0 commit comments