Skip to content

Commit

Permalink
Pass tokio Handle into GothamService::new
Browse files Browse the repository at this point in the history
This lays the groundwork for addressing #78 - exposing the `Handle` to
Gotham applications.
  • Loading branch information
smangelsdorf committed Jan 9, 2018
1 parent 9dea4e0 commit 304bfba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
12 changes: 7 additions & 5 deletions src/clone_sockets.rs
Expand Up @@ -19,7 +19,7 @@ where
let (listener, addr) = super::tcp_listener(addr);

let protocol = Arc::new(Http::new());
let service = GothamService::new(new_handler);
let new_handler = Arc::new(new_handler);

info!(
target: "gotham::start",
Expand All @@ -31,24 +31,26 @@ where
for _ in 0..threads - 1 {
let listener = listener.try_clone().expect("unable to clone TCP listener");
let protocol = protocol.clone();
let service = service.clone();
thread::spawn(move || serve(listener, &addr, &protocol, &service));
let new_handler = new_handler.clone();
thread::spawn(move || serve(listener, &addr, &protocol, new_handler));
}

serve(listener, &addr, &protocol, &service);
serve(listener, &addr, &protocol, new_handler);
}

fn serve<NH>(
listener: TcpListener,
addr: &SocketAddr,
protocol: &Http,
new_service: &GothamService<NH>,
new_handler: Arc<NH>,
) where
NH: NewHandler + 'static,
{
let mut core = Core::new().expect("unable to spawn tokio reactor");
let handle = core.handle();

let new_service = GothamService::new(new_handler, handle.clone());

let listener = tokio_core::net::TcpListener::from_listener(listener, addr, &handle)
.expect("unable to convert TCP listener to tokio listener");

Expand Down
9 changes: 6 additions & 3 deletions src/router/builder/mod.rs
Expand Up @@ -286,10 +286,12 @@ mod tests {
use super::*;

use std::str::FromStr;
use std::sync::Arc;

use hyper::{Request, Response, StatusCode, Method, Uri};
use hyper::server::{NewService, Service};
use futures::{Future, Stream};
use tokio_core::reactor::Core;

use middleware::pipeline::new_pipeline;
use middleware::session::NewSessionMiddleware;
Expand Down Expand Up @@ -471,11 +473,12 @@ mod tests {
route.delegate("/delegated").to_router(delegated_router);
});

let new_service = GothamService::new(router);
let mut core = Core::new().unwrap();
let new_service = GothamService::new(Arc::new(router), core.handle());

let call = move |req| {
let mut call = move |req| {
let service = new_service.new_service().unwrap();
service.call(req).wait().unwrap()
core.run(service.call(req)).unwrap()
};

let response = call(Request::new(Method::Get, "/".parse().unwrap()));
Expand Down
8 changes: 5 additions & 3 deletions src/service/mod.rs
Expand Up @@ -8,6 +8,7 @@ use hyper;
use hyper::server::{NewService, Service};
use hyper::{Request, Response};
use futures::Future;
use tokio_core::reactor::Handle;

use handler::NewHandler;
use state::{State, request_id, set_request_id};
Expand All @@ -24,14 +25,15 @@ where
T: NewHandler + 'static,
{
t: Arc<T>,
handle: Handle,
}

impl<T> Clone for GothamService<T>
where
T: NewHandler + 'static,
{
fn clone(&self) -> Self {
GothamService { t: self.t.clone() }
GothamService { t: self.t.clone(), handle: self.handle.clone() }
}
}

Expand Down Expand Up @@ -107,8 +109,8 @@ where
/// GothamService::new(router);
/// # }
/// ```
pub fn new(t: T) -> GothamService<T> {
GothamService { t: Arc::new(t) }
pub fn new(t: Arc<T>, handle: Handle) -> GothamService<T> {
GothamService { t, handle }
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/test/mod.rs
Expand Up @@ -7,6 +7,7 @@ use std::cell::RefCell;
use std::net::{IpAddr, SocketAddr, TcpListener, TcpStream};
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::sync::Arc;

use futures::{future, Future, Stream};
use hyper::{self, Body, Method, Request, Response, Uri};
Expand Down Expand Up @@ -116,11 +117,13 @@ where
/// Sets the request timeout to `timeout` seconds and returns a new `TestServer`.
pub fn with_timeout(new_handler: NH, timeout: u64) -> Result<TestServer<NH>, io::Error> {
Core::new().map(|core| {
let handle = core.handle();

let data = TestServerData {
core: RefCell::new(core),
http: server::Http::new(),
timeout,
new_service: GothamService::new(new_handler),
new_service: GothamService::new(Arc::new(new_handler), handle),
};

TestServer {
Expand Down

0 comments on commit 304bfba

Please sign in to comment.