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
//!
use super::Error;
use futures_core::ready;
use pin_project::{pin_project, pinned_drop};
use std::sync::Arc;
@ -29,12 +28,11 @@ impl<T> ResponseFuture<T> {
impl<F, T, E> Future for ResponseFuture<F>
where
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> {
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;
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;
@ -52,16 +52,16 @@ impl<T> ConcurrencyLimit<T> {
impl<S, Request> Service<Request> for ConcurrencyLimit<S>
where
S: Service<Request>,
S::Error: Into<Error>,
{
type Response = S::Response;
type Error = Error;
type Error = S::Error;
type Future = ResponseFuture<S::Future>;
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 {

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.
mod error;
pub mod future;
mod layer;
mod rate;
mod service;

View File

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