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

Configurable SocketConfig for servers #101

Open
gbaranski opened this issue Jan 13, 2024 · 1 comment
Open

Configurable SocketConfig for servers #101

gbaranski opened this issue Jan 13, 2024 · 1 comment

Comments

@gbaranski
Copy link
Owner

Adding configurable socket through SocketConfig could allow users to change the default heartbeat duration, timeout and message. It is already implemented on the client side.

For tungstenite:

async fn accept(
&self,
stream: TcpStream,
handle: &enfync::builtin::native::TokioHandle,
) -> Result<(Socket, Request), Error> {
let mut req0 = None;
let callback = |req: &http::Request<()>,
resp: http::Response<()>|
-> Result<http::Response<()>, ErrorResponse> {
let mut req1 = Request::builder()
.method(req.method().clone())
.uri(req.uri().clone())
.version(req.version());
for (k, v) in req.headers() {
req1 = req1.header(k, v);
}
let Ok(body) = req1.body(()) else {
return Err(ErrorResponse::default());
};
req0 = Some(body);
Ok(resp)
};
let socket = match self {
Acceptor::Plain => {
let socket = tokio_tungstenite::accept_hdr_async(stream, callback).await?;
Socket::new(socket, SocketConfig::default(), handle.clone())
}
#[cfg(feature = "native-tls")]
Acceptor::NativeTls(acceptor) => {
let tls_stream = acceptor.accept(stream).await?;
let socket = tokio_tungstenite::accept_hdr_async(tls_stream, callback).await?;
Socket::new(socket, SocketConfig::default(), handle.clone())
}
#[cfg(feature = "rustls")]
Acceptor::Rustls(acceptor) => {
let tls_stream = acceptor.accept(stream).await?;
let socket = tokio_tungstenite::accept_hdr_async(tls_stream, callback).await?;
Socket::new(socket, SocketConfig::default(), handle.clone())
}
};
let Some(req_body) = req0 else {
return Err("invalid request body".into());
};
Ok((socket, req_body))
}
}

and for Axum, on_upgrade_with_config() function is not used anywhere.

pub fn on_upgrade_with_config<E: ServerExt + 'static>(
self,
server: Server<E>,
socket_config: SocketConfig,
) -> Response {
self.ws.on_upgrade(move |socket| async move {
let handle = enfync::builtin::native::TokioHandle::try_adopt()
.expect("axum server runner only works in a tokio runtime");
let socket = Socket::new(socket, socket_config, handle);
server.accept(socket, self.request, self.address);
})
}

It comes down into three options:

  • Configuration through Server::create(), global options for all incoming sessions.
  • Configuration through Session::create(), but it could be a little bit tricky, as Socket is already created when the Session::create() is called.
  • Configuration through mentioned above on_upgrade_with_config() in case of Axum. It would work only for Axum

First option doesn't seem bad? Unless you want to have different configuration for each of the clients connected to your server, but is it actually necessary?

@UkoeHB
Copy link
Collaborator

UkoeHB commented Jan 13, 2024

and for Axum, on_upgrade_with_config() function is not used anywhere.

This function is used when implementing your Axum server.

Configuration through Server::create(), global options for all incoming sessions.

A global option does not work since you may need different Ping/Pong protocols based on the client (I use a custom ping/pong protocol for WASM clients).

Configuration through Session::create(), but it could be a little bit tricky, as Socket is already created when the Session::create() is called.

Yeah I looked at this option and the structure is not compatible with defining a custom SocketConfig.

Configuration through mentioned above on_upgrade_with_config() in case of Axum. It would work only for Axum

Doing the same for tungstenite is blocked by this issue. Right now we use SocketConfig::default() because there is not a good way to inject logic.

Clients

There is ClientConfig::socket_config() as you noted.

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