Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): Creating Server with a Handle WIP #1332

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ use http::Body;
pub use http::response::Response;
pub use http::request::Request;

/// Hyper::Server is able to either create and use its own core, or share
/// one with others.
enum ServerInternal {
Core(Core),
Handle(Handle)
}

/// An instance of the HTTP protocol, and implementation of tokio-proto's
/// `ServerProto` trait.
///
Expand All @@ -62,7 +69,7 @@ where B: Stream<Error=::Error>,
{
protocol: Http<B::Item>,
new_service: S,
core: Core,
internal: ServerInternal,
listener: TcpListener,
shutdown_timeout: Duration,
}
Expand Down Expand Up @@ -105,13 +112,33 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {

Ok(Server {
new_service: new_service,
core: core,
internal: ServerInternal::Core(core),
listener: listener,
protocol: self.clone(),
shutdown_timeout: Duration::new(1, 0),
})
}

/// Bind the provided `addr` and return a server that has
/// a shared `Core`.
///
/// This method, when provided a handle to a `Core`, allows
/// the ability to bind multiple servers to the same `Core`.
pub fn bind_handle<S, Bd>(&self, addr: &SocketAddr, new_service: S, handle: Handle) -> Server<S, Bd>
where S: NewService<Request = Request, Response = Response<Bd>, Error = ::Error> + 'static,
Bd: Stream<Item=B, Error=::Error>,
{
let listener = TcpListener::bind(addr, &handle).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably means bind_handle should return a Result, just like bind does, so that we don't panic.


Server {
new_service: new_service,
internal: ServerInternal::Handle(handle),
listener: listener,
protocol: self.clone(),
shutdown_timeout: Duration::new(1, 0),
}
}

/// Bind a `NewService` using types from the `http` crate.
///
/// See `Http::bind`.
Expand Down Expand Up @@ -389,7 +416,10 @@ impl<S, B> Server<S, B>
/// Returns a handle to the underlying event loop that this server will be
/// running on.
pub fn handle(&self) -> Handle {
self.core.handle()
match self.internal {
ServerInternal::Core(ref core) => core.handle().clone(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clone here isn't need, core.handle() already returns Handle.

ServerInternal::Handle(ref handle) => handle.clone(),
}
}

/// Configure the amount of time this server will wait for a "graceful
Expand Down Expand Up @@ -429,7 +459,13 @@ impl<S, B> Server<S, B>
pub fn run_until<F>(self, shutdown_signal: F) -> ::Result<()>
where F: Future<Item = (), Error = ()>,
{
let Server { protocol, new_service, mut core, listener, shutdown_timeout } = self;
let Server { protocol, new_service, internal, listener, shutdown_timeout } = self;

let mut core = match internal {
ServerInternal::Core(core) => core,
_ => panic!("Server does not own a core!"),
};

let handle = core.handle();

// Mini future to track the number of active services
Expand Down