-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(re-exports tokio-graceful for convenience as well)
- Loading branch information
glendc
committed
Nov 4, 2023
1 parent
acb418d
commit 5365d43
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use tower_async_layer::Layer; | ||
use tower_async_service::Service; | ||
|
||
pub use tokio_graceful::*; | ||
|
||
use crate::state::Extendable; | ||
|
||
pub struct ShutdownGuardAdder<S> { | ||
inner: S, | ||
guard: ShutdownGuard, | ||
} | ||
|
||
impl<S> ShutdownGuardAdder<S> { | ||
fn new(inner: S, guard: ShutdownGuard) -> Self { | ||
Self { inner, guard } | ||
} | ||
} | ||
|
||
impl<S, Request> Service<Request> for ShutdownGuardAdder<S> | ||
where | ||
S: Service<Request>, | ||
Request: Extendable, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
|
||
async fn call(&mut self, mut request: Request) -> Result<Self::Response, Self::Error> { | ||
let guard = self.guard.clone(); | ||
request.extensions_mut().insert(guard); | ||
self.inner.call(request).await | ||
} | ||
} | ||
|
||
pub struct ShutdownGuardAdderLayer { | ||
guard: ShutdownGuard, | ||
} | ||
|
||
impl ShutdownGuardAdderLayer { | ||
pub fn new(guard: ShutdownGuard) -> Self { | ||
Self { guard } | ||
} | ||
} | ||
|
||
impl<S> Layer<S> for ShutdownGuardAdderLayer { | ||
type Service = ShutdownGuardAdder<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
ShutdownGuardAdder::new(inner, self.guard.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#![feature(async_fn_in_trait)] | ||
|
||
pub mod graceful; | ||
pub mod server; | ||
pub mod state; | ||
pub mod stream; |