tower_async/limit/policy/
mod.rs

1//! Limit policies for [`super::Limit`]
2//! define how requests are handled when the limit is reached
3//! for a given request.
4
5mod concurrent;
6pub use concurrent::{ConcurrentPolicy, LimitReached};
7
8/// The output of a limit policy.
9#[derive(Debug)]
10pub enum PolicyOutput<Guard, Error> {
11    /// The request is allowed to proceed,
12    /// and the guard is returned to release the limit when it is dropped,
13    /// which should be done after the request is completed.
14    Ready(Guard),
15    /// The request is not allowed to proceed, and should be aborted.
16    Abort(Error),
17    /// The request is not allowed to proceed, but should be retried.
18    Retry,
19}
20
21/// A limit policy is used to determine whether a request is allowed to proceed,
22/// and if not, how to handle it.
23pub trait Policy<Request> {
24    /// The guard type that is returned when the request is allowed to proceed.
25    ///
26    /// See [`PolicyOutput::Ready`].
27    type Guard;
28    /// The error type that is returned when the request is not allowed to proceed,
29    /// and should be aborted.
30    ///
31    /// See [`PolicyOutput::Abort`].
32    type Error;
33
34    /// Check whether the request is allowed to proceed.
35    ///
36    /// Optionally modify the request before it is passed to the inner service,
37    /// which can be used to add metadata to the request regarding how the request
38    /// was handled by this limit policy.
39    fn check(
40        &self,
41        request: &mut Request,
42    ) -> impl std::future::Future<Output = PolicyOutput<Self::Guard, Self::Error>>;
43}