cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Does it makes sense to set SO_LINGER option for timed out TCP connections?

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Fri, 31 Dec 2010 23:29:53 +0100 (CET)

On Fri, 31 Dec 2010, Mykyta Dorokhin wrote:

> Now the problem. Once my inactivity timer goes off I return non-zero
> value from curl_progress_callback and curl_easy_perform returns
> CURLE_ABORTED_BY_CALLBACK code. In this case libcurl closes the
> connection, by, I assume, calling close(sock).

That is correct.

> Close() call returns
> immediately and my retry logic algorithm proceed with additional
> attempts. If there is an unsent data in the TCP socket buffer then, as
> I understand, TCP FIN segments will not be sent immediately. Taking
> into account that TCP uses 1,2,4,8,... retransmission intervals then
> it is possible that the connection quality is acceptable already but
> the data is still not being sent for, say 16 or more seconds. It means
> that the connection will remain open on the server for quite a long
> time even though there is no need in it.

In the client too if there really was lingering data to send...

> So, I think I could take advantage of SO_LINGER socket option since it
> seems may be used to force TCP/IP stack to sent the connection reset
> state to the server immediately.

SO_LINGER changes how close() works so that it will wait until all data has
been sent (or it reaches a timeout) and return info for you about it.

But when you close down the connection due to inactivity then it would be a
sign that data is not sent to the peer and then I don't see how you will be
able to send the data suddenly when you instead close() the connection?

> It least this can lower the number of
> stale connections on the server.

I don't understand how it would be able to do that.

> 2) How can I set SO_LINGER option to the socket?

The CURLOPT_SOCKOPTFUNCTION callback is sort of designed for things like this.

> 3) Is it possible to set SO_LINGER option just before closing the
> connection? Not sure if it is safe to use the options form all
> communications I have,

If you use the multi_action_* API you should be able to do that, but otherwise
you can't since the exact socket in use is not easily available to
applications.

-- 
  / daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2010-12-31