diff --git a/rama/examples/tokio_tcp_echo_server.rs b/rama/examples/tokio_tcp_echo_server.rs index b76ac3f0..11ea0528 100644 --- a/rama/examples/tokio_tcp_echo_server.rs +++ b/rama/examples/tokio_tcp_echo_server.rs @@ -1,9 +1,9 @@ use std::time::Duration; -use rama::graceful::Shutdown; -use rama::server::tcp::TcpListener; -use rama::service::{limit::ConcurrentPolicy, spawn::SpawnLayer}; -use rama::stream::service::EchoService; +use rama::{ + graceful::Shutdown, server::tcp::TcpListener, service::limit::ConcurrentPolicy, + stream::service::EchoService, +}; use tracing::metadata::LevelFilter; use tracing_subscriber::{fmt, prelude::*, EnvFilter}; @@ -25,7 +25,7 @@ async fn main() { TcpListener::bind("127.0.0.1:8080") .await .expect("bind TCP Listener") - .layer(SpawnLayer::new()) + .spawn() .limit(ConcurrentPolicy::new(2)) .timeout(Duration::from_secs(30)) .serve_graceful::<_, EchoService, _>(guard, EchoService::new()) diff --git a/rama/src/server/tcp/listener.rs b/rama/src/server/tcp/listener.rs index 8711e754..a0444990 100644 --- a/rama/src/server/tcp/listener.rs +++ b/rama/src/server/tcp/listener.rs @@ -95,6 +95,11 @@ impl TcpListener { self.layer(crate::service::layer_fn(f)) } + /// Spawn a task to handle each incoming request. + pub fn spawn(self) -> TcpListener> { + self.layer(crate::service::spawn::SpawnLayer::new()) + } + /// Fail requests that take longer than `timeout`. pub fn timeout( self,