pub struct LimitCountPlugin {
count: u64,
window: Duration,
key_template: String,
group: Option<String>,
rejected_code: u16,
rejected_msg: Option<String>,
show_limit_quota_header: bool,
allow_degradation: bool,
store: Arc<dyn CounterStore>,
}Expand description
Enforces a per-key request count within a fixed time window.
On each request the resolved key is counted against a CounterStore
(currently only the in-memory local backend). Within the limit the
request passes through the success port; over the limit it is rejected
with rejected_code and a JSON {"error_msg": ...} body through the
error port (code RATE_LIMITED). When show_limit_quota_header is set
the X-RateLimit-Limit/-Remaining/-Reset headers are written onto
context.response in both cases.
Fields§
§count: u64Maximum number of requests allowed per window.
window: DurationLength of the fixed window.
key_template: StringKey template ($var interpolated); empty result falls back to the
client remote address.
group: Option<String>Optional counter-key prefix so multiple nodes share one counter.
rejected_code: u16Status returned when a request is rejected.
rejected_msg: Option<String>Optional custom message used in the rejection body.
show_limit_quota_header: boolWhether to emit the X-RateLimit-* quota headers.
allow_degradation: boolWhen true, a counter-backend error lets the request through instead of failing it.
store: Arc<dyn CounterStore>Resolved counter backend (from policy).
Implementations§
Source§impl LimitCountPlugin
impl LimitCountPlugin
Sourcepub fn from_config(
config: &HashMap<String, Value>,
resources: &Arc<PluginResources>,
) -> Result<Self, String>
pub fn from_config( config: &HashMap<String, Value>, resources: &Arc<PluginResources>, ) -> Result<Self, String>
Builds the plugin from node config.
Accepted keys:
count(integer > 0, required): requests allowed per window.time_window(integer > 0, required): window length in seconds.key(string, default"$remote_addr"): a$vartemplate resolved per request (e.g.$remote_addr,$consumer_name,$http_x_api_key). An empty resolved value falls back to the client remote address.policy(string, default"local"): counter backend. Onlylocalis available;redis/others are rejected at config load.group(string, optional): prefixes the counter key so multiple nodes share one counter.rejected_code(integer 200-599, default503): status for over-limit requests.rejected_msg(string, optional): message placed in the rejection body ({"error_msg": ...}).show_limit_quota_header(bool, defaulttrue): emit theX-RateLimit-*headers onto the response.allow_degradation(bool, defaultfalse): on a counter-backend error, allow the request through instead of failing it.
type: limit-count
config:
count: 100
time_window: 60
key: "$remote_addr"
policy: local
rejected_code: 429
show_limit_quota_header: trueSourcefn resolve_key(&self, ctx: &Context) -> String
fn resolve_key(&self, ctx: &Context) -> String
Resolves the per-request counter key: interpolates the key template
and, when it resolves to empty, falls back to the remote address
(matching APISIX). The group prefix, when set, is prepended so nodes
in the same group share one counter.
Sourcefn set_quota_headers(&self, ctx: &mut Context, remaining: u64, reset: Duration)
fn set_quota_headers(&self, ctx: &mut Context, remaining: u64, reset: Duration)
Writes the X-RateLimit-* quota headers onto the response.
Trait Implementations§
Source§impl Plugin for LimitCountPlugin
impl Plugin for LimitCountPlugin
Source§fn plugin_type(&self) -> &str
fn plugin_type(&self) -> &str
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,
Auto Trait Implementations§
impl Freeze for LimitCountPlugin
impl !RefUnwindSafe for LimitCountPlugin
impl Send for LimitCountPlugin
impl Sync for LimitCountPlugin
impl Unpin for LimitCountPlugin
impl UnsafeUnpin for LimitCountPlugin
impl !UnwindSafe for LimitCountPlugin
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
§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>
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>
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