[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: failure detection of tcp connection with LuaSocket ?
- From: Matthew Wild <mwild1@...>
- Date: Mon, 21 Mar 2011 16:47:08 +0000
On 21 March 2011 14:27, Valerio Schiavoni <valerio.schiavoni@gmail.com> wrote:
> Hello,
> suppose I establish a tcp connection to a server.
> Is there some kind of callback mechanism that I can exploit to detect when
> such connection is no longer usable?
Not without an extra layer on top of (or even in) LuaSocket. The C
socket API does not provide such a feature - it just returns an error
when you try to send/receive on a closed socket.
> The failures I'd like to detect are in particular the crash of the server.
Unfortunately this is easier said than done - a crashed server tends
to send no notification that it has crashed :)
> Ideally, I'd like the client to detect it as soon as possible.
> It seems a feature of TCP, keepalive messages, is meant to help in this
> scenario.
Yes, for some definition of "help" - they basically prevent the
possibility of a dead TCP connection staying open forever.
> LuaSocket seems to support this option:
> "'keepalive': Setting this option to true enables the periodic transmission
> of messages on a connected socket. Should the connected party fail to
> respond to these messages, the connection is considered broken and processes
> using the socket are notified;"
> Nevertheless, the 3 parameters that regulate this tcp feature (period,
> interval, timeout) are not documented: are those supported at all ?
The keepalive logic is actually defined by the TCP RFC, and the
recommended values are relatively large, meaning that keepalives
typically won't kick in for hours after the peer goes quiet. This is
aimed as a feature - a TCP connection can be quite resilient to
temporary network glitches, and packets can be delayed and not affect
the connection.
Unfortunately in some cases you want to aggressively discover a dead
connection. Sadly the options to tweak these values per socket are not
standardised, which is most likely why they are not available in
LuaSocket.
> Is this approach the better/most-common way to detect tcp connection
> failures?
It depends... if you're asking this question you probably care
somewhat about whether the connection is alive. The easiest way by far
is to have some kind of ping/pong at the application layer instead of
using TCP keepalives. If the peer has been silent for a certain amount
of time, send a ping command to them, and disconnect them if a pong is
not received within a certain amount of time. Most protocols support
this.
As a real-world example of this problem... if you use SSH you may have
noticed sometimes "hung" connections (especially happens to me because
I often suspend my laptop while ssh is running). OpenSSH does not do
any application-level ping-pong by default and relies on TCP
keepalives. However it also supports the user-configured options
"ClientAliveInterval" and "ServerAliveInterval" to set the ping
frequency of pings made by the server and client respectively.
Hope this helps,
Matthew