Can I set a capacity on the Vert.x HTTP request queue?

人盡茶涼 提交于 2019-12-07 15:06:31

问题


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.


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!