adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
531 stars 128 forks source link

Can't find way to set WebSocket connect timeout #355

Open CoderNate opened 1 year ago

CoderNate commented 1 year ago

I've copied the WebSocket code into my project to work around this but figured I should still mention it.

Ubuntu 20 on WSL2
I have a program like this:

/+ dub.sdl:
dependency "arsd-official:http" version="~>10.9.8"
+/

import arsd.http2: WebSocket, Uri;
import std.stdio: writeln;

void main(string[] args) {
        auto ws = new WebSocket(Uri(args[1]));
        ws.connect();
        writeln("Connected!");
}

If I try to connect to a port with nothing listening it takes over 2 minutes to time out:

$ time dub run --single websocket_test.d -- ws://192.168.1.141:27985
arsd-official:http 10.9.8: target for configuration "with_openssl" is up to date.
websocket_test ~master: target for configuration "application" is up to date.
To force a rebuild of up-to-date targets, run again with --force.
Running websocket_test wss://192.168.1.141:27985
std.socket.SocketOSException@std/socket.d(2852): Unable to connect socket: Connection timed out
----------------
??:? @trusted void std.socket.Socket.connect(std.socket.Address) [0x560cc32d092f]
../.dub/packages/arsd-official-10.9.8/arsd-official/http2.d:3890 @trusted void arsd.http2.OpenSslSocket.connect(std.socket.Address) [0x560cc328d248]
../.dub/packages/arsd-official-10.9.8/arsd-official/http2.d:4475 void arsd.http2.WebSocket.connect() [0x560cc328deaa]
./websocket_test.d:15 _Dmain [0x560cc3281d61]
Program exited with code 1

real    2m12.553s
user    0m0.348s
sys     0m0.030s

On Windows it times out in a couple seconds. Maybe there's a way to change the socket defaults for the process somehow but it would be nice if the WebSocket had a config option for it since its underlying Socket is private; or at least having a hard-coded timeout that's shorter would be good. Modifying the socket's send timeout by adding this to the WebSocket constructor:

socket.setOption(SocketOptionLevel.SOCKET, SocketOption.SNDTIMEO, 5.seconds);

results in std.socket.SocketOSException@std/socket.d(2852): Unable to connect socket: Operation now in progress after 7 seconds. Maybe there's a fancier way using select or something though.

Thanks!

adamdruppe commented 1 year ago

http://arsd-official.dpldocs.info/arsd.http2.WebSocket.this.html

There's a second argument for the constructor of type Config:

http://arsd-official.dpldocs.info/arsd.http2.WebSocket.Config.html

Which has a member timeoutFromInactivity:

http://arsd-official.dpldocs.info/arsd.http2.WebSocket.Config.timeoutFromInactivity.html

.......oooh but that doesn't apply for the connect step. So there's the bug lemme see about fixing that.

adamdruppe commented 1 year ago

Just pushed a thing to master, if you wanna grab that and run some tests I think that fixes it so the timeout from inactivity config setting is respected.

I plan on rewriting this under my new async system at some point in the next few months, when I can find the time in between other work, but this should work for now.

CoderNate commented 1 year ago

but that doesn't apply for the connect step

Sorry yeah I forgot to mention that part.

I tried out that fix and it works. Thanks!