Skip to content

Conversation

@pronebird
Copy link
Contributor

@pronebird pronebird commented Oct 14, 2025

This is a draft PR that adds connect timeout for websocket. Since tokio::net::TcpSocket does not support connect_timeout() from std, the next close alternative using tokio::time::timeout() is used instead.

Connect timeout only limits how much time the call to TcpSocket::connect() may take before giving up.

A few notes:

  • The structure of project seems to be very vertical which results into dragging variables all the way down
  • I don't understand how to pass connect_timeout in wasm client. It's not that vpn-client needs wasm, but for consistency.

This change is Reviewable

@pronebird pronebird force-pushed the am/ws-set-conn-timeout branch from 1ee18e9 to 889465d Compare October 14, 2025 10:21
@vercel
Copy link

vercel bot commented Oct 14, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
nym-explorer-v2 Ready Ready Preview Comment Oct 23, 2025 10:53am
nym-node-status Ready Ready Preview Comment Oct 23, 2025 10:53am
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs-nextra Ignored Ignored Preview Oct 23, 2025 10:53am

Copy link
Contributor

@jstuczyn jstuczyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And as for wasm, that's a tricky one because there's no underlying connect_async function as internally it just calls browser's WebSocket and relies on callbacks for knowing when the connection has been opened. And gloo's implementation (the library we're using) is using a blocking call. Perhaps you could introduce js' timeout and cancel the socket in that case?

available_gateways,
#[cfg(unix)]
connection_fd_callback: None,
connect_timeout: None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure connection timeout is a propery of a GatewaySetup (I don't like connection_fd_callback being there either). but if it has to be there, maybe it should be wrapped into some sort of config (alongside that callback)?

self
}

pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would it be easier for you if rather than setting connection timeout as a separate thing of the client, you put it as part of the base client config. gateway_connection section inside the DebugConfig sounds like the perfect place for it, wouldn't you say?

packet_router: PacketRouter,
stats_reporter: ClientStatsSender,
#[cfg(unix)] connection_fd_callback: Option<Arc<dyn Fn(RawFd) + Send + Sync>>,
connect_timeout: Option<Duration>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then I guess (with my previous comment in mind), all of those extra changes could be avoided

use std::net::SocketAddr;

#[cfg(not(target_arch = "wasm32"))]
pub(crate) async fn connect_async(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while going through the code, I've noticed we have identical connect_async function in common/client-core/src/init/websockets.rs (don't ask me why - I wish I knew). and actually when you go to where it is being called (the duplicate), it's already wrapped in a timeout - perhaps we could remove some duplication there?

match tokio::time::timeout(connect_timeout, socket.connect(sock_addr)).await {
Ok(res) => res,
Err(_elapsed) => {
stream = Err(GatewayClientError::NetworkConnectionTimeout {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't existing GatewayConnectionTimeout variant be more appropriate?

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

Successfully merging this pull request may close these issues.

3 participants