Skip to content

Commit

Permalink
Merge pull request #203 from dbr/polltimeout
Browse files Browse the repository at this point in the history
Add Server.poll_timeout method
  • Loading branch information
tomaka authored Jun 8, 2019
2 parents fd02d79 + 1296a49 commit 41128ed
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,33 @@ impl<F> Server<F> where F: Send + Sync + 'static + Fn(&Request) -> Response {
}
}

/// Same as `poll()` but blocks for at most `duration` before returning.
///
/// This function can be used implement a custom server loop in a more CPU-efficient manner
/// than calling `poll`.
///
/// # Example
///
/// ```no_run
/// use rouille::Server;
/// use rouille::Response;
///
/// let server = Server::new("localhost:0", |request| {
/// Response::text("hello world")
/// }).unwrap();
/// let mut run = true;
/// while run {
/// server.poll_timeout(std::time::Duration::from_millis(100));
/// }
/// ```
#[inline]
pub fn poll_timeout(&self, dur: std::time::Duration) {
while let Ok(Some(request)) = self.server.recv_timeout(dur) {
self.process(request);
}
}

// Internal function, called when we got a request from tiny-http that needs to be processed.
fn process(&self, request: tiny_http::Request) {
// We spawn a thread so that requests are processed in parallel.
Expand Down

0 comments on commit 41128ed

Please sign in to comment.