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

jsonrpsee TowerService: how to detect when a WebSocket connection is closed. #1264

Closed
niklasad1 opened this issue Dec 12, 2023 · 1 comment · Fixed by #1284
Closed

jsonrpsee TowerService: how to detect when a WebSocket connection is closed. #1264

niklasad1 opened this issue Dec 12, 2023 · 1 comment · Fixed by #1284

Comments

@niklasad1
Copy link
Member

niklasad1 commented Dec 12, 2023

Previously with the RpcLogger it was easy to log when a connection was opened/closed but
it's not easy anymore because the TowerService::call returns once the HTTP request has
been answered.

Thus, for instance I guess a common use-case is the emit prometheus metrics when a connection is opened and closed but the following won't work correctly:

    let make_service = make_service_fn(move |_conn: &AddrStream| {
        async move {
            Ok::<_, Infallible>(service_fn(move |req| {
                let metrics = metrics.clone();
                let svc_builder = svc_builder.clone();
                let is_websocket = ws::is_upgrade_request(&req);
                let transport_label = if is_websocket { "ws" } else { "http" };

                let metrics = metrics.map(|m| MetricsLayer::new(m, transport_label));
                let rpc_middleware = RpcServiceBuilder::new().option_layer(metrics.clone());
                let mut svc = svc_builder
                    .set_rpc_middleware(rpc_middleware)
                    .build(methods.clone(), stop_handle.clone());

                async move {
                    if is_websocket {
                        let now = std::time::Instant::now();
                        metrics.as_ref().map(|m| m.ws_connect());
                        // This will return once the HTTP upgrade request has been answered.
                        let rp = svc.call(req).await;
                        // Thus, this will not actually be when the connection was closed
                        // but once the WS upgrade handshake is done
                        metrics.as_ref().map(|m| m.ws_disconnect(now));
                        rp
                    } else {
                        svc.call(req).await
                    }
                }
                .boxed()
            }))
        }
    });

Option 1

Introduce an alternative API to build the TowerService with future that resolves
once the connection is closed.

	/// Build a tower service and get a future to resolves when the connection
	/// has been terminated.
	pub fn build_with_close_handle(
		self,
		methods: impl Into<Methods>,
		stop_handle: StopHandle,
	) -> (TowerService<RpcMiddleware, HttpMiddleware>,  impl Future<Output = ()>) {

That is not great either because the TowerServiceBuilder can actually be reused/cloned to make another request....
Thus, the future will be fired once

Option 2

Tell folks to use the low-level API but annoying for just the logging stuff.

@niklasad1
Copy link
Member Author

niklasad1 commented Dec 12, 2023

For now we will stick with Option 2) and release v0.21
Then we have some time to think about a better solution to make it easier to implement.

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 a pull request may close this issue.

1 participant