From 52dbdda23d17f6329be2bc5e4d918fcd3d6f69e5 Mon Sep 17 00:00:00 2001 From: Pen Tree Date: Fri, 1 Nov 2019 02:06:04 +0800 Subject: [PATCH] Expect the poll_acquire error, not return (#362) * Expect the poll_acquire error, not return * Remove Error in tower-limit --- tower-limit/src/concurrency/future.rs | 6 ++--- tower-limit/src/concurrency/mod.rs | 2 -- tower-limit/src/concurrency/service.rs | 10 +++---- tower-limit/src/rate/error.rs | 3 --- tower-limit/src/rate/future.rs | 36 -------------------------- tower-limit/src/rate/mod.rs | 2 -- tower-limit/src/rate/service.rs | 16 +++++------- 7 files changed, 13 insertions(+), 62 deletions(-) delete mode 100644 tower-limit/src/rate/error.rs delete mode 100644 tower-limit/src/rate/future.rs diff --git a/tower-limit/src/concurrency/future.rs b/tower-limit/src/concurrency/future.rs index 171947e0..768eaedc 100644 --- a/tower-limit/src/concurrency/future.rs +++ b/tower-limit/src/concurrency/future.rs @@ -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 ResponseFuture { impl Future for ResponseFuture where F: Future>, - E: Into, { - type Output = Result; + type Output = Result; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Poll::Ready(ready!(self.project().inner.poll(cx)).map_err(Into::into)) + Poll::Ready(ready!(self.project().inner.poll(cx))) } } diff --git a/tower-limit/src/concurrency/mod.rs b/tower-limit/src/concurrency/mod.rs index 467514eb..a1eba7e0 100644 --- a/tower-limit/src/concurrency/mod.rs +++ b/tower-limit/src/concurrency/mod.rs @@ -5,5 +5,3 @@ mod layer; mod service; pub use self::{layer::ConcurrencyLimitLayer, service::ConcurrencyLimit}; - -type Error = Box; diff --git a/tower-limit/src/concurrency/service.rs b/tower-limit/src/concurrency/service.rs index 83591d83..68d4e758 100644 --- a/tower-limit/src/concurrency/service.rs +++ b/tower-limit/src/concurrency/service.rs @@ -1,4 +1,4 @@ -use super::{future::ResponseFuture, Error}; +use super::future::ResponseFuture; use tower_service::Service; @@ -52,16 +52,16 @@ impl ConcurrencyLimit { impl Service for ConcurrencyLimit where S: Service, - S::Error: Into, { type Response = S::Response; - type Error = Error; + type Error = S::Error; type Future = ResponseFuture; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - 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 { diff --git a/tower-limit/src/rate/error.rs b/tower-limit/src/rate/error.rs deleted file mode 100644 index 712f34dd..00000000 --- a/tower-limit/src/rate/error.rs +++ /dev/null @@ -1,3 +0,0 @@ -use std::error; - -pub(crate) type Error = Box; diff --git a/tower-limit/src/rate/future.rs b/tower-limit/src/rate/future.rs deleted file mode 100644 index 9810cf66..00000000 --- a/tower-limit/src/rate/future.rs +++ /dev/null @@ -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 { - #[pin] - inner: T, -} - -impl ResponseFuture { - pub(crate) fn new(inner: T) -> ResponseFuture { - ResponseFuture { inner } - } -} - -impl Future for ResponseFuture -where - F: Future>, - Error: From, -{ - type Output = Result; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Poll::Ready(Ok(ready!(self.project().inner.poll(cx))?)) - } -} diff --git a/tower-limit/src/rate/mod.rs b/tower-limit/src/rate/mod.rs index e87bd2f3..e209b6f5 100644 --- a/tower-limit/src/rate/mod.rs +++ b/tower-limit/src/rate/mod.rs @@ -1,7 +1,5 @@ //! Limit the rate at which requests are processed. -mod error; -pub mod future; mod layer; mod rate; mod service; diff --git a/tower-limit/src/rate/service.rs b/tower-limit/src/rate/service.rs index 6b41779f..bab787e2 100644 --- a/tower-limit/src/rate/service.rs +++ b/tower-limit/src/rate/service.rs @@ -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 RateLimit { impl Service for RateLimit where S: Service, - Error: From, { type Response = S::Response; - type Error = Error; - type Future = ResponseFuture; + type Error = S::Error; + type Future = S::Future; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { 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"), }