pub struct RateLimitState { /* private fields */ }Expand description
Per-identity limits. Cheap to clone (all fields are shared / atomic).
Implementations§
Source§impl RateLimitState
impl RateLimitState
Sourcepub fn from_entry(
bw: Option<RatePer>,
quota: Option<RatePer>,
bw_partitions: usize,
) -> Self
pub fn from_entry( bw: Option<RatePer>, quota: Option<RatePer>, bw_partitions: usize, ) -> Self
Build a fresh state from an identity’s authorized_keys
entry. bw_partitions is the number of worker threads the
server is running (§9.2 per-core split); passing 1
preserves the pre-§9.2 single-bucket behaviour.
Sourcepub fn from_entry_with_metrics(
bw: Option<RatePer>,
quota: Option<RatePer>,
bw_partitions: usize,
metrics: Option<Arc<ServerMetrics>>,
) -> Self
pub fn from_entry_with_metrics( bw: Option<RatePer>, quota: Option<RatePer>, bw_partitions: usize, metrics: Option<Arc<ServerMetrics>>, ) -> Self
Same as Self::from_entry but plumbs the process-wide
metrics bag so quota breaches show up on /admin/v1/metrics.
Sourcepub fn attach_ctrl(&self, tx: UnboundedSender<ServerFrame>)
pub fn attach_ctrl(&self, tx: UnboundedSender<ServerFrame>)
Install a fresh control-stream sender. Called at session start, replacing any leftover sender from a prior session for the same identity.
Sourcepub fn detach_ctrl(&self)
pub fn detach_ctrl(&self)
Detach the current control-stream sender. Called at session end.
Sourcepub async fn throttle(&self, n: u32)
pub async fn throttle(&self, n: u32)
Await bandwidth-bucket tokens for n bytes, blocking as needed.
If no bw= cap is set this is a no-op. On a multi-worker
runtime the current thread’s current_worker_id picks
which per-core partition to spend tokens against (§9.2).
Sourcepub fn charge(&self, tunnel_id: TunnelId, n: u64) -> Charge
pub fn charge(&self, tunnel_id: TunnelId, n: u64) -> Charge
Account n bytes against the period quota. Rolls the counter
over automatically at the reset deadline. On breach a
RateLimit frame is queued for the control stream and
Charge::QuotaExceeded is returned.
Sourcepub fn quota_used_snapshot(&self) -> u64
pub fn quota_used_snapshot(&self) -> u64
Bytes currently counted against the period quota. Used by the
§16.4 admin API’s /metrics endpoint.
Sourcepub fn retry_after_ms(&self) -> u32
pub fn retry_after_ms(&self) -> u32
Millis until the current quota period resets. Clamped to
u32::MAX to fit RateLimit.retry_after_ms.