Skip to content

Commit

Permalink
Update HTTP session pool size
Browse files Browse the repository at this point in the history
  • Loading branch information
brrritssocold committed Nov 19, 2023
1 parent f82ba8b commit 0a6e58e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions hath-base/src/main/java/hath/base/http/HTTPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public boolean startConnectionListener(int port) {
myThread = new Thread(this, HTTPServer.class.getSimpleName());
myThread.start();


Out.info("Internal HTTP Server was successfully started, and is listening on port " + port);

return true;
Expand Down Expand Up @@ -241,6 +242,7 @@ public void run() {
String hostAddress = addr.getHostAddress().toLowerCase();
boolean localNetworkAccess = Settings.getClientHost().replace("::ffff:", "").equals(hostAddress) || localNetworkPattern.matcher(hostAddress).matches();
boolean apiServerAccess = Settings.isValidRPCServer(addr);
sessionPool.updateMaxPoolSize(Settings.getMaxConnections());

if(!apiServerAccess && !allowNormalConnections) {
Out.warning("Rejecting connection request from " + hostAddress + " during startup.");
Expand Down
22 changes: 19 additions & 3 deletions hath-base/src/main/java/hath/base/http/HTTPSessionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class HTTPSessionPool {
private static final int CORE_POOL_SIZE = 20;

private Executor sessionThreadPool;
private ThreadPoolExecutor pool;
private int lastMaxPoolSize;

public HTTPSessionPool() {
setupThreadPool();
Expand All @@ -58,8 +60,6 @@ public void execute(Runnable runnable) {
sessionThreadPool.execute(runnable);
}



private void setupThreadPool() {
sessionThreadPool = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
Expand All @@ -70,12 +70,28 @@ public Thread newThread(Runnable r) {
}
});

ThreadPoolExecutor pool = (ThreadPoolExecutor) sessionThreadPool;
pool = (ThreadPoolExecutor) sessionThreadPool;

int maximumPoolSize = Settings.getMaxConnections();
lastMaxPoolSize = maximumPoolSize;

pool.setMaximumPoolSize(maximumPoolSize);
pool.setCorePoolSize(CORE_POOL_SIZE);
pool.setKeepAliveTime(5, TimeUnit.MINUTES);

LOGGER.debug("Session pool size is {} to {} thread(s)", CORE_POOL_SIZE, maximumPoolSize);
}

/**
* Update max thread pool size. Will only apply change and log message if there was any change.
* @param maxPoolSize max pool size to set
*/
public void updateMaxPoolSize(int maxPoolSize) {
if (this.lastMaxPoolSize != maxPoolSize) {
this.lastMaxPoolSize = maxPoolSize;

pool.setMaximumPoolSize(maxPoolSize);
LOGGER.debug("Session pool max size updated to {} thread(s)", maxPoolSize);
}
}
}

0 comments on commit 0a6e58e

Please sign in to comment.