pub struct QuotaToken { /* private fields */ }Expand description
An unforgeable capability granting the right to consume a named resource.
Dropping the token releases the underlying lease back to the executor pool.
§Example
let token = QuotaToken::new("llm-tokens", 1000);
match token.reserve(500) {
Ok(reservation) => {
let used = call_llm_api();
reservation.commit(used);
}
Err(e) => eprintln!("quota unavailable: {e}"),
}Implementations§
Source§impl QuotaToken
impl QuotaToken
Sourcepub fn new(resource_name: &str, expected_use: u64) -> Self
pub fn new(resource_name: &str, expected_use: u64) -> Self
Request a quota capability for the given resource.
resource_name: the resource name as declared in the manifest.expected_use: expected units per reservation; used to derive the credit rate and max-credit for fair scheduling.
Sourcepub fn reserve(&self, amount: u64) -> Result<Reservation, FailedReservation>
pub fn reserve(&self, amount: u64) -> Result<Reservation, FailedReservation>
Reserve amount units from the local allocation.
Blocks internally until capacity is available or the resource’s
enforcement action fires. Returns a Reservation handle that
must be committed (or dropped) to release unused capacity.
Returns Err(FailedReservation) when the enforcement policy is reject.
For throttle / terminate policies the call suspends or terminates
the agent before returning.
Sourcepub fn split(&mut self, child_expected_use: u64) -> QuotaToken
pub fn split(&mut self, child_expected_use: u64) -> QuotaToken
Split off a child token with child_expected_use units.
- The parent’s expected-use is reduced by
child_expected_use. - Credits are divided proportionally between parent and child.
§Panics
Traps if child_expected_use exceeds the parent’s current expected-use.
Sourcepub fn merge(&mut self, other: QuotaToken)
pub fn merge(&mut self, other: QuotaToken)
Merge other into this token, combining expected-use and credits.
Both tokens must refer to the same resource (same resource-name and
environment). other is consumed.
§Panics
Traps if the tokens refer to different resources.