Expect the poll_acquire error, not return (#362)

* Expect the poll_acquire error, not return

* Remove Error in tower-limit
This commit is contained in:
Pen Tree 2019-11-01 02:06:04 +08:00 committed by Lucio Franco
parent fac5c361a4
commit 52dbdda23d
7 changed files with 13 additions and 62 deletions

View File

@ -1,6 +1,5 @@
//! Future types //! Future types
//! //!
use super::Error;
use futures_core::ready; use futures_core::ready;
use pin_project::{pin_project, pinned_drop}; use pin_project::{pin_project, pinned_drop};
use std::sync::Arc; use std::sync::Arc;
@ -29,12 +28,11 @@ impl<T> ResponseFuture<T> {
impl<F, T, E> Future for ResponseFuture<F> impl<F, T, E> Future for ResponseFuture<F>
where where
F: Future<Output = Result<T, E>>, F: Future<Output = Result<T, E>>,
E: Into<Error>,
{ {
type Output = Result<T, Error>; type Output = Result<T, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(ready!(self.project().inner.poll(cx)).map_err(Into::into)) Poll::Ready(ready!(self.project().inner.poll(cx)))
} }
} }

View File

@ -5,5 +5,3 @@ mod layer;
mod service; mod service;
pub use self::{layer::ConcurrencyLimitLayer, service::ConcurrencyLimit}; pub use self::{layer::ConcurrencyLimitLayer, service::ConcurrencyLimit};
type Error = Box<dyn std::error::Error + Send + Sync>;

View File

@ -1,4 +1,4 @@
use super::{future::ResponseFuture, Error}; use super::future::ResponseFuture;
use tower_service::Service; use tower_service::Service;
@ -52,16 +52,16 @@ impl<T> ConcurrencyLimit<T> {
impl<S, Request> Service<Request> for ConcurrencyLimit<S> impl<S, Request> Service<Request> for ConcurrencyLimit<S>
where where
S: Service<Request>, S: Service<Request>,
S::Error: Into<Error>,
{ {
type Response = S::Response; type Response = S::Response;
type Error = Error; type Error = S::Error;
type Future = ResponseFuture<S::Future>; type Future = ResponseFuture<S::Future>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
ready!(self.limit.permit.poll_acquire(cx, &self.limit.semaphore))?; ready!(self.limit.permit.poll_acquire(cx, &self.limit.semaphore))
.expect("poll_acquire after semaphore closed ");
Poll::Ready(ready!(self.inner.poll_ready(cx)).map_err(Into::into)) Poll::Ready(ready!(self.inner.poll_ready(cx)))
} }
fn call(&mut self, request: Request) -> Self::Future { fn call(&mut self, request: Request) -> Self::Future {

View File

@ -1,3 +0,0 @@
use std::error;
pub(crate) type Error = Box<dyn error::Error + Send + Sync>;

View File

@ -1,36 +0,0 @@
//! Future types
use super::error::Error;
use futures_core::ready;
use pin_project::pin_project;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
/// Future for the `RateLimit` service.
#[pin_project]
#[derive(Debug)]
pub struct ResponseFuture<T> {
#[pin]
inner: T,
}
impl<T> ResponseFuture<T> {
pub(crate) fn new(inner: T) -> ResponseFuture<T> {
ResponseFuture { inner }
}
}
impl<F, T, E> Future for ResponseFuture<F>
where
F: Future<Output = Result<T, E>>,
Error: From<E>,
{
type Output = Result<T, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Poll::Ready(Ok(ready!(self.project().inner.poll(cx))?))
}
}

View File

@ -1,7 +1,5 @@
//! Limit the rate at which requests are processed. //! Limit the rate at which requests are processed.
mod error;
pub mod future;
mod layer; mod layer;
mod rate; mod rate;
mod service; mod service;

View File

@ -1,4 +1,4 @@
use super::{error::Error, future::ResponseFuture, Rate}; use super::Rate;
use futures_core::ready; use futures_core::ready;
use std::{ use std::{
future::Future, future::Future,
@ -60,17 +60,14 @@ impl<T> RateLimit<T> {
impl<S, Request> Service<Request> for RateLimit<S> impl<S, Request> Service<Request> for RateLimit<S>
where where
S: Service<Request>, S: Service<Request>,
Error: From<S::Error>,
{ {
type Response = S::Response; type Response = S::Response;
type Error = Error; type Error = S::Error;
type Future = ResponseFuture<S::Future>; type Future = S::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
match self.state { match self.state {
State::Ready { .. } => { State::Ready { .. } => return Poll::Ready(ready!(self.inner.poll_ready(cx))),
return Poll::Ready(ready!(self.inner.poll_ready(cx)).map_err(Error::from))
}
State::Limited(ref mut sleep) => { State::Limited(ref mut sleep) => {
ready!(Pin::new(sleep).poll(cx)); ready!(Pin::new(sleep).poll(cx));
} }
@ -81,7 +78,7 @@ where
rem: self.rate.num(), rem: self.rate.num(),
}; };
Poll::Ready(ready!(self.inner.poll_ready(cx)).map_err(Error::from)) Poll::Ready(ready!(self.inner.poll_ready(cx)))
} }
fn call(&mut self, request: Request) -> Self::Future { fn call(&mut self, request: Request) -> Self::Future {
@ -107,8 +104,7 @@ where
} }
// Call the inner future // Call the inner future
let inner = self.inner.call(request); self.inner.call(request)
ResponseFuture::new(inner)
} }
State::Limited(..) => panic!("service not ready; poll_ready must be called first"), State::Limited(..) => panic!("service not ready; poll_ready must be called first"),
} }