Re: [gevent] Immediately detecting lost connections in HTTPServer

138 views
Skip to first unread message

Matthias Urlichs

unread,
Aug 28, 2012, 5:16:21 PM8/28/12
to gev...@googlegroups.com
Hi,

eestolano:
> Here's the part I haven't been able to figure out: how can I make the handler detect the client timeout *when it happens*?

The client's socket becomes readable; actually reading from it yields EOF.

--
-- Matthias Urlichs

eestolano

unread,
Aug 28, 2012, 5:44:57 PM8/28/12
to gev...@googlegroups.com
Thanks! I'm not sure how to do that, though. I'm aiming for something like this (omitting the queue-producer code):

--------
def handle(request): # gevent.core.http_connection

### Wait for another greenlet to put an item on a queue

request.send_reply(200, 'OK', item_from_queue)

def main():

### launch separate greenlet to put items on queue

# set up web server
server = HTTPServer(('0.0.0.0', 10545), handle)
print "Serving on port 10545."
server.serve_forever()

--------

I was hoping HTTPServer would allow for a connection loss handler, but it doesn't seem to.

I'm not sure how to implement your suggestion for two reasons:

- How can I access the socket from the gevent.core.http_connection object? I can't find anything in the docs.
- If I can get to the socket, does this mean I have to poll the socket periodically for EOF? I'd rather avoid polling.

Any suggestions?

Thanks!

~ Esteban

Alexandre Kandalintsev

unread,
Aug 29, 2012, 5:29:06 AM8/29/12
to gev...@googlegroups.com, eest...@gmail.com
Tune tcp keepalive timeouts. This will help with long timeouts. May be
it's possible to do that on per-socket basis.

--
Alexandre Kandalintsev
> Hi,
>
> I'm trying to create an HTTP server (using HTTPServer) that works as
> follows:
>
> - Clients connect to the server with a large timeout (around 5
> minutes).
> - The server may have a reply for them immediately, or later (hence
> the large timeout).
> - Sometimes, the server will not have a reply in time; the client
> will disconnect.
>
> Here's the part I haven't been able to figure out: how can I make the
> handler detect the client timeout *when it happens*? If I catch the
> exception when the handler tries to reply and fails, that could be
> minutes after the client disconnected.
>
> Thanks!
>
> ~ Esteban

Matthias Urlichs

unread,
Aug 29, 2012, 6:01:29 AM8/29/12
to gev...@googlegroups.com, eest...@gmail.com
Hi,

Alexandre Kandalintsev:
> Tune tcp keepalive timeouts. This will help with long timeouts.

TCP keepalives exist for the purpose of auto-closing sockets whose remote
side becomes unreacheable / crashes instead of closing the connection.
They don't indicate to your application that a socket is closed.

If you don't read from (or write to) the socket, you won't notice either.

If you want a notification, you need to select() the socket.

--
-- Matthias Urlichs

Executier Godlike

unread,
Aug 29, 2012, 6:54:33 AM8/29/12
to gev...@googlegroups.com, matt...@urlichs.de, eest...@gmail.com
> If you want a notification, you need to select() the socket.
Whoops, I thought the problem is to detect dead clients...
Reply all
Reply to author
Forward
0 new messages