I have written a Vert.x HTTP server in Java. When the client sends requests faster than the server can process them, the server-side request queue slowly fills up. Eventually the JVM runs out of memory because of all the accumulating requests.
Can I set a capacity on the Vert.x request queue?
I would like to set one or more of the following:
- A maximum number of queued requests
- A maximum size (in bytes) of all queued requests
When either of these limits is violated by an incoming request, I would like to immediately respond with 503 Service Unavailable
.
AFAIK there's no built-in way to accomplish this. However, this type of back pressure should still be achievable by normal means. The approach you take is this:
When an HTTP requests is received, immediately forward the request via a message to a separate request handling verticle on the event bus and increment an outstanding request counter.
Perform request handling logic in that verticle and respond to the event bus message once complete.
Once the HTTP server verticle receives a response from the request handler verticle, decrement the request counter and send the appropriate response.
Add a request counter check to your HTTP server handler to check the outstanding request count and respond with an appropriate error if the queue grows too large.
This is a common pattern in Vert.x that essentially just separates request handling logic from the HTTP request handler. Forwarding the request on the event bus as a JsonObject ensures that requests are quickly queued in the event bus. You an use that queue to calculate the number of outstanding requests as I've shown.
Note also that you can scale your HTTP server across multiple verticle instances in order to handle more requests. In this case you can either use static variables or shared data to share a semaphore across the instances.
来源:https://stackoverflow.com/questions/29521038/can-i-set-a-capacity-on-the-vert-x-http-request-queue