简体   繁体   English

Spring Boot Undertow将RequestLimitingHandler添加到DeploymentInfo

[英]Spring Boot Undertow add RequestLimitingHandler to DeploymentInfo

I am using Spring Boot with Undertow and trying to implement some limits on the number of requests Undertow will accept so as not to become overloaded under stress. 我正在使用Spring Boot with Undertow并尝试对Undertow接受的请求数量实施一些限制,以免在压力下过载。

I've seen the answer to the question at Spring Boot Undertow add both blocking handler and NIO handler in the same application , and it appears promising, but I'm not clear what HttpHandler should be passed as the argument to the RequestLimitingHandler constructor. 我在Spring Boot Undertow看到了问题的答案在同一个应用程序中添加了阻塞处理程序和NIO处理程序 ,看起来很有希望,但我不清楚应该将HttpHandler作为参数传递给RequestLimitingHandler构造函数。

Is there an easy way to add a RequestLimitingHandler to the UndertowEmbeddedServletContainerFactory bean, perhaps using the addDeploymentInfoCustomizers method? 有没有一种简单的方法可以使用addDeploymentInfoCustomizers方法向UndertowEmbeddedServletContainerFactory bean添加RequestLimitingHandler?

Alternatively, if I look deeper and get into the Xnio code on which Undertow is based, it looks like there is an option to set Options.WORKER_TASK_LIMIT, but upon further investigation, it looks like the XnioWorker class ignores this setting after the 3.0.10.GA release and simply sets taskQueue to an unbounded LinkedBlockingQueue. 或者,如果我深入了解Undertow所基于的Xnio代码,看起来有一个选项可以设置Options.WORKER_TASK_LIMIT,但经过进一步调查,看起来XnioWorker类在3.0.10之后忽略了这个设置。 .GA发布并简单地将taskQueue设置为无界的LinkedBlockingQueue。 Am I mistaken and could this also be an option? 我错了,这也可以选择吗?

Answering my own question in case it helps others in the future. 回答我自己的问题,以防将来帮助其他人。 Solution is to create a new Undertow HandlerWrapper and instantiate the new RequestLimitingHandler object within the wrap() method, like so: 解决方案是创建一个新的Undertow HandlerWrapper并在wrap()方法中实例化新的RequestLimitingHandler对象,如下所示:

@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory(RootHandler rootHandler) {
    UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();

    factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo.addInitialHandlerChainWrapper(new HandlerWrapper() {

        @Override
        public HttpHandler wrap(HttpHandler handler) {
            return new RequestLimitingHandler(maxConcurrentRequests, queueSize, handler);
        }

    }));

    return factory;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM