Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CoderNate opened this issue Jan 6, 2023 · 3 comments
Open

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

CoderNate opened this issue Jan 6, 2023 · 3 comments

Comments

@CoderNate
Copy link

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants