Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set connection/request timeout for jetty server?

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.

like image 916
Hung Duong Avatar asked Mar 26 '11 20:03

Hung Duong


1 Answers

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

like image 177
Dennis Avatar answered Oct 01 '22 21:10

Dennis