Skip to content

Commit

Permalink
make spawn layer part of built-in tcp listener util
Browse files Browse the repository at this point in the history
  • Loading branch information
glendc committed Nov 4, 2023
1 parent c74f161 commit b458c62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 5 additions & 5 deletions rama/examples/tokio_tcp_echo_server.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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())
Expand Down
5 changes: 5 additions & 0 deletions rama/src/server/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ impl<L> TcpListener<L> {
self.layer(crate::service::layer_fn(f))
}

/// Spawn a task to handle each incoming request.
pub fn spawn(self) -> TcpListener<Stack<crate::service::spawn::SpawnLayer, L>> {
self.layer(crate::service::spawn::SpawnLayer::new())
}

/// Fail requests that take longer than `timeout`.
pub fn timeout(
self,
Expand Down

0 comments on commit b458c62

Please sign in to comment.