This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new a3e43fe99b optimise: create http client by connection pool (#14079)
a3e43fe99b is described below
commit a3e43fe99b2769cdf0077a1504a5b078bb3873dc
Author: YuLuo <[email protected]>
AuthorDate: Wed May 8 11:19:49 2024 +0800
optimise: create http client by connection pool (#14079)
---
.../remoting/http/config/HttpClientConfig.java | 24 ++++++++++++++++++++++
.../remoting/http/restclient/OKHttpRestClient.java | 9 ++++++--
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/config/HttpClientConfig.java
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/config/HttpClientConfig.java
index 09e0ab92a1..fa7cb5ab3a 100644
---
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/config/HttpClientConfig.java
+++
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/config/HttpClientConfig.java
@@ -22,6 +22,10 @@ public class HttpClientConfig {
private int connectTimeout = 6 * 1000;
private int chunkLength = 8196;
+ private int maxIdleConnections = 20;
+
+ private int keepAliveDuration = 30 * 1000;
+
private int HTTP_CLIENT_CONNECTION_MANAGER_MAX_PER_ROUTE = 20;
private int HTTP_CLIENT_CONNECTION_MANAGER_MAX_TOTAL = 20;
private int HTTPCLIENT_KEEP_ALIVE_DURATION = 30 * 1000;
@@ -57,4 +61,24 @@ public class HttpClientConfig {
public int getChunkLength() {
return chunkLength;
}
+
+ public int getMaxIdleConnections() {
+
+ return maxIdleConnections;
+ }
+
+ public void setMaxIdleConnections(int maxIdleConnections) {
+
+ this.maxIdleConnections = maxIdleConnections;
+ }
+
+ public int getKeepAliveDuration() {
+
+ return keepAliveDuration;
+ }
+
+ public void setKeepAliveDuration(int keepAliveDuration) {
+
+ this.keepAliveDuration = keepAliveDuration;
+ }
}
diff --git
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/OKHttpRestClient.java
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/OKHttpRestClient.java
index 36ba2e6d18..762be4b2cd 100644
---
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/OKHttpRestClient.java
+++
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/OKHttpRestClient.java
@@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
+import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
@@ -140,11 +141,15 @@ public class OKHttpRestClient implements RestClient {
}
public OkHttpClient createHttpClient(HttpClientConfig httpClientConfig) {
- OkHttpClient client = new OkHttpClient.Builder()
+
+ return new OkHttpClient.Builder()
.readTimeout(httpClientConfig.getReadTimeout(),
TimeUnit.SECONDS)
.writeTimeout(httpClientConfig.getWriteTimeout(),
TimeUnit.SECONDS)
.connectTimeout(httpClientConfig.getConnectTimeout(),
TimeUnit.SECONDS)
+ .connectionPool(new ConnectionPool(
+ httpClientConfig.getMaxIdleConnections(),
+ httpClientConfig.getKeepAliveDuration(),
+ TimeUnit.SECONDS))
.build();
- return client;
}
}