pub struct RateLimitPlugin { /* private fields */ }Expand description
Rate limiting plugin. Limits requests per IP/user within a time window.
Implementations§
Source§impl RateLimitPlugin
impl RateLimitPlugin
pub fn new(max_requests: u32, window: Duration) -> Self
Sourcepub fn check_request(
&self,
user_id: Option<&str>,
peer_ip: &str,
) -> Result<(), PluginError>
pub fn check_request( &self, user_id: Option<&str>, peer_ip: &str, ) -> Result<(), PluginError>
Rate-limit by user id when present, otherwise by peer IP. Prefer
this over the Plugin trait’s on_request hook: that hook has no
access to peer IP and collapses every unauthenticated caller into
a single __anon__ bucket, which means one attacker can DoS the
entire anonymous client population.
Call from the HTTP layer where peer IP is available. Pass "" for
peer_ip if unknown — the fallback is the same shared __anon__
bucket as before (not worse than the old behavior).
Trait Implementations§
Source§impl Plugin for RateLimitPlugin
impl Plugin for RateLimitPlugin
Source§fn on_request(
&self,
_method: &str,
_path: &str,
auth: &AuthContext,
) -> Result<(), PluginError>
fn on_request( &self, _method: &str, _path: &str, auth: &AuthContext, ) -> Result<(), PluginError>
Called on every incoming request (middleware).
Source§fn on_request_with_meta(
&self,
_method: &str,
_path: &str,
auth: &AuthContext,
meta: &RequestMeta<'_>,
) -> Result<(), PluginError>
fn on_request_with_meta( &self, _method: &str, _path: &str, auth: &AuthContext, meta: &RequestMeta<'_>, ) -> Result<(), PluginError>
Richer variant of [
on_request] that also receives per-request
metadata (peer IP today; more fields may be added later). The
default implementation delegates to on_request so existing
plugins keep working without changes. Plugins that care about
IP — notably rate limiting — override this hook.Source§fn on_init(&self, _ctx: &PluginContext)
fn on_init(&self, _ctx: &PluginContext)
Called once when the plugin is registered.
Source§fn routes(&self) -> Vec<PluginRoute>
fn routes(&self) -> Vec<PluginRoute>
Custom API routes this plugin handles.
Source§fn before_insert(
&self,
_entity: &str,
_data: &mut Value,
_auth: &AuthContext,
) -> Result<(), PluginError>
fn before_insert( &self, _entity: &str, _data: &mut Value, _auth: &AuthContext, ) -> Result<(), PluginError>
Called before an entity insert. Return Err to reject.
Source§fn after_insert(
&self,
_entity: &str,
_id: &str,
_data: &Value,
_auth: &AuthContext,
)
fn after_insert( &self, _entity: &str, _id: &str, _data: &Value, _auth: &AuthContext, )
Called after a successful insert.
Source§fn before_update(
&self,
_entity: &str,
_id: &str,
_data: &mut Value,
_auth: &AuthContext,
) -> Result<(), PluginError>
fn before_update( &self, _entity: &str, _id: &str, _data: &mut Value, _auth: &AuthContext, ) -> Result<(), PluginError>
Called before an entity update. Return Err to reject.
Source§fn after_update(
&self,
_entity: &str,
_id: &str,
_data: &Value,
_auth: &AuthContext,
)
fn after_update( &self, _entity: &str, _id: &str, _data: &Value, _auth: &AuthContext, )
Called after a successful update.
Source§fn before_delete(
&self,
_entity: &str,
_id: &str,
_auth: &AuthContext,
) -> Result<(), PluginError>
fn before_delete( &self, _entity: &str, _id: &str, _auth: &AuthContext, ) -> Result<(), PluginError>
Called before an entity delete. Return Err to reject.
Source§fn after_delete(&self, _entity: &str, _id: &str, _auth: &AuthContext)
fn after_delete(&self, _entity: &str, _id: &str, _auth: &AuthContext)
Called after a successful delete.
Source§fn on_session_create(&self, _user_id: &str, _token: &str)
fn on_session_create(&self, _user_id: &str, _token: &str)
Called when a new session is created.
Source§fn entities(&self) -> Vec<ManifestEntity>
fn entities(&self) -> Vec<ManifestEntity>
Additional manifest entities this plugin contributes.
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§
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