AI接口加入超时处理

This commit is contained in:
2025-08-17 22:37:41 +08:00
parent 2f09f47194
commit 077312cfa6
4 changed files with 19 additions and 7 deletions

View File

@@ -88,8 +88,20 @@ public class AiChatController extends BaseController {
* 配置了5分钟的读取超时时间用于与Dify API进行通信 * 配置了5分钟的读取超时时间用于与Dify API进行通信
*/ */
private final OkHttpClient client = new OkHttpClient.Builder() private final OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(Duration.ofMinutes(5)) .connectTimeout(Duration.ofSeconds(5)) // 建连超时
.readTimeout(Duration.ofSeconds(12)) // 读超时(服务端应在此内有数据返回)
.writeTimeout(Duration.ofSeconds(12)) // 写超时
.retryOnConnectionFailure(true)
.build(); .build();
/** 为本次请求设置 “总超时”(含连接/读写),避免无限挂起 */
private Response execWithTimeouts(Request req, int callSecs, int readSecs, int writeSecs) throws IOException {
OkHttpClient shortClient = client.newBuilder()
.callTimeout(java.time.Duration.ofSeconds(callSecs)) // 整次调用最大时长
.readTimeout(java.time.Duration.ofSeconds(readSecs)) // 读超时(这段时间没有字节到达就超时)
.writeTimeout(java.time.Duration.ofSeconds(writeSecs)) // 写超时
.build();
return shortClient.newCall(req).execute();
}
/** /**
* JSON对象映射器 * JSON对象映射器
@@ -539,7 +551,7 @@ public class AiChatController extends BaseController {
.build(); .build();
// 执行请求 // 执行请求
try (Response response = client.newCall(request).execute()) { try (Response response = execWithTimeouts(request, 8, 8, 8)) {
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
throw new IOException("Dify API 请求失败: " + response.code() + " " + response.message()); throw new IOException("Dify API 请求失败: " + response.code() + " " + response.message());
} }
@@ -580,7 +592,7 @@ public class AiChatController extends BaseController {
} }
public List<ConversationDTO> getConversations(String user, String lastId) throws IOException { public List<ConversationDTO> getConversations(String user, String lastId) throws IOException {
return getConversations(user, lastId, 0, null); return getConversations(user, lastId, 1, "-updated_at");
} }
public List<ConversationDTO> getConversations(String user, Integer limit, String sortBy) throws IOException { public List<ConversationDTO> getConversations(String user, Integer limit, String sortBy) throws IOException {
@@ -623,7 +635,7 @@ public class AiChatController extends BaseController {
.get() .get()
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = execWithTimeouts(request, 8, 8, 8)) {
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
String body = response.body() != null ? response.body().string() : "No body"; String body = response.body() != null ? response.body().string() : "No body";

View File

@@ -1,4 +1,4 @@
package com.srs.dormitory.controller; package com.srs.web.controller.dormitory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@@ -1,4 +1,4 @@
package com.srs.routine.controller; package com.srs.web.controller.routine;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;

View File

@@ -1,4 +1,4 @@
package com.srs.teacher.controller; package com.srs.web.controller.teacher;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;