5

My main objective is setting max concurrent request connections limit for Undertow.

I have tried to set it by configuration through the properties. But it doesn't work.

  • application.yml undertow: worker-threads: 1 io-threads: 1 options: server: MAX_CONCURRENT_REQUESTS_PER_CONNECTION: 1

I have researched how to limit the maximum connections per request and have found the following:

In order to limit the connections in undertow it is necessary to implement RequestLimitHandler and RequestLimit.

"Request Limiting Handler Handler that limits the number of concurrent requests. If the number exceeds the limit requests are queued. If the queue fills up then requests are rejected."

http://undertow.io/undertow-docs/undertow-docs-1.2.0/#built-in-handlers

I have tried the next implementations, but this handler doesn't limit the request because it doesn't return error 503 response and I haven't been able to binding with MVC methods.

1. Test case: 
     @Bean
        public UndertowServletWebServerFactory myUndertowServletWebServerFactory() {
            final UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();

            final HandlerWrapper handlerWrapperLimit = new HandlerWrapper() {

                @Override
                public HttpHandler wrap(final HttpHandler handler) {
                    final int maximumConcurrentRequests = 1;
                    final int queueSize = 100;

                    final RequestLimit requestLimit = new RequestLimit(maximumConcurrentRequests,
                        queueSize);


                    final RequestLimitingHandler limitHandler = new RequestLimitingHandler(requestLimit,
                        new ResponseCodeHandler(
                            StatusCodes.OK));

                    return limitHandler;
                }
            };
            factory.addDeploymentInfoCustomizers(info -> info.addInitialHandlerChainWrapper(handlerWrapperLimit));

            return factory;
        }

2. Test case: 
@Bean
public UndertowServletWebServerFactory myUndertowServletWebServerFactory() {
    final UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();

    final HandlerWrapper handlerWrapperLimit = new HandlerWrapper() {

        @Override
        public HttpHandler wrap(final HttpHandler handler) {
            final int maximumConcurrentRequests = 1;
            final int queueSize = 100;

            final RequestLimit requestLimit = new RequestLimit(maximumConcurrentRequests,
                queueSize);

            requestLimit.setFailureHandler(new HttpHandler() {
                @Override
                public void handleRequest(final HttpServerExchange exchange) throws Exception {

                    final ResponseCodeHandler responseCodeHandler = new ResponseCodeHandler(
                        StatusCodes.SERVICE_UNAVAILABLE);
                    exchange.dispatch(responseCodeHandler);

                }
            });

            final RequestLimitingHandler limitHandler = new RequestLimitingHandler(requestLimit,
                new HttpHandler() {                                                 //Default Handler
                    @Override
                    public void handleRequest(final HttpServerExchange exchange) throws Exception {
                        exchange.dispatch(new ResponseCodeHandler(StatusCodes.OK));
                    }
                });

            return limitHandler;
        }
    };
    factory.addDeploymentInfoCustomizers(info -> info.addInitialHandlerChainWrapper(handlerWrapperLimit));

    return factory;
}

Please can help me with this issue.

jcliment
  • 51
  • 2

0 Answers0