Skip to content

Commit

Permalink
add tcp nodelay option
Browse files Browse the repository at this point in the history
  • Loading branch information
fiag committed Mar 2, 2021
1 parent 74c376d commit 44ff15f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/listener/tcp_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ where
}

Ok(stream) => {
if let Some(server) = &self.server {
stream.set_nodelay(server.tcp_nodelay())?;
}
handle_tcp(server.clone(), stream);
}
};
Expand Down
29 changes: 29 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Server<State> {
/// We don't use a Mutex around the Vec here because adding a middleware during execution should be an error.
#[allow(clippy::rc_buffer)]
middleware: Arc<Vec<Arc<dyn Middleware<State>>>>,
tcp_nodelay: bool,
}

impl Server<()> {
Expand Down Expand Up @@ -113,6 +114,7 @@ where
Arc::new(log::LogMiddleware::new()),
]),
state,
tcp_nodelay: false,
}
}

Expand Down Expand Up @@ -286,6 +288,7 @@ where
router,
state,
middleware,
tcp_nodelay: _tcp_nodelay,
} = self.clone();

let method = req.method().to_owned();
Expand Down Expand Up @@ -317,6 +320,31 @@ where
pub fn state(&self) -> &State {
&self.state
}

/// Gets the value of the TCP_NODELAY option for tcp connections.
pub fn tcp_nodelay(&self) -> bool {
self.tcp_nodelay
}

/// Set the TCP_NODELAY option for tcp connections.
///
/// # Examples
///
/// ```no_run
/// # use async_std::task::block_on;
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
/// #
/// let mut app = tide::new();
/// app.at("/").get(|_| async { Ok("Hello, world!") });
/// app.set_tcp_nodelay(true);
/// app.listen("127.0.0.1:8080").await?;
/// #
/// # Ok(()) }) }
/// ```
pub fn set_tcp_nodelay(&mut self, tcp_nodelay: bool) -> &mut Self {
self.tcp_nodelay = tcp_nodelay;
self
}
}

impl<State: Send + Sync + 'static> std::fmt::Debug for Server<State> {
Expand All @@ -331,6 +359,7 @@ impl<State: Clone> Clone for Server<State> {
router: self.router.clone(),
state: self.state.clone(),
middleware: self.middleware.clone(),
tcp_nodelay: self.tcp_nodelay,
}
}
}
Expand Down

0 comments on commit 44ff15f

Please sign in to comment.