pub struct SessionBudgetTracker { /* private fields */ }Expand description
Per-session spending limits and accumulated spend.
Allows callers to cap the total cost a single session ID may accrue and to reject new requests once the cap is breached.
Thread-safe: the inner map is protected by a Mutex.
§Panics
Methods on this type never panic (the mutex is never poisoned by user code in normal operation; if it were the method would return an error rather than unwrapping).
Implementations§
Source§impl SessionBudgetTracker
impl SessionBudgetTracker
Sourcepub fn record_spend(
&self,
session_id: &str,
cost_usd: f64,
) -> Result<(), OrchestratorError>
pub fn record_spend( &self, session_id: &str, cost_usd: f64, ) -> Result<(), OrchestratorError>
Record additional spend for a session and check whether the budget is exceeded.
If no limit has been registered for the session, the spend is silently accepted (no cap).
§Arguments
session_id— The session incurring the cost.cost_usd— The amount to add to the session’s accumulated spend.
§Returns
Ok(())— within budget (or no limit registered).Err(OrchestratorError::BudgetExceeded)— the session’s limit would be exceeded by this request.
§Panics
This function never panics.
Sourcepub fn spent(&self, session_id: &str) -> Option<f64>
pub fn spent(&self, session_id: &str) -> Option<f64>
Return how much has been spent for a session.
Returns None if the session is not registered.
§Panics
This function never panics.
Sourcepub fn limit(&self, session_id: &str) -> Option<f64>
pub fn limit(&self, session_id: &str) -> Option<f64>
Return the configured limit for a session.
Returns None if the session is not registered.
§Panics
This function never panics.
Sourcepub fn reset_session(&self, session_id: &str)
pub fn reset_session(&self, session_id: &str)
Reset the accumulated spend for a session to zero without removing the limit.
§Panics
This function never panics.
Trait Implementations§
Source§impl Debug for SessionBudgetTracker
impl Debug for SessionBudgetTracker
Source§impl Default for SessionBudgetTracker
impl Default for SessionBudgetTracker
Source§fn default() -> SessionBudgetTracker
fn default() -> SessionBudgetTracker
Auto Trait Implementations§
impl !Freeze for SessionBudgetTracker
impl RefUnwindSafe for SessionBudgetTracker
impl Send for SessionBudgetTracker
impl Sync for SessionBudgetTracker
impl Unpin for SessionBudgetTracker
impl UnsafeUnpin for SessionBudgetTracker
impl UnwindSafe for SessionBudgetTracker
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request