Disabling keep-alive?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Disabling keep-alive?

JamesL
I've just ported one of our applications from mod_perl to Ratpack - our 2nd Ratpack app :) - but have stumbled upon a problem which prohibits me from releasing it.

I have the requirement of disabling keep-alive but can't find a way of implementing this in Ratpack. The DefaultResponseTransmitter looks at the request protocol version and "Connection" header. As most of our clients will be issuing HTTP/1.1 requests this means that Ratpack always returns a keep-alive response, regardless of whether I add a "Connection: close" header to the response (I've tried that one).

If there isn't a way of disabling keep-alive at current it may be an idea to make the DefaultResponseTransmitter look at response headers to see if "Connection" has already been added or adding a config setting to ServerConfig? NettyHandlerAdapter (the object that creates the instance of DefaultResponseTransmitter) has access to the Registry, and uses ServerConfig in it's constructor, so both changes are minor and I don't mind doing the work if we thing it's worth it and can agree on an approach.

James.

Reply | Threaded
Open this post in threaded view
|

Re: Disabling keep-alive?

Luke Daley
Administrator
Hi James,

Great to hear that this is your second Ratpack app.

Are you saying that we aren’t closing the connection, even if you send `Connection: close`? Or is it that you can’t send `Connection: close` in your environment. 

We are using https://github.com/netty/netty/blob/master/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaderUtil.java#L32 to determine whether to close or not, so I’m not sure why we wouldn’t be closing if this is what you mean.
Reply | Threaded
Open this post in threaded view
|

Re: Disabling keep-alive?

JamesL
Hi Luke and thanks for the quick reply. Sorry mine wasn't so quick.

The actual closing of the connection works fine and as expected per the requested http version. Our problem is that most of our clients are HTTP/1.1 which and they most likely won't be sending a http "Connection: close" header so Ratpack/Netty quite rightly keeps the connection open. This screws us up as we're throttling the number of requests the client can make at the router and keep-alive obviously bypasses that.

My problem is that even if I explicitly set a "Connection: close" response headers in my handler the DefaultResponseTransmitter overwrites the header if Ratpack/Netty thinks it should keep the connection alive (https://github.com/ratpack/ratpack/blob/master/ratpack-core/src/main/java/ratpack/server/internal/DefaultResponseTransmitter.java#L83-L84)

What I require is the ability to send a "Connection: close" and also maybe have Ratpack close the connection even if Ratpack/Netty thinks we should be using keep-alive for the request. Does that make sense?
Reply | Threaded
Open this post in threaded view
|

Re: Disabling keep-alive?

Luke Daley
Administrator
Right, now I understand. Thanks.

What I think we should do is add a forceCloseConnection() method to Response that does exactly that. For now, I'd just pass this through to the response transmitter when calling transmit() and make it work. We can refactor later.

If you want to send something like that, we can get it in 1.1 this week.

Keepalive is a little hard to test given that our client doesn't yet support it, but testing for the close header should be enough.
Reply | Threaded
Open this post in threaded view
|

Re: Disabling keep-alive?

JamesL
Thanks for the response again Luke. Did you create https://github.com/ratpack/ratpack/issues/817 for this issue? If so will continue this discussion on there as I have a few questions I'd like to ask re: implementation.

James.



Reply | Threaded
Open this post in threaded view
|

Re: Disabling keep-alive?

Luke Daley
Administrator

Yep, that’s for this issue.