mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-01-22 11:05:22 +01:00
format with rustfmt 0.8.3 (#148)
This commit is contained in:
@@ -2,17 +2,17 @@ use {bincode, future, num_cpus};
|
||||
use future::server::{Response, Shutdown};
|
||||
use futures::{Future, future as futures};
|
||||
use futures::sync::oneshot;
|
||||
#[cfg(feature = "tls")]
|
||||
use native_tls_inner::TlsAcceptor;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
use std::usize;
|
||||
use thread_pool::{self, Sender, Task, ThreadPool};
|
||||
use tokio_core::reactor;
|
||||
use tokio_service::{NewService, Service};
|
||||
#[cfg(feature = "tls")]
|
||||
use native_tls_inner::TlsAcceptor;
|
||||
|
||||
/// Additional options to configure how the server operates.
|
||||
#[derive(Debug)]
|
||||
@@ -91,16 +91,19 @@ impl fmt::Debug for Handle {
|
||||
const CORE: &'static &'static str = &"Core { .. }";
|
||||
const SERVER: &'static &'static str = &"Box<Future<Item = (), Error = ()>>";
|
||||
|
||||
f.debug_struct("Handle").field("reactor", CORE)
|
||||
.field("handle", &self.handle)
|
||||
.field("server", SERVER)
|
||||
.finish()
|
||||
f.debug_struct("Handle")
|
||||
.field("reactor", CORE)
|
||||
.field("handle", &self.handle)
|
||||
.field("server", SERVER)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn listen<S, Req, Resp, E>(new_service: S, addr: SocketAddr, options: Options)
|
||||
-> io::Result<Handle>
|
||||
pub fn listen<S, Req, Resp, E>(new_service: S,
|
||||
addr: SocketAddr,
|
||||
options: Options)
|
||||
-> io::Result<Handle>
|
||||
where S: NewService<Request = Result<Req, bincode::Error>,
|
||||
Response = Response<Resp, E>,
|
||||
Error = io::Error> + 'static,
|
||||
@@ -117,27 +120,33 @@ pub fn listen<S, Req, Resp, E>(new_service: S, addr: SocketAddr, options: Option
|
||||
future::server::listen(new_service, addr, &reactor.handle(), options.opts)?;
|
||||
let server = Box::new(server);
|
||||
Ok(Handle {
|
||||
reactor: reactor,
|
||||
handle: handle,
|
||||
server: server,
|
||||
})
|
||||
reactor: reactor,
|
||||
handle: handle,
|
||||
server: server,
|
||||
})
|
||||
}
|
||||
|
||||
/// A service that uses a thread pool.
|
||||
struct NewThreadService<S> where S: NewService {
|
||||
struct NewThreadService<S>
|
||||
where S: NewService
|
||||
{
|
||||
new_service: S,
|
||||
sender: Sender<ServiceTask<<S::Instance as Service>::Future>>,
|
||||
_pool: ThreadPool<ServiceTask<<S::Instance as Service>::Future>>,
|
||||
}
|
||||
|
||||
/// A service that runs by executing request handlers in a thread pool.
|
||||
struct ThreadService<S> where S: Service {
|
||||
struct ThreadService<S>
|
||||
where S: Service
|
||||
{
|
||||
service: S,
|
||||
sender: Sender<ServiceTask<S::Future>>,
|
||||
}
|
||||
|
||||
/// A task that handles a single request.
|
||||
struct ServiceTask<F> where F: Future {
|
||||
struct ServiceTask<F>
|
||||
where F: Future
|
||||
{
|
||||
future: F,
|
||||
tx: oneshot::Sender<Result<F::Item, F::Error>>,
|
||||
}
|
||||
@@ -146,12 +155,16 @@ impl<S> NewThreadService<S>
|
||||
where S: NewService,
|
||||
<S::Instance as Service>::Future: Send + 'static,
|
||||
S::Response: Send,
|
||||
S::Error: Send,
|
||||
S::Error: Send
|
||||
{
|
||||
/// Create a NewThreadService by wrapping another service.
|
||||
fn new(new_service: S, pool: thread_pool::Builder) -> Self {
|
||||
let (sender, _pool) = pool.build();
|
||||
NewThreadService { new_service, sender, _pool }
|
||||
NewThreadService {
|
||||
new_service,
|
||||
sender,
|
||||
_pool,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +172,7 @@ impl<S> NewService for NewThreadService<S>
|
||||
where S: NewService,
|
||||
<S::Instance as Service>::Future: Send + 'static,
|
||||
S::Response: Send,
|
||||
S::Error: Send,
|
||||
S::Error: Send
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = S::Response;
|
||||
@@ -168,16 +181,16 @@ impl<S> NewService for NewThreadService<S>
|
||||
|
||||
fn new_service(&self) -> io::Result<Self::Instance> {
|
||||
Ok(ThreadService {
|
||||
service: self.new_service.new_service()?,
|
||||
sender: self.sender.clone(),
|
||||
})
|
||||
service: self.new_service.new_service()?,
|
||||
sender: self.sender.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> Task for ServiceTask<F>
|
||||
where F: Future + Send + 'static,
|
||||
F::Item: Send,
|
||||
F::Error: Send,
|
||||
F::Error: Send
|
||||
{
|
||||
fn run(self) {
|
||||
// Don't care if sending fails. It just means the request is no longer
|
||||
@@ -190,25 +203,26 @@ impl<S> Service for ThreadService<S>
|
||||
where S: Service,
|
||||
S::Future: Send + 'static,
|
||||
S::Response: Send,
|
||||
S::Error: Send,
|
||||
S::Error: Send
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future =
|
||||
futures::AndThen<
|
||||
futures::MapErr<
|
||||
oneshot::Receiver<Result<Self::Response, Self::Error>>,
|
||||
fn(oneshot::Canceled) -> Self::Error>,
|
||||
Result<Self::Response, Self::Error>,
|
||||
fn(Result<Self::Response, Self::Error>) -> Result<Self::Response, Self::Error>>;
|
||||
type Future = futures::AndThen<futures::MapErr<oneshot::Receiver<Result<Self::Response,
|
||||
Self::Error>>,
|
||||
fn(oneshot::Canceled) -> Self::Error>,
|
||||
Result<Self::Response, Self::Error>,
|
||||
fn(Result<Self::Response, Self::Error>)
|
||||
-> Result<Self::Response, Self::Error>>;
|
||||
|
||||
fn call(&self, request: Self::Request) -> Self::Future {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
self.sender.send(ServiceTask {
|
||||
future: self.service.call(request),
|
||||
tx: tx,
|
||||
}).unwrap();
|
||||
self.sender
|
||||
.send(ServiceTask {
|
||||
future: self.service.call(request),
|
||||
tx: tx,
|
||||
})
|
||||
.unwrap();
|
||||
rx.map_err(unreachable as _).and_then(ident)
|
||||
}
|
||||
}
|
||||
@@ -222,4 +236,3 @@ fn unreachable<T, U>(t: T) -> U
|
||||
fn ident<T>(t: T) -> T {
|
||||
t
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user