-
-
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.
improve ergonomics of tcp echo example
- Loading branch information
glendc
committed
Nov 4, 2023
1 parent
867bee2
commit d532583
Showing
9 changed files
with
123 additions
and
39 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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
#![feature(async_fn_in_trait)] | ||
#![feature(return_type_notation)] | ||
#![allow(incomplete_features)] | ||
|
||
pub mod graceful; | ||
pub mod server; | ||
pub mod service; | ||
pub mod state; | ||
pub mod stream; | ||
|
||
pub use tower_async_layer::Layer; | ||
pub use tower_async_service::Service; |
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,7 @@ | ||
pub use tower_async::{service_fn, Layer, Service, ServiceBuilder}; | ||
|
||
pub mod util { | ||
pub use tower_async::layer::util::{Identity, Stack}; | ||
} | ||
|
||
pub mod spawn; |
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,62 @@ | ||
use crate::{ | ||
graceful::ShutdownGuard, | ||
service::{Layer, Service}, | ||
state::Extendable, | ||
}; | ||
|
||
pub struct SpawnService<S> { | ||
inner: S, | ||
} | ||
|
||
impl<S> SpawnService<S> { | ||
pub fn new(inner: S) -> Self { | ||
Self { inner } | ||
} | ||
} | ||
|
||
impl<S, Request> Service<Request> for SpawnService<S> | ||
where | ||
S: Service<Request, call(): Send> + Clone + Send + 'static, | ||
S::Error: std::error::Error, | ||
Request: Extendable + Send + 'static, | ||
{ | ||
type Response = (); | ||
type Error = std::convert::Infallible; | ||
|
||
async fn call(&mut self, request: Request) -> Result<Self::Response, Self::Error> { | ||
let mut service = self.inner.clone(); | ||
if let Some(guard) = request.extensions().get::<ShutdownGuard>() { | ||
guard.clone().spawn_task(async move { | ||
if let Err(err) = service.call(request).await { | ||
tracing::error!( | ||
error = &err as &dyn std::error::Error, | ||
"graceful service error" | ||
); | ||
} | ||
}); | ||
} else { | ||
tokio::spawn(async move { | ||
if let Err(err) = service.call(request).await { | ||
tracing::error!(error = &err as &dyn std::error::Error, "service error"); | ||
} | ||
}); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct SpawnLayer(()); | ||
|
||
impl SpawnLayer { | ||
pub fn new() -> Self { | ||
Self(()) | ||
} | ||
} | ||
|
||
impl<S> Layer<S> for SpawnLayer { | ||
type Service = SpawnService<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
SpawnService::new(inner) | ||
} | ||
} |
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