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.
§Panics
Traps if this token has already been transferred (for example, sent to another agent through an RPC call or returned from a method). Split the token first if you need to both keep and send a capability.
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,
or if this token has already been transferred.
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, or if either token has already been transferred.
Trait Implementations§
Source§impl FromWire for QuotaToken
impl FromWire for QuotaToken
fn read_wire(reader: &mut WireReader, index: i32) -> Result<Self, WireError>
fn read_result_payload( reader: &mut WireReader, index: Option<i32>, ) -> Result<Self, WireError>
Source§impl IntoWire for QuotaToken
impl IntoWire for QuotaToken
fn preflight(&self, resources: &mut WirePreflight) -> Result<(), WireError>
fn write_wire(&self, writer: &mut WireWriter) -> Result<i32, WireError>
Source§const NEEDS_PREPARATION: bool = true
const NEEDS_PREPARATION: bool = true
prepare_wire is a no-op for every value of this type.
Resource preflight still runs regardless of this flag. Custom and derived
implementations prepare by default, including those with no stream schema.