-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
/// | ||
|
@@ -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, | ||
} | ||
|
@@ -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(); | ||
|
||
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`. | ||
|
@@ -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(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The clone here isn't need, |
||
ServerInternal::Handle(ref handle) => handle.clone(), | ||
} | ||
} | ||
|
||
/// Configure the amount of time this server will wait for a "graceful | ||
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 aResult
, just likebind
does, so that we don't panic.