pub struct RateLimitPlugin {
requests_per_second: u64,
burst: u64,
key_source: KeySource,
buckets: Arc<DashMap<String, TokenBucket>>,
}Expand description
Enforces a per-client request rate using the token bucket algorithm.
Each client key gets its own bucket sized burst and refilled at
requests_per_second; the key is the remote address by default or the
value of a configured header (falling back to the remote address when the
header is absent). Over-limit requests are rejected with a 429 JSON
response and a Retry-After: 1 header, exiting through the limited
port. Does not write to context.message.
Fields§
§requests_per_second: u64Sustained refill rate: tokens added per second.
burst: u64Bucket capacity: maximum requests allowed in a burst.
key_source: KeySourceWhere the per-client key is taken from.
buckets: Arc<DashMap<String, TokenBucket>>Per-key token buckets, created lazily on first request.
Implementations§
Source§impl RateLimitPlugin
impl RateLimitPlugin
Sourcepub fn from_config(config: &HashMap<String, Value>) -> Result<Self, String>
pub fn from_config(config: &HashMap<String, Value>) -> Result<Self, String>
Builds the plugin from node config. Never fails; every key has a default.
Accepted keys:
requests_per_second(integer, default100): sustained rate per client key.burst(integer, default =requests_per_second): bucket capacity.key_from(string, default remote address): use"header:<name>"to key on a request header instead; any other value keys on the remote address.
type: rate-limit
config:
requests_per_second: 10
burst: 20
key_from: "header:x-api-key"Trait Implementations§
Source§impl Plugin for RateLimitPlugin
impl Plugin for RateLimitPlugin
Source§fn plugin_type(&self) -> &str
fn plugin_type(&self) -> &str
Source§fn execute<'life0, 'async_trait>(
&'life0 self,
ctx: Context,
) -> Pin<Box<dyn Future<Output = Result<PluginOutput, PluginExecutionError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn execute<'life0, 'async_trait>(
&'life0 self,
ctx: Context,
) -> Pin<Box<dyn Future<Output = Result<PluginOutput, PluginExecutionError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Executes the plugin logic against the request/response context. Read more
Source§fn reads_response_body(&self) -> bool
fn reads_response_body(&self) -> bool
Whether this configured instance reads
context.response.body. Read moreSource§fn cache_target(&self) -> Option<CacheTarget>
fn cache_target(&self) -> Option<CacheTarget>
The cache backend this node writes to, if it is a
proxy-cache half. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for RateLimitPlugin
impl !UnwindSafe for RateLimitPlugin
impl Freeze for RateLimitPlugin
impl Send for RateLimitPlugin
impl Sync for RateLimitPlugin
impl Unpin for RateLimitPlugin
impl UnsafeUnpin for RateLimitPlugin
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more