parent
b42a365828
commit
0f58e50c3d
|
@ -1,4 +1,4 @@
|
|||
mod chain;
|
||||
mod identity;
|
||||
mod stack;
|
||||
|
||||
pub use self::{chain::Chain, identity::Identity};
|
||||
pub use self::{identity::Identity, stack::Stack};
|
||||
|
|
|
@ -5,21 +5,21 @@ use tower_service::Service;
|
|||
///
|
||||
/// This type is produced by `Layer::chain`.
|
||||
#[derive(Debug)]
|
||||
pub struct Chain<Inner, Outer> {
|
||||
pub struct Stack<Inner, Outer> {
|
||||
inner: Inner,
|
||||
outer: Outer,
|
||||
}
|
||||
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
impl<Inner, Outer> Chain<Inner, Outer> {
|
||||
/// Create a new `Chain`.
|
||||
impl<Inner, Outer> Stack<Inner, Outer> {
|
||||
/// Create a new `Stack`.
|
||||
pub fn new(inner: Inner, outer: Outer) -> Self {
|
||||
Chain { inner, outer }
|
||||
Stack { inner, outer }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, Request, Inner, Outer> Layer<S, Request> for Chain<Inner, Outer>
|
||||
impl<S, Request, Inner, Outer> Layer<S, Request> for Stack<Inner, Outer>
|
||||
where
|
||||
S: Service<Request>,
|
||||
Inner: Layer<S, Request>,
|
|
@ -15,7 +15,7 @@ use crate::{
|
|||
use tower_layer::Layer;
|
||||
use tower_service::Service;
|
||||
use tower_util::{
|
||||
layer::{Chain, Identity},
|
||||
layer::{Identity, Stack},
|
||||
MakeService,
|
||||
};
|
||||
|
||||
|
@ -228,14 +228,14 @@ impl ServiceBuilder<Identity> {
|
|||
|
||||
impl<L> ServiceBuilder<L> {
|
||||
/// Layer a new layer `T` onto the `ServiceBuilder`.
|
||||
pub fn layer<T>(self, layer: T) -> ServiceBuilder<Chain<T, L>> {
|
||||
pub fn layer<T>(self, layer: T) -> ServiceBuilder<Stack<T, L>> {
|
||||
ServiceBuilder {
|
||||
layer: Chain::new(layer, self.layer),
|
||||
layer: Stack::new(layer, self.layer),
|
||||
}
|
||||
}
|
||||
|
||||
/// Buffer requests when when the next layer is out of capacity.
|
||||
pub fn buffer(self, bound: usize) -> ServiceBuilder<Chain<BufferLayer, L>> {
|
||||
pub fn buffer(self, bound: usize) -> ServiceBuilder<Stack<BufferLayer, L>> {
|
||||
self.layer(BufferLayer::new(bound))
|
||||
}
|
||||
|
||||
|
@ -244,7 +244,7 @@ impl<L> ServiceBuilder<L> {
|
|||
/// A request is in-flight from the time the request is received until the
|
||||
/// response future completes. This includes the time spent in the next
|
||||
/// layers.
|
||||
pub fn concurrency_limit(self, max: usize) -> ServiceBuilder<Chain<ConcurrencyLimitLayer, L>> {
|
||||
pub fn concurrency_limit(self, max: usize) -> ServiceBuilder<Stack<ConcurrencyLimitLayer, L>> {
|
||||
self.layer(ConcurrencyLimitLayer::new(max))
|
||||
}
|
||||
|
||||
|
@ -256,12 +256,12 @@ impl<L> ServiceBuilder<L> {
|
|||
///
|
||||
/// `load_shed` immediately responds with an error when the next layer is
|
||||
/// out of capacity.
|
||||
pub fn load_shed(self) -> ServiceBuilder<Chain<LoadShedLayer, L>> {
|
||||
pub fn load_shed(self) -> ServiceBuilder<Stack<LoadShedLayer, L>> {
|
||||
self.layer(LoadShedLayer::new())
|
||||
}
|
||||
|
||||
/// Limit requests to at most `num` per the given duration
|
||||
pub fn rate_limit(self, num: u64, per: Duration) -> ServiceBuilder<Chain<RateLimitLayer, L>> {
|
||||
pub fn rate_limit(self, num: u64, per: Duration) -> ServiceBuilder<Stack<RateLimitLayer, L>> {
|
||||
self.layer(RateLimitLayer::new(num, per))
|
||||
}
|
||||
|
||||
|
@ -270,7 +270,7 @@ impl<L> ServiceBuilder<L> {
|
|||
/// `policy` must implement [`Policy`].
|
||||
///
|
||||
/// [`Policy`]: ../retry/trait.Policy.html
|
||||
pub fn retry<P>(self, policy: P) -> ServiceBuilder<Chain<RetryLayer<P>, L>> {
|
||||
pub fn retry<P>(self, policy: P) -> ServiceBuilder<Stack<RetryLayer<P>, L>> {
|
||||
self.layer(RetryLayer::new(policy))
|
||||
}
|
||||
|
||||
|
@ -278,7 +278,7 @@ impl<L> ServiceBuilder<L> {
|
|||
///
|
||||
/// If the next layer takes more than `timeout` to respond to a request,
|
||||
/// processing is terminated and an error is returned.
|
||||
pub fn timeout(self, timeout: Duration) -> ServiceBuilder<Chain<TimeoutLayer, L>> {
|
||||
pub fn timeout(self, timeout: Duration) -> ServiceBuilder<Stack<TimeoutLayer, L>> {
|
||||
self.layer(TimeoutLayer::new(timeout))
|
||||
}
|
||||
|
||||
|
|
|
@ -4,23 +4,5 @@ pub use tower_layer::Layer;
|
|||
|
||||
/// `util` exports an Identity Layer and Chain, a mechanism for chaining them.
|
||||
pub mod util {
|
||||
pub use tower_util::layer::{Chain, Identity};
|
||||
pub use tower_util::layer::{Identity, Stack};
|
||||
}
|
||||
|
||||
/// An extension trait for `Layer`'s that provides a variety of convenient
|
||||
/// adapters.
|
||||
pub trait LayerExt<S, Request>: Layer<S, Request> {
|
||||
/// Return a new `Layer` instance that applies both `self` and
|
||||
/// `middleware` to services being wrapped.
|
||||
///
|
||||
/// This defines a middleware stack.
|
||||
fn chain<T>(self, middleware: T) -> util::Chain<Self, T>
|
||||
where
|
||||
T: Layer<Self::Service, Request>,
|
||||
Self: Sized,
|
||||
{
|
||||
util::Chain::new(self, middleware)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, Request> LayerExt<S, Request> for T where T: Layer<S, Request> {}
|
||||
|
|
Loading…
Reference in New Issue