This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new ee1887bd31 fix(sdk): close http client resources on shutdown (#7071)
ee1887bd31 is described below
commit ee1887bd317bea6e2a09716bb9270bea3d493f77
Author: Liming Deng <[email protected]>
AuthorDate: Thu Oct 1 07:05:47 2026 +0800
fix(sdk): close http client resources on shutdown (#7071)
---
.../shenyu/sdk/core/client/ShenyuSdkClient.java | 6 ++-
.../sdk/core/client/ShenyuSdkClientFactory.java | 7 +--
.../core/client/ShenyuSdkClientFactoryTest.java | 26 +++------
.../shenyu/sdk/httpclient/HttpShenyuSdkClient.java | 21 ++++++--
.../sdk/httpclient/HttpShenyuSdkClientTest.java | 61 +++++++++++++++++-----
.../shenyu/sdk/okhttp/OkHttpShenyuSdkClient.java | 2 +-
.../sdk/okhttp/OkHttpShenyuSdkClientTest.java | 11 ++++
7 files changed, 91 insertions(+), 43 deletions(-)
diff --git
a/shenyu-sdk/shenyu-sdk-core/src/main/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClient.java
b/shenyu-sdk/shenyu-sdk-core/src/main/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClient.java
index 1eb67b326b..7475296c73 100644
---
a/shenyu-sdk/shenyu-sdk-core/src/main/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClient.java
+++
b/shenyu-sdk/shenyu-sdk-core/src/main/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClient.java
@@ -31,7 +31,7 @@ import java.util.List;
* ShenyuSdkClient.
*/
@SPI
-public interface ShenyuSdkClient {
+public interface ShenyuSdkClient extends AutoCloseable {
/**
* Init.
@@ -51,4 +51,8 @@ public interface ShenyuSdkClient {
* @throws IOException error
*/
ShenyuResponse execute(ShenyuRequest request) throws IOException;
+
+ @Override
+ default void close() throws IOException {
+ }
}
diff --git
a/shenyu-sdk/shenyu-sdk-core/src/main/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClientFactory.java
b/shenyu-sdk/shenyu-sdk-core/src/main/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClientFactory.java
index 44bf34ec56..32d7252dda 100644
---
a/shenyu-sdk/shenyu-sdk-core/src/main/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClientFactory.java
+++
b/shenyu-sdk/shenyu-sdk-core/src/main/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClientFactory.java
@@ -19,15 +19,10 @@ package org.apache.shenyu.sdk.core.client;
import org.apache.shenyu.spi.ExtensionLoader;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
/**
* The type Shenyu sdk client factory.
*/
public class ShenyuSdkClientFactory {
-
- private static final Map<String, ShenyuSdkClient> SDK_CLIENT_MAP = new
ConcurrentHashMap<>();
/**
* New shenyu sku client.
@@ -36,6 +31,6 @@ public class ShenyuSdkClientFactory {
* @return the shenyu instance register repository
*/
public static ShenyuSdkClient newInstance(final String clientType) {
- return SDK_CLIENT_MAP.computeIfAbsent(clientType,
ExtensionLoader.getExtensionLoader(ShenyuSdkClient.class)::getJoin);
+ return
ExtensionLoader.getExtensionLoader(ShenyuSdkClient.class).getJoin(clientType);
}
}
diff --git
a/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClientFactoryTest.java
b/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClientFactoryTest.java
index dede584af3..972e5ab8a1 100644
---
a/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClientFactoryTest.java
+++
b/shenyu-sdk/shenyu-sdk-core/src/test/java/org/apache/shenyu/sdk/core/client/ShenyuSdkClientFactoryTest.java
@@ -18,14 +18,10 @@
package org.apache.shenyu.sdk.core.client;
import org.apache.shenyu.spi.ExtensionLoader;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
-import java.lang.reflect.Field;
-import java.util.Map;
-
-import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
@@ -35,26 +31,18 @@ import static org.mockito.Mockito.when;
*/
public class ShenyuSdkClientFactoryTest {
- @BeforeEach
- public void setUp() throws Exception {
- final Field field = ShenyuSdkClientFactory.class.getDeclaredField(
- "SDK_CLIENT_MAP");
- field.setAccessible(true);
- final Map<String, ShenyuSdkClient> map = (Map<String,
ShenyuSdkClient>) field.get(
- null);
- map.put("httpclient", mock(ShenyuSdkClient.class));
- }
-
@Test
public void testNewInstance() {
- assertNotNull(ShenyuSdkClientFactory.newInstance("httpclient"));
try (MockedStatic<ExtensionLoader> mocked =
mockStatic(ExtensionLoader.class)) {
ExtensionLoader extensionLoader = mock(ExtensionLoader.class);
+ ShenyuSdkClient firstClient = mock(ShenyuSdkClient.class);
+ ShenyuSdkClient secondClient = mock(ShenyuSdkClient.class);
mocked.when(() ->
ExtensionLoader.getExtensionLoader(ShenyuSdkClient.class))
.thenReturn(extensionLoader);
- when(extensionLoader.getJoin("clientType")).thenReturn(
- mock(ShenyuSdkClient.class));
- assertNotNull(ShenyuSdkClientFactory.newInstance("clientType"));
+
when(extensionLoader.getJoin("clientType")).thenReturn(firstClient,
secondClient);
+
+ assertSame(firstClient,
ShenyuSdkClientFactory.newInstance("clientType"));
+ assertSame(secondClient,
ShenyuSdkClientFactory.newInstance("clientType"));
}
}
}
diff --git
a/shenyu-sdk/shenyu-sdk-httpclient/src/main/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClient.java
b/shenyu-sdk/shenyu-sdk-httpclient/src/main/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClient.java
index 32dfbc29fc..384000a0d3 100644
---
a/shenyu-sdk/shenyu-sdk-httpclient/src/main/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClient.java
+++
b/shenyu-sdk/shenyu-sdk-httpclient/src/main/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClient.java
@@ -30,7 +30,6 @@ import
org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
-import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.nio.conn.NoopIOSessionStrategy;
import org.apache.http.nio.conn.SchemeIOSessionStrategy;
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
@@ -51,6 +50,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
+import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
@@ -60,7 +60,7 @@ import java.util.stream.Collectors;
/**
* shenyu httpclient.
*/
-@Join
+@Join(isSingleton = false)
public class HttpShenyuSdkClient extends AbstractShenyuSdkClient {
private static final Logger LOG =
LoggerFactory.getLogger(HttpShenyuSdkClient.class);
@@ -69,7 +69,7 @@ public class HttpShenyuSdkClient extends
AbstractShenyuSdkClient {
private RequestConfig requestConfig;
- private HttpAsyncClient httpAsyncClient;
+ private CloseableHttpAsyncClient httpAsyncClient;
@Override
protected void initClient(final Properties props) {
@@ -98,7 +98,7 @@ public class HttpShenyuSdkClient extends
AbstractShenyuSdkClient {
}
}
- private HttpAsyncClient getHttpClient() {
+ private CloseableHttpAsyncClient getHttpClient() {
CloseableHttpAsyncClient client =
HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.build();
@@ -106,6 +106,19 @@ public class HttpShenyuSdkClient extends
AbstractShenyuSdkClient {
return client;
}
+ @Override
+ public void close() throws IOException {
+ try {
+ if (Objects.nonNull(httpAsyncClient)) {
+ httpAsyncClient.close();
+ }
+ } finally {
+ if (Objects.nonNull(connectionManager)) {
+ connectionManager.shutdown();
+ }
+ }
+ }
+
@Override
protected ShenyuResponse doRequest(final ShenyuRequest request) throws
IOException {
String url = request.getUrl();
diff --git
a/shenyu-sdk/shenyu-sdk-httpclient/src/test/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClientTest.java
b/shenyu-sdk/shenyu-sdk-httpclient/src/test/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClientTest.java
index 07d116109f..0f3d5f16be 100644
---
a/shenyu-sdk/shenyu-sdk-httpclient/src/test/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClientTest.java
+++
b/shenyu-sdk/shenyu-sdk-httpclient/src/test/java/org/apache/shenyu/sdk/httpclient/HttpShenyuSdkClientTest.java
@@ -17,11 +17,14 @@
package org.apache.shenyu.sdk.httpclient;
+import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
+import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
+import org.apache.shenyu.sdk.core.ShenyuRequest;
+import org.apache.shenyu.sdk.core.client.ShenyuSdkClient;
+import org.apache.shenyu.sdk.core.client.ShenyuSdkClientFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.concurrent.FutureCallback;
-import org.apache.http.nio.client.HttpAsyncClient;
-import org.apache.shenyu.sdk.core.ShenyuRequest;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
@@ -37,7 +40,9 @@ import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
+import static org.junit.Assert.assertNotSame;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class HttpShenyuSdkClientTest {
@@ -45,19 +50,51 @@ public class HttpShenyuSdkClientTest {
@Test
public void testShenyuHttpClient() throws IOException {
HttpShenyuSdkClient shenyuHttpClient = mock(HttpShenyuSdkClient.class,
Mockito.CALLS_REAL_METHODS);
- shenyuHttpClient.initClient(new Properties());
- Map<String, Collection<String>> headerMap = new HashMap<>();
- headerMap.put("header", Arrays.asList("test1", "test2"));
- ShenyuRequest shenyuRequest =
ShenyuRequest.create(ShenyuRequest.HttpMethod.GET, "https://shenyu.apache.org",
- headerMap, null, null, null);
- when(shenyuHttpClient.doRequest(shenyuRequest)).thenCallRealMethod();
+ try {
+ shenyuHttpClient.initClient(new Properties());
+ Map<String, Collection<String>> headerMap = new HashMap<>();
+ headerMap.put("header", Arrays.asList("test1", "test2"));
+ ShenyuRequest shenyuRequest =
ShenyuRequest.create(ShenyuRequest.HttpMethod.GET, "https://shenyu.apache.org",
+ headerMap, null, null, null);
+
when(shenyuHttpClient.doRequest(shenyuRequest)).thenCallRealMethod();
+ } finally {
+ shenyuHttpClient.close();
+ }
+ }
+
+ @Test
+ public void testClose() throws Exception {
+ HttpShenyuSdkClient shenyuHttpClient = new HttpShenyuSdkClient();
+ CloseableHttpAsyncClient httpAsyncClient =
mock(CloseableHttpAsyncClient.class);
+ PoolingNHttpClientConnectionManager connectionManager =
mock(PoolingNHttpClientConnectionManager.class);
+ setField(shenyuHttpClient, "httpAsyncClient", httpAsyncClient);
+ setField(shenyuHttpClient, "connectionManager", connectionManager);
+
+ shenyuHttpClient.close();
+
+ verify(httpAsyncClient).close();
+ verify(connectionManager).shutdown();
+ }
+
+ @Test
+ public void testFactoryCreatesIndependentClients() {
+ ShenyuSdkClient firstClient =
ShenyuSdkClientFactory.newInstance("httpclient");
+ ShenyuSdkClient secondClient =
ShenyuSdkClientFactory.newInstance("httpclient");
+
+ assertNotSame(firstClient, secondClient);
+ }
+
+ private void setField(final HttpShenyuSdkClient client, final String name,
final Object value) throws Exception {
+ Field field = HttpShenyuSdkClient.class.getDeclaredField(name);
+ field.setAccessible(true);
+ field.set(client, value);
}
@Test
@SuppressWarnings("unchecked")
public void testTransportFailureThrowsIOException() throws Exception {
HttpShenyuSdkClient shenyuHttpClient = new HttpShenyuSdkClient();
- HttpAsyncClient httpAsyncClient = mock(HttpAsyncClient.class);
+ CloseableHttpAsyncClient httpAsyncClient =
mock(CloseableHttpAsyncClient.class);
Future<HttpResponse> future = mock(Future.class);
when(httpAsyncClient.execute(Mockito.any(HttpUriRequest.class),
Mockito.<FutureCallback<HttpResponse>>any())).thenReturn(future);
when(future.get()).thenThrow(new ExecutionException(new
IOException("connection reset")));
@@ -70,7 +107,7 @@ public class HttpShenyuSdkClientTest {
@SuppressWarnings("unchecked")
public void testInterruptedFailureRestoresInterruptStatus() throws
Exception {
HttpShenyuSdkClient shenyuHttpClient = new HttpShenyuSdkClient();
- HttpAsyncClient httpAsyncClient = mock(HttpAsyncClient.class);
+ CloseableHttpAsyncClient httpAsyncClient =
mock(CloseableHttpAsyncClient.class);
Future<HttpResponse> future = mock(Future.class);
when(httpAsyncClient.execute(Mockito.any(HttpUriRequest.class),
Mockito.<FutureCallback<HttpResponse>>any())).thenReturn(future);
when(future.get()).thenThrow(new InterruptedException("interrupted"));
@@ -88,7 +125,7 @@ public class HttpShenyuSdkClientTest {
@SuppressWarnings("unchecked")
public void testCancelledRequestThrowsIOException() throws Exception {
HttpShenyuSdkClient shenyuHttpClient = new HttpShenyuSdkClient();
- HttpAsyncClient httpAsyncClient = mock(HttpAsyncClient.class);
+ CloseableHttpAsyncClient httpAsyncClient =
mock(CloseableHttpAsyncClient.class);
Future<HttpResponse> future = mock(Future.class);
when(httpAsyncClient.execute(Mockito.any(HttpUriRequest.class),
Mockito.<FutureCallback<HttpResponse>>any())).thenReturn(future);
when(future.get()).thenThrow(new CancellationException("cancelled"));
@@ -102,7 +139,7 @@ public class HttpShenyuSdkClientTest {
new HashMap<>(), null, null, null);
}
- private void setHttpAsyncClient(final HttpShenyuSdkClient client, final
HttpAsyncClient httpAsyncClient) throws Exception {
+ private void setHttpAsyncClient(final HttpShenyuSdkClient client, final
CloseableHttpAsyncClient httpAsyncClient) throws Exception {
Field field =
HttpShenyuSdkClient.class.getDeclaredField("httpAsyncClient");
field.setAccessible(true);
field.set(client, httpAsyncClient);
diff --git
a/shenyu-sdk/shenyu-sdk-okhttp/src/main/java/org/apache/shenyu/sdk/okhttp/OkHttpShenyuSdkClient.java
b/shenyu-sdk/shenyu-sdk-okhttp/src/main/java/org/apache/shenyu/sdk/okhttp/OkHttpShenyuSdkClient.java
index f82f58bcab..4d1b7bd8b1 100644
---
a/shenyu-sdk/shenyu-sdk-okhttp/src/main/java/org/apache/shenyu/sdk/okhttp/OkHttpShenyuSdkClient.java
+++
b/shenyu-sdk/shenyu-sdk-okhttp/src/main/java/org/apache/shenyu/sdk/okhttp/OkHttpShenyuSdkClient.java
@@ -42,7 +42,7 @@ import java.util.stream.Collectors;
/**
* shenyu okhttp.
*/
-@Join
+@Join(isSingleton = false)
public class OkHttpShenyuSdkClient extends AbstractShenyuSdkClient {
private OkHttpClient okHttpClient;
diff --git
a/shenyu-sdk/shenyu-sdk-okhttp/src/test/java/org/apache/shenyu/sdk/okhttp/OkHttpShenyuSdkClientTest.java
b/shenyu-sdk/shenyu-sdk-okhttp/src/test/java/org/apache/shenyu/sdk/okhttp/OkHttpShenyuSdkClientTest.java
index dabd0ef76c..897772e637 100644
---
a/shenyu-sdk/shenyu-sdk-okhttp/src/test/java/org/apache/shenyu/sdk/okhttp/OkHttpShenyuSdkClientTest.java
+++
b/shenyu-sdk/shenyu-sdk-okhttp/src/test/java/org/apache/shenyu/sdk/okhttp/OkHttpShenyuSdkClientTest.java
@@ -20,6 +20,8 @@ package org.apache.shenyu.sdk.okhttp;
import org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository;
import org.apache.shenyu.registry.api.config.RegisterConfig;
import org.apache.shenyu.sdk.core.ShenyuRequest;
+import org.apache.shenyu.sdk.core.client.ShenyuSdkClient;
+import org.apache.shenyu.sdk.core.client.ShenyuSdkClientFactory;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -29,6 +31,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.mockito.Mockito.mock;
/**
@@ -36,6 +39,14 @@ import static org.mockito.Mockito.mock;
*/
public class OkHttpShenyuSdkClientTest {
+ @Test
+ public void testFactoryCreatesIndependentClients() {
+ ShenyuSdkClient firstClient =
ShenyuSdkClientFactory.newInstance("okhttp");
+ ShenyuSdkClient secondClient =
ShenyuSdkClientFactory.newInstance("okhttp");
+
+ assertNotSame(firstClient, secondClient);
+ }
+
/**
* Test shenyu http client.
*