pub struct Budget { /* private fields */ }Expand description
Shared between an engine’s runtime and its interrupt handler.
Records why execution was interrupted rather than leaving it to be inferred from the
engine’s own exception or trap text. QuickJS, for instance, reports an interrupt as an
ordinary Error whose message happens to be “interrupted”; keying behavior off that
string would make the difference between “your rule looped forever” and “your rule
threw” depend on wording this project does not control. A different engine’s own
interrupted-execution signal would be exactly as unreliable to string-match, for the
same reason.
Implementations§
Source§impl Budget
impl Budget
Sourcepub fn new(clock: Arc<RunClock>) -> Arc<Self> ⓘ
pub fn new(clock: Arc<RunClock>) -> Arc<Self> ⓘ
Build a budget enforcer sharing the run’s clock.
Sourcepub fn pause(&self) -> Paused<'_>
pub fn pause(&self) -> Paused<'_>
Stop charging the current invocation while the host does work on its behalf.
Returns a guard; the invocation resumes with exactly the time it had when the guard was taken, measured from wherever the run clock is when the guard drops.
§Why not disarm followed by arm
Budget::arm takes a fresh rule_timeout and also clears Budget::take_trip’s
record. Re-arming after a provider call would therefore hand the rule a whole new
allowance on every question it asks — a rule asking a hundred type questions would be
bounded by nothing — and would erase a global-budget trip that had already been
recorded, turning a run timeout into silence. This carries the remainder instead, so a
rule’s own budget still bounds a rule’s own code and nothing else.
The global check at Budget::should_interrupt is untouched: a paused invocation is
still inside a run, and a run that has overrun must still stop.
Nesting composes rather than being forbidden, which costs no runtime check: an inner pause reads an already stopped clock as disarmed and so restores nothing on drop, leaving the outermost guard — the only one holding a real remainder — to resume.
Sourcepub fn should_interrupt(&self) -> bool
pub fn should_interrupt(&self) -> bool
Whether execution should stop now, recording why. Called by the engine’s interrupt handler, so it runs often and must stay cheap.