feat: add cancellation_token method to RunningService (#218)
This commit is contained in:
parent
0d52b7d24f
commit
633d42425a
1 changed files with 17 additions and 2 deletions
|
|
@ -424,14 +424,14 @@ pub struct RunningService<R: ServiceRole, S: Service<R>> {
|
||||||
service: Arc<S>,
|
service: Arc<S>,
|
||||||
peer: Peer<R>,
|
peer: Peer<R>,
|
||||||
handle: tokio::task::JoinHandle<QuitReason>,
|
handle: tokio::task::JoinHandle<QuitReason>,
|
||||||
/// cancellation token with drop guard
|
cancellation_token: CancellationToken,
|
||||||
dg: DropGuard,
|
dg: DropGuard,
|
||||||
}
|
}
|
||||||
impl<R: ServiceRole, S: Service<R>> Deref for RunningService<R, S> {
|
impl<R: ServiceRole, S: Service<R>> Deref for RunningService<R, S> {
|
||||||
type Target = Peer<R>;
|
type Target = Peer<R>;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
self.peer()
|
&self.peer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -444,6 +444,11 @@ impl<R: ServiceRole, S: Service<R>> RunningService<R, S> {
|
||||||
pub fn service(&self) -> &S {
|
pub fn service(&self) -> &S {
|
||||||
self.service.as_ref()
|
self.service.as_ref()
|
||||||
}
|
}
|
||||||
|
#[inline]
|
||||||
|
pub fn cancellation_token(&self) -> RunningServiceCancellationToken {
|
||||||
|
RunningServiceCancellationToken(self.cancellation_token.clone())
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
pub async fn waiting(self) -> Result<QuitReason, tokio::task::JoinError> {
|
pub async fn waiting(self) -> Result<QuitReason, tokio::task::JoinError> {
|
||||||
self.handle.await
|
self.handle.await
|
||||||
}
|
}
|
||||||
|
|
@ -454,6 +459,15 @@ impl<R: ServiceRole, S: Service<R>> RunningService<R, S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use a wrapper type so we can tweak the implementation if needed
|
||||||
|
pub struct RunningServiceCancellationToken(CancellationToken);
|
||||||
|
|
||||||
|
impl RunningServiceCancellationToken {
|
||||||
|
pub fn cancel(self) {
|
||||||
|
self.0.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum QuitReason {
|
pub enum QuitReason {
|
||||||
Cancelled,
|
Cancelled,
|
||||||
|
|
@ -801,6 +815,7 @@ where
|
||||||
service,
|
service,
|
||||||
peer: peer_return,
|
peer: peer_return,
|
||||||
handle,
|
handle,
|
||||||
|
cancellation_token: ct.clone(),
|
||||||
dg: ct.drop_guard(),
|
dg: ct.drop_guard(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue