合入小壹整屏读取排队修复(R5)

This commit is contained in:
WuYongJie 2026-11-28 03:13:11 +08:00
commit 6504b620ab
2 changed files with 87 additions and 10 deletions

View File

@ -572,7 +572,7 @@ fn allowed_canvas_action(action: &str) -> bool {
type CanvasReply = Result<(Value, Option<PiCanvasSnapshot>), BridgeFailure>;
fn queue_canvas_screen(
pool: &super::reads::ReadPool,
pool: &super::reads::CanvasScreenReads,
cancellation: Arc<AtomicBool>,
render: impl FnOnce() -> R<Value> + Send + 'static,
reply: impl FnOnce(Value) + Send + 'static,
@ -819,7 +819,7 @@ fn run_inner(b: &mut Backend, input: &Value, events: &mpsc::Sender<Event>, ctx:
}
if action == "pi-product-screen" {
let queued = if let Some(PiCanvasSnapshot { document, cancellation }) = snapshot {
let pool = canvas_screen_reads.get_or_insert_with(super::reads::ReadPool::canvas_screens);
let pool = canvas_screen_reads.get_or_insert_with(super::reads::CanvasScreenReads::new);
let transport = daemon.clone();
let run = daemon_run.clone();
let id = value["id"].clone();
@ -933,7 +933,7 @@ mod tests {
#[test]
fn pi_screen_render_does_not_hold_save_cancel_or_read_receipts() {
let pool = super::super::reads::ReadPool::canvas_screens();
let pool = super::super::reads::CanvasScreenReads::new();
let cancellation = Arc::new(AtomicBool::new(false));
let render_cancellation = cancellation.clone();
let (started, running) = mpsc::channel();
@ -964,6 +964,66 @@ mod tests {
assert!(receipt.get("value").is_none(), "the cancelled round must not receive the rendered data");
dispatch.join().unwrap();
}
#[test]
fn pi_screen_reads_queue_the_full_batch_in_order_and_skip_cancelled_render() {
let pool = super::super::reads::CanvasScreenReads::new();
let cancellation = Arc::new(AtomicBool::new(false));
let (reply, receipts) = mpsc::channel();
let (release, gate) = mpsc::channel();
let (queued, submitted) = mpsc::channel();
let cancelled_page = 7;
let cancelled_request = cancellation.clone();
let dispatch = std::thread::spawn(move || {
let first_reply = reply.clone();
queue_canvas_screen(
&pool,
Arc::new(AtomicBool::new(false)),
move || {
gate.recv().unwrap();
Ok(json!(1))
},
move |value| first_reply.send((1, value)).unwrap(),
).unwrap();
for page in 2..=15 {
let cancellation = if page == cancelled_page {
cancelled_request.clone()
} else {
Arc::new(AtomicBool::new(false))
};
let reply = reply.clone();
queue_canvas_screen(
&pool,
cancellation,
move || {
assert_ne!(page, cancelled_page, "cancelled screen must skip rendering");
Ok(json!(page))
},
move |value| reply.send((page, value)).unwrap(),
).unwrap();
}
queued.send(()).unwrap();
});
let timeout = Duration::from_secs(2);
submitted.recv_timeout(timeout).unwrap();
assert!(receipts.try_recv().is_err());
cancellation.store(true, Ordering::SeqCst);
release.send(()).unwrap();
for page in 1..=15 {
let (id, receipt) = receipts.recv_timeout(timeout).unwrap();
assert_eq!(id, page);
if page == cancelled_page {
assert_eq!(receipt["error"]["category"], "cancelled");
} else {
assert_eq!(receipt, json!({"value":page}));
}
}
dispatch.join().unwrap();
}
#[test]
fn knowledge_selection_uses_the_resource_set_without_a_platform_rule_binding() {
let creation=json!({"brief":{"brandId":"ekb:brand:顾家","brandName":"顾家","categoryId":"ekb:category:电脑桌","categoryName":"电脑桌"},

View File

@ -1,7 +1,8 @@
//! Bounded workers for catalogue, image and canvas evidence reads.
//! Background workers for catalogue, image and canvas evidence reads.
use std::sync::{Arc, Mutex, mpsc};
type Task = Box<dyn FnOnce() + Send + 'static>;
const READ_SERVICE_STOPPED: &str = "素材加载服务已停止,请保留作品后重启";
pub(super) struct ReadPool {
queue: mpsc::SyncSender<Task>,
@ -21,11 +22,6 @@ impl ReadPool {
Self::with_limits(2, 9)
}
pub(super) fn canvas_screens() -> Self {
// Keep full-screen compositing separate from candidate and reference reads.
Self::with_limits(1, 4)
}
fn with_limits(workers: usize, pending: usize) -> Self {
let (queue, receive) = mpsc::sync_channel::<Task>(pending);
let receive = Arc::new(Mutex::new(receive));
@ -51,11 +47,32 @@ impl ReadPool {
pub(super) fn submit(&self, task: impl FnOnce() + Send + 'static) -> Result<(), &'static str> {
self.queue.try_send(Box::new(task)).map_err(|error| match error {
mpsc::TrySendError::Full(_) => "素材正在加载,请稍后再试",
mpsc::TrySendError::Disconnected(_) => "素材加载服务已停止,请保留作品后重启",
mpsc::TrySendError::Disconnected(_) => READ_SERVICE_STOPPED,
})
}
}
pub(super) struct CanvasScreenReads {
queue: mpsc::Sender<Task>,
}
impl CanvasScreenReads {
pub(super) fn new() -> Self {
// Queue whole review batches without blocking dispatch. One worker composites in order.
let (queue, receive) = mpsc::channel::<Task>();
std::thread::spawn(move || {
for task in receive {
task();
}
});
Self { queue }
}
pub(super) fn submit(&self, task: impl FnOnce() + Send + 'static) -> Result<(), &'static str> {
self.queue.send(Box::new(task)).map_err(|_| READ_SERVICE_STOPPED)
}
}
#[cfg(test)]
mod tests {
use super::*;