Skip to content

Commit f45e9c8

Browse files
alexcrichtonseanmonstar
authored andcommitted
refactor(server): expose Http that implements ServerProto
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.
1 parent 39a53fc commit f45e9c8

6 files changed

Lines changed: 385 additions & 271 deletions

File tree

benches/end_to_end.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
extern crate futures;
55
extern crate hyper;
6-
extern crate tokio_core;
76
extern crate pretty_env_logger;
8-
97
extern crate test;
8+
extern crate tokio_core;
9+
10+
use std::net::SocketAddr;
1011

1112
use futures::{Future, Stream};
12-
use tokio_core::reactor::Core;
13+
use tokio_core::reactor::{Core, Handle};
14+
use tokio_core::net::TcpListener;
1315

1416
use hyper::client;
1517
use hyper::header::{ContentLength, ContentType};
@@ -22,9 +24,7 @@ fn get_one_at_a_time(b: &mut test::Bencher) {
2224
let _ = pretty_env_logger::init();
2325
let mut core = Core::new().unwrap();
2426
let handle = core.handle();
25-
26-
let addr = hyper::Server::http(&"127.0.0.1:0".parse().unwrap(), &handle).unwrap()
27-
.handle(|| Ok(Hello), &handle).unwrap();
27+
let addr = spawn_hello(&handle);
2828

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

@@ -47,9 +47,7 @@ fn post_one_at_a_time(b: &mut test::Bencher) {
4747
let _ = pretty_env_logger::init();
4848
let mut core = Core::new().unwrap();
4949
let handle = core.handle();
50-
51-
let addr = hyper::Server::http(&"127.0.0.1:0".parse().unwrap(), &handle).unwrap()
52-
.handle(|| Ok(Hello), &handle).unwrap();
50+
let addr = spawn_hello(&handle);
5351

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

@@ -92,3 +90,17 @@ impl Service for Hello {
9290
}
9391

9492
}
93+
94+
fn spawn_hello(handle: &Handle) -> SocketAddr {
95+
let addr = "127.0.0.1:0".parse().unwrap();
96+
let listener = TcpListener::bind(&addr, handle).unwrap();
97+
let addr = listener.local_addr().unwrap();
98+
99+
let handle2 = handle.clone();
100+
handle.spawn(listener.incoming().for_each(move |(socket, addr)| {
101+
let http = hyper::server::Http::new();
102+
http.bind_connection(&handle2, socket, addr, Hello);
103+
Ok(())
104+
}).then(|_| Ok(())));
105+
return addr
106+
}

examples/hello.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate pretty_env_logger;
55
//extern crate num_cpus;
66

77
use hyper::header::{ContentLength, ContentType};
8-
use hyper::server::{Server, Service, Request, Response};
8+
use hyper::server::{Http, Service, Request, Response};
99

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

@@ -31,9 +31,7 @@ impl Service for Hello {
3131
fn main() {
3232
pretty_env_logger::init().unwrap();
3333
let addr = "127.0.0.1:3000".parse().unwrap();
34-
let _server = Server::standalone(|tokio| {
35-
Server::http(&addr, tokio)?
36-
.handle(|| Ok(Hello), tokio)
37-
}).unwrap();
38-
println!("Listening on http://{}", addr);
34+
let server = Http::new().bind(&addr, || Ok(Hello)).unwrap();
35+
println!("Listening on http://{}", server.local_addr().unwrap());
36+
server.run().unwrap();
3937
}

examples/server.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ extern crate log;
77

88
use hyper::{Get, Post, StatusCode};
99
use hyper::header::ContentLength;
10-
use hyper::server::{Server, Service, Request, Response};
11-
10+
use hyper::server::{Http, Service, Request, Response};
1211

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

@@ -48,10 +47,8 @@ impl Service for Echo {
4847
fn main() {
4948
pretty_env_logger::init().unwrap();
5049
let addr = "127.0.0.1:1337".parse().unwrap();
51-
let (listening, server) = Server::standalone(|tokio| {
52-
Server::http(&addr, tokio)?
53-
.handle(|| Ok(Echo), tokio)
54-
}).unwrap();
55-
println!("Listening on http://{}", listening);
56-
server.run();
50+
51+
let server = Http::new().bind(&addr, || Ok(Echo)).unwrap();
52+
println!("Listening on http://{}", server.local_addr().unwrap());
53+
server.run().unwrap();
5754
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! [Server](server/index.html), along with a
1515
//! [typed Headers system](header/index.html).
1616
17-
extern crate futures;
17+
#[macro_use] extern crate futures;
1818
extern crate futures_cpupool;
1919
extern crate httparse;
2020
#[macro_use] extern crate language_tags;

0 commit comments

Comments
 (0)