feat(server): change Incoming to iterator over Connections

A connection is returned from Incoming.next(), and can be passed to a
separate thread before any parsing happens. Call conn.open() to get a
Result<(Request, Response)>.

BREAKING CHANGE
This commit is contained in:
Sean McArthur
2014-11-10 13:55:11 -08:00
parent 0500b5f17f
commit 3c10a8a191
6 changed files with 45 additions and 34 deletions

View File

@@ -11,21 +11,6 @@ use hyper::server::{Server, Handler, Incoming, Request, Response};
use hyper::header::common::ContentLength;
use hyper::net::{HttpStream, HttpAcceptor, Fresh};
trait ConcurrentHandler: Send + Sync {
fn handle(&self, req: Request, res: Response<Fresh>);
}
struct Concurrent<H: ConcurrentHandler> { handler: Arc<H> }
impl<H: ConcurrentHandler> Handler<HttpAcceptor, HttpStream> for Concurrent<H> {
fn handle(self, mut incoming: Incoming) {
for (mut req, mut res) in incoming {
let clone = self.handler.clone();
spawn(proc() { clone.handle(req, res) })
}
}
}
macro_rules! try_abort(
($e:expr) => {{
match $e {
@@ -35,6 +20,24 @@ macro_rules! try_abort(
}}
)
trait ConcurrentHandler: Send + Sync {
fn handle(&self, req: Request, res: Response<Fresh>);
}
struct Concurrent<H: ConcurrentHandler> { handler: Arc<H> }
impl<H: ConcurrentHandler> Handler<HttpAcceptor, HttpStream> for Concurrent<H> {
fn handle(self, mut incoming: Incoming) {
for conn in incoming {
let clone = self.handler.clone();
spawn(proc() {
let (req, res) = try_abort!(conn.open());
clone.handle(req, res);
})
}
}
}
struct Echo;
impl ConcurrentHandler for Echo {

View File

@@ -8,8 +8,9 @@ static PHRASE: &'static [u8] = b"Hello World!";
fn hyper_handle(mut incoming: hyper::server::Incoming) {
let mut pool = TaskPool::new(100, || { proc(_) { } });
for (_, mut res) in incoming {
for conn in incoming {
pool.execute(proc(_) {
let (_, res) = conn.open().unwrap();
let mut res = res.start().unwrap();
res.write(PHRASE).unwrap();
res.end().unwrap();

View File

@@ -19,7 +19,8 @@ macro_rules! try_continue(
)
fn echo(mut incoming: Incoming) {
for (mut req, mut res) in incoming {
for conn in incoming {
let (mut req, mut res) = try_continue!(conn.open());
match req.uri {
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
(&Get, "/") | (&Get, "/echo") => {