Commit 39891e5c authored by zhouwei's avatar zhouwei

add timeout

parent 0745b64b
......@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
import com.nanyan.securitylink.config.AppConfig;
import com.nanyan.securitylink.service.AlertService;
import com.nanyan.securitylink.service.TokenRouteMappingService;
import io.netty.channel.ChannelOption;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -16,17 +17,20 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;
@Slf4j
@Component
......@@ -182,6 +186,37 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
true,
errorStats
).timeout(Duration.ofMillis(appConfig.getRequestTimeout()))
.onErrorResume(TimeoutException.class, ex -> {
// 记录超时错误
long duration = System.currentTimeMillis() - startTime;
log.warn("Source request timed out after {}ms, switching to target: {}",
duration, uriConfig.getTargetUri());
String targetUri = tokenRouteMappingService.resolveTargetUri(sourceUri, uriConfig);
// 直接转发到 target
return tryRequest(uriConfig.getFallbackUrl(),
targetUri,
uriConfig.getFallbackHost(),
uriConfig.getModel(),
uriConfig.getModelKey(),
exchange,
false,
null);
})
.onErrorResume(Exception.class, ex -> {
// 处理其他错误
log.error("Source request failed: {}", ex.getMessage());
log.info("Source request timed out after {}ms, switching to target: {}",
System.currentTimeMillis() - startTime, uriConfig.getTargetUri());
String targetUri = tokenRouteMappingService.resolveTargetUri(sourceUri, uriConfig);
return tryRequest(uriConfig.getFallbackUrl(),
targetUri,
uriConfig.getFallbackHost(),
uriConfig.getModel(),
uriConfig.getModelKey(),
exchange,
false,
null);
})
.onErrorResume(primaryError -> {
log.error("Primary endpoint failed: {}, error: {}",
uriConfig.getPrimaryUrl() + sourceUri,
......@@ -250,7 +285,14 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
log.info("Starting {} request to: {}", isSourceRequest ? "source" : "target", fullUrl);
long startTime = System.currentTimeMillis();
WebClient.RequestBodySpec requestBodySpec = webClientBuilder.build()
// 创建 WebClient 请求
WebClient client = webClientBuilder.clone()
.clientConnector(new ReactorClientHttpConnector(HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, appConfig.getRequestTimeout())
.responseTimeout(Duration.ofMillis(appConfig.getRequestTimeout()))))
.build();
WebClient.RequestBodySpec requestBodySpec = client
.method(request.getMethod())
.uri(fullUrl + (request.getQueryParams().isEmpty() ? "" :
"?" + request.getQueryParams().toString().substring(1)));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment