How to set limit to the number of concurrent request in servlet?

后端 未结 5 1651
轻奢々
轻奢々 2020-12-09 23:55

I got this servlet which return a pdf file to the client web browser. We do not want to risk any chance that when the number of request is too much, the server is paralyzed.

5条回答
  •  -上瘾入骨i
    2020-12-10 00:02

    I'd suggest writing a simple servlet Filter. Configure it in your web.xml to apply to the path that you want to limit the number of concurrent requests. The code would look something like this:

    public class LimitFilter implements Filter {
        private int limit = 5;
        private int count;
        private Object lock = new Object();
    
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            try {
                boolean ok;
                synchronized (lock) {
                    ok = count++ < limit;
                }
                if (ok) {
                    // let the request through and process as usual
                    chain.doFilter(request, response);
                } else {
                    // handle limit case, e.g. return status code 429 (Too Many Requests)
                    // see http://tools.ietf.org/html/rfc6585#page-3
                }
            } finally {
                synchronized (lock) {
                    count--;
                }           
            }
        }
    }
    

    Or alternatively you could just put this logic into your HttpServlet. It's just a bit cleaner and more reusable as a Filter. You might want to make the limit configurable through the web.xml rather than hard coding it.

    Ref.:
    Check definition of HTTP status code 429.

提交回复
热议问题