如何设置jetty服务器的连接/请求超时?

发布于 2024-10-27 02:02:09 字数 828 浏览 1 评论 0 原文

我在我的应用程序中运行嵌入式jetty服务器(jetty 6.1.24),如下所示:

    Handler handler=new AbstractHandler()
    {
        @Override
        public void handle(String target, HttpServletRequest request,
                HttpServletResponse response, int dispatch)
                throws IOException, ServletException {
              //this can take a long time
              doSomething();  
        }
    };


    Server server = new Server(8080);
    Connector connector = new org.mortbay.jetty.nio.SelectChannelConnector();      
    server.addConnector(connector);

    server.setHandler(handler);
    server.start();

我想设置一个超时值(2秒),这样如果handler.handle()方法花费超过2秒,jetty服务器将超时并以 408 http 代码响应客户端(请求超时)。

这是为了保证我的应用程序不会长时间保留客户端请求并始终在 2 秒内响应。

我做了一些研究并用“connector.setMaxIdleTime(2000);”对其进行了测试但它不起作用。

I'm running an embedded jetty server (jetty 6.1.24) inside my application like this:

    Handler handler=new AbstractHandler()
    {
        @Override
        public void handle(String target, HttpServletRequest request,
                HttpServletResponse response, int dispatch)
                throws IOException, ServletException {
              //this can take a long time
              doSomething();  
        }
    };


    Server server = new Server(8080);
    Connector connector = new org.mortbay.jetty.nio.SelectChannelConnector();      
    server.addConnector(connector);

    server.setHandler(handler);
    server.start();

I would like to set a timeout value (2 seconds) so that if handler.handle() method takes more than 2 seconds, jetty server will timeout and response to the client with 408 http code (request timeout).

This is to guarantee that my application will not hold the client request for a long time and always response within 2 seconds.

I did some research and tested it with "connector.setMaxIdleTime(2000);" but it doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め可乐爱微笑 2024-11-03 02:02:09

查看 SelectChannelConnector (Jetty) 的 API:

http://download.eclipse.org/jetty/7.6.17.v20150415/apidocs/org/eclipse/jetty/server/nio/SelectChannelConnector.html

我试图找到通道的任何超时功能(控制传入连接):setMaxIdleTime()、setLowResourceMaxIdleTime() 和 setSoLingerTime() 均可用。

注意:超时功能不起作用的原因与操作系统上套接字的性质有关。也许甚至是码头的本质(我在某处读过它,但不记得它在哪里)。

注意2:我不确定你为什么尝试限制超时,也许更好的方法是限制缓冲区大小?如果您想防止拒绝服务......

Take a look at the API for SelectChannelConnector (Jetty):

http://download.eclipse.org/jetty/7.6.17.v20150415/apidocs/org/eclipse/jetty/server/nio/SelectChannelConnector.html

I've tried to locate any timeout features of the channel (which controls incoming connections): setMaxIdleTime(), setLowResourceMaxIdleTime() and setSoLingerTime() are available it appears.

NOTE: the reason for your timeout feature not to work has to do with the nature of the socket on your operating system. Perhaps even the nature of Jetty (i've read about it somewhere, but cannot remember where it was).

NOTE2: i'm not sure why you try to limit the timeout, perhaps a better approach is limiting the buffer sizes? If you're trying to prevent denial of service...

阳光下慵懒的猫 2024-11-03 02:02:09

是的,这是可能的。您可以使用 Jetty 的 DosFilter 来完成此操作。此过滤器通常用于为 Jetty Web 服务器配置 DOS 攻击预防机制。该过滤器的一个名为“MaxRequestMs”的属性提供了您正在寻找的内容。

有关更多详细信息,请检查此。

https://www.eclipse. org/jetty/javadoc/jetty-9/org/eclipse/jetty/servlets/DoSFilter.html

Yes, this is possible. You could do this using DosFilter of Jetty. This filter is generally used to configure a DOS attack prevention mechanism for your Jetty web server. A property of this filter called 'MaxRequestMs' provides what you are looking for.

For more details, check this.

https://www.eclipse.org/jetty/javadoc/jetty-9/org/eclipse/jetty/servlets/DoSFilter.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文