The main solution will be related to the thread responsible for processing several incoming requests (with your desired limit), and then transferring them to other worker / request handler threads. This basic structure is very similar to most servers: the main thread, which is responsible for sending requests to worker threads. When each of these workflows is complete, you can update the total / global counter so that the main thread knows that it can establish a new connection. This will require synchronization, but this is a simple and simple abstraction.
Here is an idea:
Server Subject :
// Receive Packet while (true) { serverLock.acquire(); try { if (numberOfRequests < MAX_REQUESTS) { packet = socket.receive(); numberOfRequests++; requestThread(packet).run(); } else { serverMonitor.wait(serverLock); } } finally { serverLock.release(); } }
Topic request :
// Handle Packet serverLock.acquire(); try { if (numberOfRequests == MAX_REQUESTS){ numberOfRequests--; serverMonitor.pulse(); } } finally { serverLock.release(); }
This is just to give you an idea of ββwhere you can start. But when you hang it, you can make optimizations and improvements to make sure that the synchronization is correct.
One specific enhancement that also lends itself to a limited number of requests is called ThreadPool .
Teocci
source share