vapor-community / sockets

🔌 Non-blocking TCP socket layer, with event-driven server and client.
MIT License
575 stars 54 forks source link

Send/receive timeouts #50

Closed czechboy0 closed 8 years ago

czechboy0 commented 8 years ago

Added sending and receiving timeout options to SocketOptions. This allows callers to specify timeouts on a socket. Please note that 0 timeout means wait forever, not return immediately, so be careful when integrating with other libraries, some use -1 as wait forever, which is an invalid value here.

/cc @tannernelson

Fixes #30, #48

tanner0101 commented 8 years ago

Sweet! If this is merged/tagged today it can make it in Vapor 0.11.

czechboy0 commented 8 years ago

It's tagged as 0.6.0 as there are new APIs, more notably the timeout options. Please note that you'll have to set the receiving/sending timeout on the socket itself, not on each request. So to conform to C7 you'll probably want to 1. read the current timeout, 2. set the desired timeout, 3. defer setting of the timeout back to the value in 1, 4. execute the send/receive.

Don't have time until Sunday, can help then (but it should be simple enough to do if you have time).

tanner0101 commented 8 years ago

I think I might wrap Socks' stream in a class/struct that conforms to C7.Stream. That way there can be a stored value of what the current timeout is, so multiple sends do not create extra setsocketopt calls which I believe involve the kernel which could be slow.

czechboy0 commented 8 years ago

Sounds useful, but if I were you I'd do the naive way first (set the timeout before each send/recv) and profile it so that you actually can tell how much CPU time it's taking.