Skip to content

Commit

Permalink
refactor(server): expose Http that implements ServerProto
Browse files Browse the repository at this point in the history
The main changes are:

* The entry point is how `Http`, the implementation of `ServerProto`.
  This type has a `new` constructor as well as builder methods to
  configure it.

* A high-level entry point of `Http::bind` was added which returns a
  `Server`. Binding a protocol to a port requires a socket address
  (where to bind) as well as the instance of `NewService`. Internally
  this creates a core and a TCP listener.

* The returned `Server` has a few methods to learn about itself, e.g.
  `local_addr` and `handle`, but mainly has two methods: `run` and
  `run_until`.

* The `Server::run` entry point will execute a server infinitely, never
  having it exit.

* The `Server::run_until` method is intended as a graceful shutdown
  mechanism. When the provided future resolves the server stops
  accepting connections immediately and then waits for a fixed period of
  time for all active connections to get torn down, after which the
  whole server is torn down anyway.

* Finally a `Http::bind_connection` method exists as a low-level entry
  point to spawning a server connection. This is used by `Server::run`
  as is intended for external use in other event loops if necessary or
  otherwise low-level needs.

BREAKING CHANGE: `Server` is no longer the pimary entry point. Instead,
  an `Http` type is created  and then either `bind` to receiver a `Server`,
  or it can be passed to other Tokio things.
  • Loading branch information
alexcrichton authored and seanmonstar committed Jan 18, 2017
1 parent 39a53fc commit f45e9c8
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 271 deletions.
30 changes: 21 additions & 9 deletions benches/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

extern crate futures;
extern crate hyper;
extern crate tokio_core;
extern crate pretty_env_logger;

extern crate test;
extern crate tokio_core;

use std::net::SocketAddr;

use futures::{Future, Stream};
use tokio_core::reactor::Core;
use tokio_core::reactor::{Core, Handle};
use tokio_core::net::TcpListener;

use hyper::client;
use hyper::header::{ContentLength, ContentType};
Expand All @@ -22,9 +24,7 @@ fn get_one_at_a_time(b: &mut test::Bencher) {
let _ = pretty_env_logger::init();
let mut core = Core::new().unwrap();
let handle = core.handle();

let addr = hyper::Server::http(&"127.0.0.1:0".parse().unwrap(), &handle).unwrap()
.handle(|| Ok(Hello), &handle).unwrap();
let addr = spawn_hello(&handle);

let client = hyper::Client::new(&handle);

Expand All @@ -47,9 +47,7 @@ fn post_one_at_a_time(b: &mut test::Bencher) {
let _ = pretty_env_logger::init();
let mut core = Core::new().unwrap();
let handle = core.handle();

let addr = hyper::Server::http(&"127.0.0.1:0".parse().unwrap(), &handle).unwrap()
.handle(|| Ok(Hello), &handle).unwrap();
let addr = spawn_hello(&handle);

let client = hyper::Client::new(&handle);

Expand Down Expand Up @@ -92,3 +90,17 @@ impl Service for Hello {
}

}

fn spawn_hello(handle: &Handle) -> SocketAddr {
let addr = "127.0.0.1:0".parse().unwrap();
let listener = TcpListener::bind(&addr, handle).unwrap();
let addr = listener.local_addr().unwrap();

let handle2 = handle.clone();
handle.spawn(listener.incoming().for_each(move |(socket, addr)| {
let http = hyper::server::Http::new();
http.bind_connection(&handle2, socket, addr, Hello);
Ok(())
}).then(|_| Ok(())));
return addr
}
10 changes: 4 additions & 6 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate pretty_env_logger;
//extern crate num_cpus;

use hyper::header::{ContentLength, ContentType};
use hyper::server::{Server, Service, Request, Response};
use hyper::server::{Http, Service, Request, Response};

static PHRASE: &'static [u8] = b"Hello World!";

Expand All @@ -31,9 +31,7 @@ impl Service for Hello {
fn main() {
pretty_env_logger::init().unwrap();
let addr = "127.0.0.1:3000".parse().unwrap();
let _server = Server::standalone(|tokio| {
Server::http(&addr, tokio)?
.handle(|| Ok(Hello), tokio)
}).unwrap();
println!("Listening on http://{}", addr);
let server = Http::new().bind(&addr, || Ok(Hello)).unwrap();
println!("Listening on http://{}", server.local_addr().unwrap());
server.run().unwrap();
}
13 changes: 5 additions & 8 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ extern crate log;

use hyper::{Get, Post, StatusCode};
use hyper::header::ContentLength;
use hyper::server::{Server, Service, Request, Response};

use hyper::server::{Http, Service, Request, Response};

static INDEX: &'static [u8] = b"Try POST /echo";

Expand Down Expand Up @@ -48,10 +47,8 @@ impl Service for Echo {
fn main() {
pretty_env_logger::init().unwrap();
let addr = "127.0.0.1:1337".parse().unwrap();
let (listening, server) = Server::standalone(|tokio| {
Server::http(&addr, tokio)?
.handle(|| Ok(Echo), tokio)
}).unwrap();
println!("Listening on http://{}", listening);
server.run();

let server = Http::new().bind(&addr, || Ok(Echo)).unwrap();
println!("Listening on http://{}", server.local_addr().unwrap());
server.run().unwrap();
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! [Server](server/index.html), along with a
//! [typed Headers system](header/index.html).
extern crate futures;
#[macro_use] extern crate futures;
extern crate futures_cpupool;
extern crate httparse;
#[macro_use] extern crate language_tags;
Expand Down
Loading

0 comments on commit f45e9c8

Please sign in to comment.