Fix linger-on-close behaviour to avoid rare failure conditions.
- Don't assume that when FIONREAD returns 0, that it's safe to close the
socket. There may still be data that's about to arrive, and we'll still
send an RST if the socket is confused, potentially confusing the client.
- Don't close the connection immediately after sending a successful
response; linger-on-close was only happening in the case of errors, but it
has to happen in case of success too, because the client doesn't
necessarily know we're about to close after this request, and may have
sent additional ones. (eg. if server.max-keep-alive-requests is small.)
- Don't close the connection immediately even if keep_alive is 0; there are
several reasons keep_alive can be 0. If the client requested Connection:
close, then it would be okay to close right away, since we can assume he
didn't send anything else. But it's harmless (and more resilient) to do
the lingering regardless.
- Increase the lingering timeout from 1s to 30s. In the vast majority of
cases, the timeout never kicks in anyway. The only times when it might
be needed are a) in race conditions, in which case timing out too early
defeats the purpose of lingering at all; b) if there's a lot of data,
which is basically the same as (a); or c) if the remote end disappears,
in which case we now suffer through a longer timeout... but we would
anyway, if we were waiting for them to receive our transmission.