Skip to content

Commit

Permalink
add built-in graceful support
Browse files Browse the repository at this point in the history
(re-exports tokio-graceful for convenience as well)
  • Loading branch information
glendc committed Nov 4, 2023
1 parent acb418d commit 5365d43
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ http = "0.2.9"
matchit = "0.7.3"
pin-project-lite = "0.2.13"
tokio = { version = "1.33.0", features = ["net", "io-util"] }
tokio-graceful = "0.1.5"
tower-async-layer = "0.1.1"
tower-async-service = "0.1.1"

[dev-dependencies]
Expand Down
50 changes: 50 additions & 0 deletions rama/src/graceful.rs
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())
}
}
1 change: 1 addition & 0 deletions rama/src/lib.rs
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;

0 comments on commit 5365d43

Please sign in to comment.