Release connection break a bit to not completely freeze the webserver on too many requests

This commit is contained in:
Blue (Lukas Rieger) 2021-09-13 20:10:08 +02:00
parent eadf854c50
commit a09d930b15
No known key found for this signature in database
GPG Key ID: 904C4995F9E1F800
2 changed files with 5 additions and 3 deletions

View File

@ -74,15 +74,17 @@ public class HttpConnection implements Runnable {
try {
HttpRequest request = acceptRequest();
boolean hasPermit = false;
try {
processingSemaphore.acquire();
//just slow down processing if limit is reached
hasPermit = processingSemaphore.tryAcquire(1, TimeUnit.SECONDS);
HttpResponse response = handler.handle(request);
sendResponse(response);
if (verbose) log(request, response);
} finally {
processingSemaphore.release();
if (hasPermit) processingSemaphore.release();
}
} catch (InvalidRequestException e){

View File

@ -60,7 +60,7 @@ public class WebServer extends Thread {
this.verbose = verbose;
this.handler = handler;
this.processingSemaphore = new Semaphore(18);
this.processingSemaphore = new Semaphore(24);
connectionThreads = null;
}