Re: [kgio] handling Timeout.timeout with sockets
- Eric Wong
- 2012-05-03 @ 07:10
Mike Perham <firstname.lastname@example.org> wrote:
> I've noticed that my code cannot rescue the Timeout::Error that is
> raised and I expect that is by design.
Yes, it's by design. Reading timeout.rb (and its
"git log -p --full-diff" history) is enlightening, btw. You'll also
get better performance if you explicitly specify a class for timeout
to raise (ref: https://bugs.ruby-lang.org/issues/5765)
> However I'm wondering how my network client can recover from that
> timeout? Presumably there is now data to be read on the socket which,
> due to the timeout, has not been read, meaning my protocol state
> machine is now out of sync, thus leading to the crash.
> Should I drain the socket somehow before each operation? What is the
> best way to do this?
The easiest way is to open a new socket.
The other way is to _completely_ parse the old response (that timed out)
before reusing the socket.
Do _not_ rely on IO#nread in the io/wait module for this, it's
inherently racy: bytes may arrive after you call IO#nread. Unless the
protocol changed recently, for memcached (and nearly everything
implemented on top of TCP), you must completely parse the response the
server gives you.