1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
//! Conditionally dispatch requests to the inner service based on the result of
//! a predicate.
//!
//! A predicate takes some request type and returns a `Result<Request, Error>`.
//! If the predicate returns [`Ok`], the inner service is called with the request
//! returned by the predicate — which may be the original request or a
//! modified one. If the predicate returns [`Err`], the request is rejected and
//! the inner service is not called.
//!
//! Predicates may either be synchronous (simple functions from a `Request` to
//! a [`Result`]) or asynchronous (functions returning [`Future`]s). Separate
//! traits, [`Predicate`] and [`AsyncPredicate`], represent these two types of
//! predicate. Note that when it is not necessary to await some other
//! asynchronous operation in the predicate, the synchronous predicate should be
//! preferred, as it introduces less overhead.
//!
//! The predicate traits are implemented for closures and function pointers.
//! However, users may also implement them for other types, such as when the
//! predicate requires some state carried between requests. For example,
//! [`Predicate`] could be implemented for a type that rejects a fixed set of
//! requests by checking if they are contained by a a [`HashSet`] or other
//! collection.
//!
//! [`Future`]: std::future::Future
//! [`HashSet`]: std::collections::HashSet
pub mod future;
mod layer;
mod predicate;
pub use self::{
layer::{AsyncFilterLayer, FilterLayer},
predicate::{AsyncPredicate, Predicate},
};
use self::future::{AsyncResponseFuture, ResponseFuture};
use crate::BoxError;
use futures_util::{future::Either, TryFutureExt};
use std::task::{Context, Poll};
use tower_service::Service;
/// Conditionally dispatch requests to the inner service based on a [predicate].
///
/// [predicate]: Predicate
#[derive(Clone, Debug)]
pub struct Filter<T, U> {
inner: T,
predicate: U,
}
/// Conditionally dispatch requests to the inner service based on an
/// [asynchronous predicate].
///
/// [asynchronous predicate]: AsyncPredicate
#[derive(Clone, Debug)]
pub struct AsyncFilter<T, U> {
inner: T,
predicate: U,
}
// ==== impl Filter ====
impl<T, U> Filter<T, U> {
/// Returns a new [`Filter`] service wrapping `inner`.
pub fn new(inner: T, predicate: U) -> Self {
Self { inner, predicate }
}
/// Returns a new [`Layer`] that wraps services with a [`Filter`] service
/// with the given [`Predicate`].
///
/// [`Layer`]: crate::Layer
pub fn layer(predicate: U) -> FilterLayer<U> {
FilterLayer::new(predicate)
}
/// Check a `Request` value against this filter's predicate.
pub fn check<R>(&mut self, request: R) -> Result<U::Request, BoxError>
where
U: Predicate<R>,
{
self.predicate.check(request)
}
/// Get a reference to the inner service
pub fn get_ref(&self) -> &T {
&self.inner
}
/// Get a mutable reference to the inner service
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
/// Consume `self`, returning the inner service
pub fn into_inner(self) -> T {
self.inner
}
}
impl<T, U, Request> Service<Request> for Filter<T, U>
where
U: Predicate<Request>,
T: Service<U::Request>,
T::Error: Into<BoxError>,
{
type Response = T::Response;
type Error = BoxError;
type Future = ResponseFuture<T::Response, T::Future>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx).map_err(Into::into)
}
fn call(&mut self, request: Request) -> Self::Future {
ResponseFuture::new(match self.predicate.check(request) {
Ok(request) => Either::Right(self.inner.call(request).err_into()),
Err(e) => Either::Left(futures_util::future::ready(Err(e))),
})
}
}
// ==== impl AsyncFilter ====
impl<T, U> AsyncFilter<T, U> {
/// Returns a new [`AsyncFilter`] service wrapping `inner`.
pub fn new(inner: T, predicate: U) -> Self {
Self { inner, predicate }
}
/// Returns a new [`Layer`] that wraps services with an [`AsyncFilter`]
/// service with the given [`AsyncPredicate`].
///
/// [`Layer`]: crate::Layer
pub fn layer(predicate: U) -> FilterLayer<U> {
FilterLayer::new(predicate)
}
/// Check a `Request` value against this filter's predicate.
pub async fn check<R>(&mut self, request: R) -> Result<U::Request, BoxError>
where
U: AsyncPredicate<R>,
{
self.predicate.check(request).await
}
/// Get a reference to the inner service
pub fn get_ref(&self) -> &T {
&self.inner
}
/// Get a mutable reference to the inner service
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
/// Consume `self`, returning the inner service
pub fn into_inner(self) -> T {
self.inner
}
}
impl<T, U, Request> Service<Request> for AsyncFilter<T, U>
where
U: AsyncPredicate<Request>,
T: Service<U::Request> + Clone,
T::Error: Into<BoxError>,
{
type Response = T::Response;
type Error = BoxError;
type Future = AsyncResponseFuture<U, T, Request>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx).map_err(Into::into)
}
fn call(&mut self, request: Request) -> Self::Future {
use std::mem;
let inner = self.inner.clone();
// In case the inner service has state that's driven to readiness and
// not tracked by clones (such as `Buffer`), pass the version we have
// already called `poll_ready` on into the future, and leave its clone
// behind.
let inner = mem::replace(&mut self.inner, inner);
// Check the request
let check = self.predicate.check(request);
AsyncResponseFuture::new(check, inner)
}
}