refactor: provide http server as tower service (#228)

* refactor: streamable http server as tower service
This commit is contained in:
4t145 2025-05-28 19:21:40 +08:00 • committed by GitHub
commit 209be7bdb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 1828 additions and 1180 deletions

View file

@ -111,7 +111,7 @@ pub async fn serve_client<S, T, E, A>(
where
S: Service<RoleClient>,
T: IntoTransport<RoleClient, E, A>,
E: std::error::Error + From<std::io::Error> + Send + Sync + 'static,
E: std::error::Error + Send + Sync + 'static,
{
serve_client_with_ct(service, transport, Default::default()).await
}
@ -124,7 +124,7 @@ pub async fn serve_client_with_ct<S, T, E, A>(
where
S: Service<RoleClient>,
T: IntoTransport<RoleClient, E, A>,
E: std::error::Error + From<std::io::Error> + Send + Sync + 'static,
E: std::error::Error + Send + Sync + 'static,
{
let mut transport = transport.into_transport();
let id_provider = <Arc<AtomicU32RequestIdProvider>>::default();
@ -175,7 +175,7 @@ where
context: "send initialized notification".into(),
})?;
let (peer, peer_rx) = Peer::new(id_provider, Some(initialize_result));
Ok(serve_inner(service, transport, peer, peer_rx, ct).await)
Ok(serve_inner(service, transport, peer, peer_rx, ct))
}
macro_rules! method {

View file

@ -70,7 +70,7 @@ impl<S: Service<RoleServer>> ServiceExt<RoleServer> for S {
) -> impl Future<Output = Result<RunningService<RoleServer, Self>, ServerInitializeError<E>>> + Send
where
T: IntoTransport<RoleServer, E, A>,
E: std::error::Error + From<std::io::Error> + Send + Sync + 'static,
E: std::error::Error + Send + Sync + 'static,
Self: Sized,
{
serve_server_with_ct(self, transport, ct)
@ -84,7 +84,7 @@ pub async fn serve_server<S, T, E, A>(
where
S: Service<RoleServer>,
T: IntoTransport<RoleServer, E, A>,
E: std::error::Error + From<std::io::Error> + Send + Sync + 'static,
E: std::error::Error + Send + Sync + 'static,
{
serve_server_with_ct(service, transport, CancellationToken::new()).await
}
@ -143,7 +143,7 @@ pub async fn serve_server_with_ct<S, T, E, A>(
where
S: Service<RoleServer>,
T: IntoTransport<RoleServer, E, A>,
E: std::error::Error + From<std::io::Error> + Send + Sync + 'static,
E: std::error::Error + Send + Sync + 'static,
{
let mut transport = transport.into_transport();
let id_provider = <Arc<AtomicU32RequestIdProvider>>::default();
@ -212,7 +212,7 @@ where
};
let _ = service.handle_notification(notification).await;
// Continue processing service
Ok(serve_inner(service, transport, peer, peer_rx, ct).await)
Ok(serve_inner(service, transport, peer, peer_rx, ct))
}
macro_rules! method {