Skip to content

Commit

Permalink
update jetty、Undertow ThreadPool
Browse files Browse the repository at this point in the history
  • Loading branch information
weihubeats committed Feb 28, 2022
1 parent 491cdac commit 4558594
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,5 @@ public Executor getWebThreadPool() {
return executor;
}

public void log(PoolParameterInfo poolParameterInfo, ThreadPoolExecutor threadPoolExecutor) {
int originalCoreSize = threadPoolExecutor.getCorePoolSize();
int originalMaximumPoolSize = threadPoolExecutor.getMaximumPoolSize();
long originalKeepAliveTime = threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
log.info(
"🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]",
String.format("%s => %s", originalCoreSize, poolParameterInfo.getCoreSize()),
String.format("%s => %s", originalMaximumPoolSize, poolParameterInfo.getMaxSize()),
String.format("%s => %s", originalKeepAliveTime, poolParameterInfo.getKeepAliveTime())
);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import cn.hippo4j.common.model.PoolParameterInfo;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
import org.springframework.boot.web.server.WebServer;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @author : wh
Expand All @@ -28,15 +27,22 @@ protected Executor getWebThreadPoolByServer(WebServer webServer) {
@Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {
ThreadPoolExecutor jettyExecutor = (ThreadPoolExecutor) executor;
jettyExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
jettyExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize());
jettyExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);

super.log(poolParameterInfo, jettyExecutor);
ThreadPool.SizedThreadPool jettyExecutor = (ThreadPool.SizedThreadPool) executor;

Integer coreSize = poolParameterInfo.getCoreSize();
Integer maxSize = poolParameterInfo.getMaxSize();
jettyExecutor.setMinThreads(coreSize);
jettyExecutor.setMaxThreads(maxSize);
log.info(
"🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}]",
String.format("%s => %s", jettyExecutor.getMinThreads(), coreSize),
String.format("%s => %s", jettyExecutor.getMaxThreads(), maxSize)
);
} catch (Exception ex) {
log.error("Failed to modify the jetty thread pool parameter.", ex);
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
tomcatExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
tomcatExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize());
tomcatExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);
int originalCoreSize = tomcatExecutor.getCorePoolSize();
int originalMaximumPoolSize = tomcatExecutor.getMaximumPoolSize();
long originalKeepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS);
log.info(
"🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]",
String.format("%s => %s", originalCoreSize, poolParameterInfo.getCoreSize()),
String.format("%s => %s", originalMaximumPoolSize, poolParameterInfo.getMaxSize()),
String.format("%s => %s", originalKeepAliveTime, poolParameterInfo.getKeepAliveTime())
);

super.log(poolParameterInfo, tomcatExecutor);

} catch (Exception ex) {
log.error("Failed to modify the Tomcat thread pool parameter.", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

import cn.hippo4j.common.model.PoolParameterInfo;
import io.undertow.Undertow;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.embedded.undertow.UndertowWebServer;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.util.ReflectionUtils;
import org.xnio.Options;
import org.xnio.XnioWorker;

import java.lang.reflect.Field;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Undertow web thread pool handler.
Expand Down Expand Up @@ -42,11 +40,25 @@ protected Executor getWebThreadPoolByServer(WebServer webServer) {
@Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {
ThreadPoolExecutor undertowExecutor = (ThreadPoolExecutor) executor;
undertowExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
undertowExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize());
undertowExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);
super.log(poolParameterInfo, undertowExecutor);
XnioWorker xnioWorker = (XnioWorker) executor;

Integer coreSize = poolParameterInfo.getCoreSize();
Integer maxSize = poolParameterInfo.getMaxSize();
Integer keepAliveTime = poolParameterInfo.getKeepAliveTime();

int originalCoreSize = xnioWorker.getOption(Options.WORKER_TASK_CORE_THREADS);
int originalMaximumPoolSize = xnioWorker.getOption(Options.WORKER_TASK_MAX_THREADS);
int originalKeepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE);

xnioWorker.setOption(Options.WORKER_TASK_CORE_THREADS, coreSize);
xnioWorker.setOption(Options.WORKER_TASK_MAX_THREADS, maxSize);
xnioWorker.setOption(Options.WORKER_TASK_KEEPALIVE, keepAliveTime);
log.info(
"🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]",
String.format("%s => %s", originalCoreSize, coreSize),
String.format("%s => %s", originalMaximumPoolSize, maxSize),
String.format("%s => %s", originalKeepAliveTime, keepAliveTime)
);

} catch (Exception ex) {
log.error("Failed to modify the undertow thread pool parameter.", ex);
Expand Down

0 comments on commit 4558594

Please sign in to comment.