feat(server): keep-alive!

Internals have been shuffled around such that Request and Reponse are
now given only a mutable reference to the stream, instead of being
allowed to consume it. This allows the server to re-use the streams if
keep-alive is true.

A task pool is used, and the number of the threads can currently be
adjusted by using the `listen_threads()` method on Server.

[breaking-change]
This commit is contained in:
Sean McArthur
2014-11-12 15:17:41 -08:00
parent 1f2f93cfea
commit 3cd9b10bcb
12 changed files with 167 additions and 232 deletions

View File

@@ -1,77 +0,0 @@
#![feature(macro_rules, default_type_params)]
extern crate hyper;
use std::io::util::copy;
use std::io::net::ip::Ipv4Addr;
use std::sync::Arc;
use hyper::{Get, Post};
use hyper::server::{Server, Handler, Incoming, Request, Response};
use hyper::header::common::ContentLength;
use hyper::net::{HttpStream, HttpAcceptor, Fresh};
macro_rules! try_abort(
($e:expr) => {{
match $e {
Ok(v) => v,
Err(..) => return
}
}}
)
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 {
fn handle(&self, mut req: Request, mut res: Response<Fresh>) {
match req.uri {
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
(&Get, "/") | (&Get, "/echo") => {
let out = b"Try POST /echo";
res.headers_mut().set(ContentLength(out.len()));
let mut res = try_abort!(res.start());
try_abort!(res.write(out));
try_abort!(res.end());
return;
},
(&Post, "/echo") => (), // fall through, fighting mutable borrows
_ => {
*res.status_mut() = hyper::status::NotFound;
try_abort!(res.start().and_then(|res| res.end()));
return;
}
},
_ => {
try_abort!(res.start().and_then(|res| res.end()));
return;
}
}
let mut res = try_abort!(res.start());
try_abort!(copy(&mut req, &mut res));
try_abort!(res.end());
}
}
fn main() {
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 3000);
server.listen(Concurrent { handler: Arc::new(Echo) }).unwrap();
}

View File

@@ -1,23 +1,16 @@
extern crate hyper;
use std::sync::TaskPool;
use std::io::net::ip::Ipv4Addr;
use hyper::server::{Request, Response};
static PHRASE: &'static [u8] = b"Hello World!";
fn hyper_handle(mut incoming: hyper::server::Incoming) {
let pool = TaskPool::new(100);
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();
});
}
fn hello(_: Request, res: Response) {
let mut res = res.start().unwrap();
res.write(PHRASE).unwrap();
res.end().unwrap();
}
fn main() {
hyper::Server::http(Ipv4Addr(127, 0, 0, 1), 3000).listen(hyper_handle).unwrap();
hyper::Server::http(Ipv4Addr(127, 0, 0, 1), 3000).listen(hello).unwrap();
}

View File

@@ -1,54 +1,52 @@
#![feature(macro_rules)]
#![feature(macro_rules, phase)]
extern crate hyper;
#[phase(plugin, link)] extern crate log;
use std::io::util::copy;
use std::io::net::ip::Ipv4Addr;
use hyper::{Get, Post};
use hyper::header::common::ContentLength;
use hyper::server::{Server, Incoming};
use hyper::server::{Server, Request, Response};
macro_rules! try_continue(
macro_rules! try_return(
($e:expr) => {{
match $e {
Ok(v) => v,
Err(e) => { println!("Error: {}", e); continue; }
Err(e) => { error!("Error: {}", e); return; }
}
}}
)
fn echo(mut incoming: 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") => {
let out = b"Try POST /echo";
fn echo(mut req: Request, mut res: Response) {
match req.uri {
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
(&Get, "/") | (&Get, "/echo") => {
let out = b"Try POST /echo";
res.headers_mut().set(ContentLength(out.len()));
let mut res = try_continue!(res.start());
try_continue!(res.write(out));
try_continue!(res.end());
continue;
},
(&Post, "/echo") => (), // fall through, fighting mutable borrows
_ => {
*res.status_mut() = hyper::status::NotFound;
try_continue!(res.start().and_then(|res| res.end()));
continue;
}
res.headers_mut().set(ContentLength(out.len()));
let mut res = try_return!(res.start());
try_return!(res.write(out));
try_return!(res.end());
return;
},
(&Post, "/echo") => (), // fall through, fighting mutable borrows
_ => {
try_continue!(res.start().and_then(|res| res.end()));
continue;
*res.status_mut() = hyper::status::NotFound;
try_return!(res.start().and_then(|res| res.end()));
return;
}
};
},
_ => {
try_return!(res.start().and_then(|res| res.end()));
return;
}
};
let mut res = try_continue!(res.start());
try_continue!(copy(&mut req, &mut res));
try_continue!(res.end());
}
let mut res = try_return!(res.start());
try_return!(copy(&mut req, &mut res));
try_return!(res.end());
}
fn main() {