This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 717ac8e Add close method to cleanup resource and reduce the scheduled
thread count (#1907) (#1907)
717ac8e is described below
commit 717ac8e8fb3478341f0721371ea22d0c19aab1e2
Author: GuoJiwei <[email protected]>
AuthorDate: Mon Aug 9 18:36:27 2021 +0800
Add close method to cleanup resource and reduce the scheduled thread count
(#1907) (#1907)
---
.../admin/service/impl/UpstreamCheckService.java | 27 ++++++++++++++++++++--
.../admin/service/UpstreamCheckServiceTest.java | 12 ++++++++++
2 files changed, 37 insertions(+), 2 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java
index 808ff91..93fcb7e 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java
@@ -48,6 +48,7 @@ import
org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
+import javax.annotation.PreDestroy;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -55,6 +56,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
+import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@@ -85,6 +87,10 @@ public class UpstreamCheckService {
private final SelectorConditionMapper selectorConditionMapper;
+ private ScheduledThreadPoolExecutor executor;
+
+ private ScheduledFuture<?> scheduledFuture;
+
/**
* Instantiates a new Upstream check service.
*
@@ -117,8 +123,25 @@ public class UpstreamCheckService {
public void setup() {
if (checked) {
this.fetchUpstreamData();
- new
ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
ShenyuThreadFactory.create("scheduled-upstream-task", false))
- .scheduleWithFixedDelay(this::scheduled, 10,
scheduledTime, TimeUnit.SECONDS);
+ executor = new ScheduledThreadPoolExecutor(1,
ShenyuThreadFactory.create("scheduled-upstream-task", false));
+ scheduledFuture = executor.scheduleWithFixedDelay(this::scheduled,
10, scheduledTime, TimeUnit.SECONDS);
+ }
+ }
+
+ /**
+ * Close relative resource on container destroy.
+ */
+ @PreDestroy
+ public void close() {
+ if (Objects.nonNull(scheduledFuture)) {
+ scheduledFuture.cancel(false);
+ executor.shutdownNow();
+ try {
+ executor.awaitTermination(5, TimeUnit.SECONDS);
+ } catch (InterruptedException ex) {
+ log.error("shutdown executor error", ex);
+ Thread.currentThread().interrupt();
+ }
}
}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java
index 2da412f..ae4068a 100644
---
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java
@@ -37,6 +37,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.MockitoJUnitRunner;
+import org.powermock.reflect.Whitebox;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.test.util.ReflectionTestUtils;
@@ -45,8 +46,10 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
@@ -208,6 +211,15 @@ public final class UpstreamCheckServiceTest {
assertTrue(upstreamMap.containsKey(MOCK_SELECTOR_NAME_OTHER));
}
+ @Test
+ public void testClose() {
+ ScheduledThreadPoolExecutor executor =
Whitebox.getInternalState(upstreamCheckService, "executor");
+ assertNotNull(executor);
+ upstreamCheckService.close();
+ executor = Whitebox.getInternalState(upstreamCheckService, "executor");
+ assertTrue(executor.isShutdown());
+ }
+
private void setupZombieSet() {
final DivideUpstream divideUpstream1 = DivideUpstream.builder()
.upstreamHost("127.0.0.1")