mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
增加超时时间和读取超时时间设置
This commit is contained in:
@@ -110,4 +110,36 @@ public interface AIConfig {
|
||||
*/
|
||||
Map<String, Object> getAdditionalConfigMap();
|
||||
|
||||
/**
|
||||
* 设置连接超时时间
|
||||
*
|
||||
* @param timeout 连接超时时间
|
||||
* @since 5.8.39
|
||||
*/
|
||||
void setTimeout(int timeout);
|
||||
|
||||
/**
|
||||
* 获取连接超时时间
|
||||
*
|
||||
* @return timeout
|
||||
* @since 5.8.39
|
||||
*/
|
||||
int getTimeout();
|
||||
|
||||
/**
|
||||
* 设置读取超时时间
|
||||
*
|
||||
* @param readTimeout 连接超时时间
|
||||
* @since 5.8.39
|
||||
*/
|
||||
void setReadTimeout(int readTimeout);
|
||||
|
||||
/**
|
||||
* 获取读取超时时间
|
||||
*
|
||||
* @return readTimeout
|
||||
* @since 5.8.39
|
||||
*/
|
||||
int getReadTimeout();
|
||||
|
||||
}
|
||||
|
@@ -106,6 +106,34 @@ public class AIConfigBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置连接超时时间,不设置为默认值
|
||||
*
|
||||
* @param timeout 超时时间
|
||||
* @return config
|
||||
* @since 5.8.39
|
||||
*/
|
||||
public synchronized AIConfigBuilder setTimout(final int timeout) {
|
||||
if (timeout > 0) {
|
||||
config.setTimeout(timeout);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置读取超时时间,不设置为默认值
|
||||
*
|
||||
* @param readTimout 取超时时间
|
||||
* @return config
|
||||
* @since 5.8.39
|
||||
*/
|
||||
public synchronized AIConfigBuilder setReadTimout(final int readTimout) {
|
||||
if (readTimout > 0) {
|
||||
config.setReadTimeout(readTimout);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回config实例
|
||||
*
|
||||
|
@@ -59,7 +59,7 @@ public class BaseAIService {
|
||||
return HttpRequest.get(config.getApiUrl() + endpoint)
|
||||
.header(Header.ACCEPT, "application/json")
|
||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||
.timeout(180000)
|
||||
.timeout(config.getTimeout())
|
||||
.execute();
|
||||
} catch (final AIException e) {
|
||||
throw new AIException("Failed to send GET request: " + e.getMessage(), e);
|
||||
@@ -80,7 +80,7 @@ public class BaseAIService {
|
||||
.header(Header.ACCEPT, "application/json")
|
||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||
.body(paramJson)
|
||||
.timeout(180000)
|
||||
.timeout(config.getTimeout())
|
||||
.execute();
|
||||
} catch (final AIException e) {
|
||||
throw new AIException("Failed to send POST request:" + e.getMessage(), e);
|
||||
@@ -103,7 +103,7 @@ public class BaseAIService {
|
||||
.header(Header.ACCEPT, "application/json")
|
||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||
.form(paramMap)
|
||||
.timeout(180000)
|
||||
.timeout(config.getTimeout())
|
||||
.execute();
|
||||
} catch (final AIException e) {
|
||||
throw new AIException("Failed to send POST request:" + e.getMessage(), e);
|
||||
@@ -128,9 +128,9 @@ public class BaseAIService {
|
||||
connection.setRequestProperty(Header.AUTHORIZATION.getValue(), "Bearer " + config.getApiKey());
|
||||
connection.setDoOutput(true);
|
||||
//5分钟
|
||||
connection.setReadTimeout(300000);
|
||||
connection.setReadTimeout(config.getReadTimeout());
|
||||
//3分钟
|
||||
connection.setConnectTimeout(180000);
|
||||
connection.setConnectTimeout(config.getTimeout());
|
||||
// 发送请求体
|
||||
try (OutputStream os = connection.getOutputStream()) {
|
||||
String jsonInputString = JSONUtil.toJsonStr(paramMap);
|
||||
|
@@ -35,6 +35,10 @@ public class BaseConfig implements AIConfig {
|
||||
protected volatile String model;
|
||||
//动态扩展字段
|
||||
protected Map<String, Object> additionalConfig = new ConcurrentHashMap<>();
|
||||
//连接超时时间
|
||||
protected volatile int timeout = 180000;
|
||||
//读取超时时间
|
||||
protected volatile int readTimeout = 300000;
|
||||
|
||||
@Override
|
||||
public void setApiKey(final String apiKey) {
|
||||
@@ -81,4 +85,23 @@ public class BaseConfig implements AIConfig {
|
||||
return new ConcurrentHashMap<>(additionalConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeout(final int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReadTimeout() {
|
||||
return readTimeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadTimeout(final int readTimeout) {
|
||||
this.readTimeout = readTimeout;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user