1

I'm trying to implement the onSocketOption delegate for a Curl's HTTP object (http://erdani.com/d/phobos-prerelease/std_net_curl.html#.Curl.onSocketOption).

int onSocketOption(curl_socket_t socketfd,
                   CurlSockType type) {
        import std.socket:Socket, SocketOptionLevel, SocketOption, Linger, AddressFamily;

        Socket sock = new Socket(socketfd, AddressFamily.INET);

        Linger lingerOpt;
        lingerOpt.on = 0;
        lingerOpt.time = 0;

        sock.setOption(SocketOptionLevel.SOCKET,
                       SocketOption.LINGER,
                       lingerOpt);

        return 0;
};

Sometimes It seems to work, sometimes I get std.socket.SocketOSException@/tmp/ldc.build/ldc/runtime/phobos/std/socket.d(3175): Unable to set socket option: Bad file descriptor

Is there another way to use that "curl_socket_t"? Its an alias of socket_t which in turn should just be a regular socket fd (running on linux)

Notice that HTTP doesn't have that callback, so I have a fork of std.net.curl that adds that interface to the "Protocol" mixin in curl.d:

@property void onSocketOption(int delegate(curl_socket_t,
                                           CurlSockType) callback)
{
    p.curl.onSocketOption = callback;
}
ordahan
  • 187
  • 9
  • Why? The best advice I can give you about the SO_LINGER option is not to mess around with it. The default settings are perfectly adequate for all but pathological cases. The way you're using it here is particularly inadvisable. – user207421 Dec 11 '15 at 09:21
  • I got 64 concurrent CURL connections, after each finishes it leaves around a `TIME_WAIT` socket open.. I get thousands of them, its starting to cause problems. – ordahan Dec 11 '15 at 09:35
  • http://stackoverflow.com/a/3760186 – Kozzi11 Dec 12 '15 at 12:03
  • I understand, and suspected, the problem, but SO_LINGER is the wrong solution. For one thing it causes loss of data in flight. The correct solution is to redesign your protocol to one where the client initiates the connection close, if possible, which is typically done via connection pooling at the client, as seen in HTTP for example. – user207421 Dec 13 '15 at 01:45
  • 1
    I'm actually the client in this scenario. I know my solution is going towards pooling persistent sockets and re-using them, but until I will get to that point I need a quick fix. @EJP where are those said examples? is it an internal implementation of `std.net.curl.HTTP`? – ordahan Dec 13 '15 at 09:38

0 Answers0