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, a Retry-After: 1 header, and error code RATE_LIMITED. 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
Unique identifier for the plugin type (e.g., “proxy-rewrite”, “upstream”).
Source§fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: Context,
_named_inputs: &'life1 HashMap<String, Value>,
) -> Pin<Box<dyn Future<Output = Result<PluginOutput, PluginExecutionError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: Context,
_named_inputs: &'life1 HashMap<String, Value>,
) -> Pin<Box<dyn Future<Output = Result<PluginOutput, PluginExecutionError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Executes the plugin logic against the request/response context. Read more
Auto Trait Implementations§
impl Freeze for RateLimitPlugin
impl !RefUnwindSafe for RateLimitPlugin
impl Send for RateLimitPlugin
impl Sync for RateLimitPlugin
impl Unpin for RateLimitPlugin
impl UnsafeUnpin for RateLimitPlugin
impl !UnwindSafe 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