Add buffer support

This commit is contained in:
Lucio Franco
2019-08-31 03:18:52 -04:00
parent 644cc310f7
commit 4000c42478
4 changed files with 26 additions and 18 deletions
+2
View File
@@ -25,6 +25,8 @@ hyper = { git = "https://github.com/hyperium/hyper" }
console = "0.7"
structopt = "0.2"
pretty_env_logger = "0.3"
tracing-fmt = "0.0.1-alpha.1"
tracing = "0.1"
[build-dependencies]
tonic-build = { path = "../tonic-build" }
+2 -1
View File
@@ -14,7 +14,8 @@ struct Opts {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
pretty_env_logger::init();
let sub = tracing_fmt::FmtSubscriber::builder().finish();
tracing::subscriber::set_global_default(sub).unwrap();
let matches = Opts::from_args();
+2 -1
View File
@@ -26,7 +26,8 @@ hyper = { git = "https://github.com/hyperium/hyper", optional = true}
# tower
tower-reconnect = { path = "../../tower/tower-reconnect", optional = true }
tower-buffer = { path = "../../tower/tower-buffer", optional = true }
[features]
default = ["transport"]
transport = ["hyper", "tower-reconnect"]
transport = ["hyper", "tower-reconnect", "tower-buffer"]
+20 -16
View File
@@ -11,19 +11,23 @@ use hyper::{Request, Response};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use tower_buffer::{future::ResponseFuture, Buffer};
use tower_service::Service;
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
// #[derive/(Clone)]
type Inner = Box<
dyn Service<
Request<BoxBody>,
Response = Response<hyper::Body>,
Error = crate::Error,
Future = BoxFuture<'static, Result<Response<hyper::Body>, crate::Error>>,
> + Send
+ 'static,
>;
#[derive(Clone)]
pub struct Client {
svc: Box<
dyn GrpcService<
BoxBody,
ResponseBody = hyper::Body,
Error = crate::Error,
Future = BoxFuture<'static, Result<Response<hyper::Body>, crate::Error>>,
> + Send
+ 'static,
>,
svc: Buffer<Inner, Request<BoxBody>>,
}
impl Client {
@@ -35,7 +39,9 @@ impl Client {
let svc = AddOrigin::new(svc, addr);
let svc = BoxService::new(svc);
Ok(Self { svc: Box::new(svc) })
let svc = Buffer::new(Box::new(svc) as Inner, 100);
Ok(Self { svc })
}
}
@@ -44,19 +50,17 @@ impl GrpcService<BoxBody> for Client {
type Error = super::Error;
type Future = MapErr<
BoxFuture<'static, Result<Response<Self::ResponseBody>, crate::Error>>,
ResponseFuture<BoxFuture<'static, Result<Response<Self::ResponseBody>, crate::Error>>>,
fn(crate::Error) -> super::Error,
>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.svc
.poll_ready(cx)
GrpcService::poll_ready(&mut self.svc, cx)
.map_err(|e| super::Error::from((super::ErrorKind::Client, e)))
}
fn call(&mut self, request: Request<BoxBody>) -> Self::Future {
self.svc
.call(request)
GrpcService::call(&mut self.svc, request)
.map_err(|e| super::Error::from((super::ErrorKind::Client, e)))
}
}