better fend interrupt
This commit is contained in:
parent
f350ca4202
commit
d6915cfd1d
@ -1,4 +1,4 @@
|
|||||||
use std::{sync::LazyLock, time::Instant};
|
use std::{cell::Cell, sync::LazyLock, time::Instant};
|
||||||
|
|
||||||
use fend_core::SpanKind;
|
use fend_core::SpanKind;
|
||||||
|
|
||||||
@ -109,23 +109,17 @@ pub static FEND_CONTEXT: LazyLock<fend_core::Context> = LazyLock::new(|| {
|
|||||||
context
|
context
|
||||||
});
|
});
|
||||||
|
|
||||||
struct TimeoutInterrupt {
|
struct Interrupter {
|
||||||
start: Instant,
|
invocations_left: Cell<u32>,
|
||||||
timeout: u128,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TimeoutInterrupt {
|
impl fend_core::Interrupt for Interrupter {
|
||||||
fn new_with_timeout(timeout: u128) -> Self {
|
|
||||||
Self {
|
|
||||||
start: Instant::now(),
|
|
||||||
timeout,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fend_core::Interrupt for TimeoutInterrupt {
|
|
||||||
fn should_interrupt(&self) -> bool {
|
fn should_interrupt(&self) -> bool {
|
||||||
Instant::now().duration_since(self.start).as_millis() > self.timeout
|
if self.invocations_left.get() == 0 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
self.invocations_left.set(self.invocations_left.get() - 1);
|
||||||
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,7 +195,9 @@ fn evaluate_into_spans(query: &str, multiline: bool) -> Vec<Span> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// not a perfect anti-abuse but good enough for our purposes
|
// not a perfect anti-abuse but good enough for our purposes
|
||||||
let interrupt = TimeoutInterrupt::new_with_timeout(10);
|
let interrupt = Interrupter {
|
||||||
|
invocations_left: Cell::new(10000),
|
||||||
|
};
|
||||||
let Ok(result) = fend_core::evaluate_with_interrupt(query, &mut context, &interrupt) else {
|
let Ok(result) = fend_core::evaluate_with_interrupt(query, &mut context, &interrupt) else {
|
||||||
return vec![];
|
return vec![];
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user