how to set connection/request timeout for jetty server?

若如初见. 提交于 2019-12-10 12:49:21

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/5445006/how-to-set-connection-request-timeout-for-jetty-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!