12

我在我的应用程序中运行一个嵌入式码头服务器(码头 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 秒,码头服务器将超时并使用 408 http 代码(请求超时)响应客户端。

这是为了保证我的应用程序不会长时间持有客户端请求,并且总是在 2 秒内响应。

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

4

2 回答 2

3

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...

于 2012-02-10T22:15:27.533 回答
0

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

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

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

于 2022-02-25T10:52:37.343 回答