fix: cancellable initialization process (#280)
This commit is contained in:
parent
1f7f4d3055
commit
57db771a81
2 changed files with 42 additions and 0 deletions
|
|
@ -39,6 +39,9 @@ pub enum ClientInitializeError<E> {
|
||||||
error: E,
|
error: E,
|
||||||
context: Cow<'static, str>,
|
context: Cow<'static, str>,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
#[error("Cancelled")]
|
||||||
|
Cancelled,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function to get the next message from the stream
|
/// Helper function to get the next message from the stream
|
||||||
|
|
@ -121,6 +124,24 @@ pub async fn serve_client_with_ct<S, T, E, A>(
|
||||||
transport: T,
|
transport: T,
|
||||||
ct: CancellationToken,
|
ct: CancellationToken,
|
||||||
) -> Result<RunningService<RoleClient, S>, ClientInitializeError<E>>
|
) -> Result<RunningService<RoleClient, S>, ClientInitializeError<E>>
|
||||||
|
where
|
||||||
|
S: Service<RoleClient>,
|
||||||
|
T: IntoTransport<RoleClient, E, A>,
|
||||||
|
E: std::error::Error + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
tokio::select! {
|
||||||
|
result = serve_client_with_ct_inner(service, transport, ct.clone()) => { result }
|
||||||
|
_ = ct.cancelled() => {
|
||||||
|
Err(ClientInitializeError::Cancelled)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn serve_client_with_ct_inner<S, T, E, A>(
|
||||||
|
service: S,
|
||||||
|
transport: T,
|
||||||
|
ct: CancellationToken,
|
||||||
|
) -> Result<RunningService<RoleClient, S>, ClientInitializeError<E>>
|
||||||
where
|
where
|
||||||
S: Service<RoleClient>,
|
S: Service<RoleClient>,
|
||||||
T: IntoTransport<RoleClient, E, A>,
|
T: IntoTransport<RoleClient, E, A>,
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,9 @@ pub enum ServerInitializeError<E> {
|
||||||
error: E,
|
error: E,
|
||||||
context: Cow<'static, str>,
|
context: Cow<'static, str>,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
#[error("Cancelled")]
|
||||||
|
Cancelled,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ClientSink = Peer<RoleServer>;
|
pub type ClientSink = Peer<RoleServer>;
|
||||||
|
|
@ -140,6 +143,24 @@ pub async fn serve_server_with_ct<S, T, E, A>(
|
||||||
transport: T,
|
transport: T,
|
||||||
ct: CancellationToken,
|
ct: CancellationToken,
|
||||||
) -> Result<RunningService<RoleServer, S>, ServerInitializeError<E>>
|
) -> Result<RunningService<RoleServer, S>, ServerInitializeError<E>>
|
||||||
|
where
|
||||||
|
S: Service<RoleServer>,
|
||||||
|
T: IntoTransport<RoleServer, E, A>,
|
||||||
|
E: std::error::Error + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
tokio::select! {
|
||||||
|
result = serve_server_with_ct_inner(service, transport, ct.clone()) => { result }
|
||||||
|
_ = ct.cancelled() => {
|
||||||
|
Err(ServerInitializeError::Cancelled)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn serve_server_with_ct_inner<S, T, E, A>(
|
||||||
|
service: S,
|
||||||
|
transport: T,
|
||||||
|
ct: CancellationToken,
|
||||||
|
) -> Result<RunningService<RoleServer, S>, ServerInitializeError<E>>
|
||||||
where
|
where
|
||||||
S: Service<RoleServer>,
|
S: Service<RoleServer>,
|
||||||
T: IntoTransport<RoleServer, E, A>,
|
T: IntoTransport<RoleServer, E, A>,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue