Files
nahodka-back/src/main/java/com/krylov/refound/ai/client/TextModerationClient.java
SlimusMinus 3fd045131c Use ai.retry settings, add AI connect timeout, stop logging post text
AiRetryProperties was bound but @Retryable had hard-coded values, so
ai.retry.* in application.yaml had no effect. WebClient had no connect
timeout (Netty default 30 s per attempt). TextModerationProcessor logged the
whole moderation context, including post text, at INFO.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 01:04:30 +03:00

57 lines
2.2 KiB
Java

package com.krylov.refound.ai.client;
import com.krylov.refound.ai.dto.ModerationResponse;
import com.krylov.refound.ai.dto.TextModerationRequest;
import com.krylov.refound.ai.exception.AiServerException;
import com.krylov.refound.ai.exception.AiTimeoutException;
import com.krylov.refound.ai.exception.AiUnavailableException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@Slf4j
public class TextModerationClient {
private final AiHttpClient client;
@Retryable(
retryFor = {AiUnavailableException.class, AiServerException.class, AiTimeoutException.class},
// Параметры из ai.retry.* (AiRetryProperties); раньше они были захардкожены
maxAttemptsExpression = "${ai.retry.max-attempts:3}",
backoff = @Backoff(delayExpression = "${ai.retry.delay:500}",
multiplierExpression = "${ai.retry.multiplier:2}")
)
public ModerationResponse moderate(String text) {
log.info("Calling AI TextModeration service");
return client.post("/api/v1/moderation/text", new TextModerationRequest(text), ModerationResponse.class);
}
// После исчерпания попыток возвращаем null: пост остаётся на модерации и
// проверяется снова при следующем запуске планировщика.
@Recover
public ModerationResponse recover(AiUnavailableException e, String text) {
log.error("AI TextModeration unavailable after retries: {}", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
return null;
}
@Recover
public ModerationResponse recover(AiTimeoutException e, String text) {
log.error("AI TextModeration timed out after retries");
return null;
}
@Recover
public ModerationResponse recover(AiServerException e, String text) {
log.error("AI TextModeration server error after retries: {}", e.getMessage());
return null;
}
}