mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 16:03:10 +00:00
refactor(cdp): clarify response settlement and deferred completion
This commit is contained in:
@@ -2709,7 +2709,7 @@ impl CdpScheduler {
|
||||
for work in publications {
|
||||
if let Some(output) = work.command_output_dependency() {
|
||||
let ready = self.state.scheduler_events_ready();
|
||||
output.defer_until_response_flush(move || ready.notify_one());
|
||||
output.defer_until_response_settled(move || ready.notify_one());
|
||||
}
|
||||
// An already-admitted owner continuation declares its own turn.
|
||||
// Neither command output handling nor the adapter infers this from
|
||||
|
||||
@@ -928,11 +928,9 @@ async fn flush_background_completion_input(
|
||||
|
||||
enum RuntimeDeferredReplyFlushItem {
|
||||
Protocol(ProtocolOutputSequence),
|
||||
FinishCommand(RuntimeDeferredReplyCompletedCommand),
|
||||
}
|
||||
|
||||
struct RuntimeDeferredReplyCompletedCommand {
|
||||
output_release_permit: Option<CommandOutputReleasePermit>,
|
||||
FinishCommand {
|
||||
output_release_permit: Option<CommandOutputReleasePermit>,
|
||||
},
|
||||
}
|
||||
|
||||
struct RuntimeDeferredReplyCompletion {
|
||||
@@ -988,7 +986,7 @@ impl RuntimeDeferredReplyCompletion {
|
||||
) -> (
|
||||
ProtocolOutputSequence,
|
||||
Vec<BackgroundProtocolEvent>,
|
||||
RuntimeDeferredReplyCompletedCommand,
|
||||
Option<CommandOutputReleasePermit>,
|
||||
) {
|
||||
let (
|
||||
completion_output,
|
||||
@@ -1009,9 +1007,7 @@ impl RuntimeDeferredReplyCompletion {
|
||||
(
|
||||
completion_output,
|
||||
post_response_events,
|
||||
RuntimeDeferredReplyCompletedCommand {
|
||||
output_release_permit,
|
||||
},
|
||||
output_release_permit,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1137,9 +1133,14 @@ async fn flush_protocol_output_with_runtime_deferred_reply_routing(
|
||||
}
|
||||
}
|
||||
},
|
||||
RuntimeDeferredReplyFlushItem::FinishCommand(completed) => {
|
||||
let followup_output =
|
||||
finish_runtime_deferred_reply_completed_command(scheduler, completed).await;
|
||||
RuntimeDeferredReplyFlushItem::FinishCommand {
|
||||
output_release_permit,
|
||||
} => {
|
||||
let followup_output = finish_runtime_deferred_reply_completed_command(
|
||||
scheduler,
|
||||
output_release_permit,
|
||||
)
|
||||
.await;
|
||||
if !followup_output.is_empty() {
|
||||
pending_flush
|
||||
.push_front(RuntimeDeferredReplyFlushItem::Protocol(followup_output));
|
||||
@@ -1155,11 +1156,13 @@ fn enqueue_runtime_deferred_reply_completion_flush(
|
||||
completion: RuntimeDeferredReplyCompletion,
|
||||
remaining_output: ProtocolOutputSequence,
|
||||
) {
|
||||
let (completion_output, post_response_events, completed) = completion.into_parts();
|
||||
let (completion_output, post_response_events, output_release_permit) = completion.into_parts();
|
||||
if !remaining_output.is_empty() {
|
||||
pending_flush.push_front(RuntimeDeferredReplyFlushItem::Protocol(remaining_output));
|
||||
}
|
||||
pending_flush.push_front(RuntimeDeferredReplyFlushItem::FinishCommand(completed));
|
||||
pending_flush.push_front(RuntimeDeferredReplyFlushItem::FinishCommand {
|
||||
output_release_permit,
|
||||
});
|
||||
if !post_response_events.is_empty() {
|
||||
pending_flush.push_front(RuntimeDeferredReplyFlushItem::Protocol(
|
||||
ProtocolOutputSequence::from_background_events(post_response_events),
|
||||
@@ -1170,10 +1173,9 @@ fn enqueue_runtime_deferred_reply_completion_flush(
|
||||
|
||||
async fn finish_runtime_deferred_reply_completed_command(
|
||||
scheduler: &mut CdpScheduler,
|
||||
completed: RuntimeDeferredReplyCompletedCommand,
|
||||
output_release_permit: Option<CommandOutputReleasePermit>,
|
||||
) -> ProtocolOutputSequence {
|
||||
let mut followup_output =
|
||||
scheduler.finish_command_dispatch_output_flush(completed.output_release_permit);
|
||||
let mut followup_output = scheduler.finish_command_dispatch_output_flush(output_release_permit);
|
||||
followup_output.append(scheduler.complete_ready_protocol_residences_after_command());
|
||||
followup_output
|
||||
}
|
||||
@@ -1285,7 +1287,8 @@ async fn flush_runtime_deferred_reply_advance(
|
||||
let completion = (*completion)
|
||||
.settle_exact_outputs_before_response(scheduler)
|
||||
.await;
|
||||
let (completion_output, post_response_events, completed) = completion.into_parts();
|
||||
let (completion_output, post_response_events, output_release_permit) =
|
||||
completion.into_parts();
|
||||
frontend_router.enqueue_protocol_output_sequence(completion_output);
|
||||
if !post_response_events.is_empty()
|
||||
&& !flush_protocol_output_with_runtime_deferred_reply_routing(
|
||||
@@ -1299,7 +1302,8 @@ async fn flush_runtime_deferred_reply_advance(
|
||||
return false;
|
||||
}
|
||||
let followup_output =
|
||||
finish_runtime_deferred_reply_completed_command(scheduler, completed).await;
|
||||
finish_runtime_deferred_reply_completed_command(scheduler, output_release_permit)
|
||||
.await;
|
||||
if !followup_output.is_empty() {
|
||||
flush_protocol_output_with_runtime_deferred_reply_routing(
|
||||
frontend_router,
|
||||
|
||||
@@ -442,10 +442,10 @@ impl CommandResponseFlushContext {
|
||||
self.receiver.clone()
|
||||
}
|
||||
|
||||
/// Releases owner work when the boundary finishes or is abandoned.
|
||||
/// Releases owner work when the response boundary is flushed or cancelled.
|
||||
/// This is also suitable for waking readiness observers. It is not a
|
||||
/// success notification; use `state()` to distinguish flush and cancel.
|
||||
pub fn defer_until_response_flush(&self, release: impl FnOnce() + Send + 'static) {
|
||||
pub fn defer_until_response_settled(&self, release: impl FnOnce() + Send + 'static) {
|
||||
let release = CommandResponseFlushRelease::new(release);
|
||||
let immediate = match &self.deferred_releases {
|
||||
Some(deferred_releases) => {
|
||||
|
||||
@@ -55,7 +55,7 @@ impl CommandDispatchContext {
|
||||
}
|
||||
if let Some(continuation) = completion.take_post_response_continuation() {
|
||||
self.response_flush()
|
||||
.defer_until_response_flush(move || continuation.release());
|
||||
.defer_until_response_settled(move || continuation.release());
|
||||
}
|
||||
completion
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ impl ProtocolWorkflowState<'_> {
|
||||
if let Some(continuation) = continuation {
|
||||
context
|
||||
.response_flush()
|
||||
.defer_until_response_flush(move || continuation.release());
|
||||
.defer_until_response_settled(move || continuation.release());
|
||||
}
|
||||
if binding.as_ref().is_some_and(SessionDisposalWait::is_closed)
|
||||
|| !conn.accepts_pending_document_navigation_for_owner(&owner, &token)
|
||||
|
||||
@@ -489,7 +489,7 @@ fn command_response_flush_permit_runs_deferred_release_exactly_once() {
|
||||
let (permit, context) = conn.begin_command_response_flush_permit();
|
||||
let releases = Arc::new(AtomicUsize::new(0));
|
||||
let release_counter = releases.clone();
|
||||
context.defer_until_response_flush(move || {
|
||||
context.defer_until_response_settled(move || {
|
||||
release_counter.fetch_add(1, Ordering::SeqCst);
|
||||
});
|
||||
|
||||
@@ -508,7 +508,7 @@ fn abandoned_command_response_flush_permit_releases_fail_open() {
|
||||
let (permit, context) = conn.begin_command_response_flush_permit();
|
||||
let releases = Arc::new(AtomicUsize::new(0));
|
||||
let release_counter = releases.clone();
|
||||
context.defer_until_response_flush(move || {
|
||||
context.defer_until_response_settled(move || {
|
||||
release_counter.fetch_add(1, Ordering::SeqCst);
|
||||
});
|
||||
|
||||
@@ -531,7 +531,7 @@ fn missing_command_response_flush_context_releases_immediately() {
|
||||
);
|
||||
let releases = Arc::new(AtomicUsize::new(0));
|
||||
let release_counter = releases.clone();
|
||||
context.defer_until_response_flush(move || {
|
||||
context.defer_until_response_settled(move || {
|
||||
release_counter.fetch_add(1, Ordering::SeqCst);
|
||||
});
|
||||
|
||||
@@ -545,7 +545,7 @@ fn command_output_release_callbacks_already_observe_the_terminal_state() {
|
||||
let (permit, context) = conn.begin_command_response_flush_permit();
|
||||
let observed = context.clone();
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
context.defer_until_response_flush(move || sender.send(observed.state()).unwrap());
|
||||
context.defer_until_response_settled(move || sender.send(observed.state()).unwrap());
|
||||
std::thread::spawn(move || {
|
||||
if flush {
|
||||
permit.finish();
|
||||
@@ -563,7 +563,7 @@ fn command_output_release_callbacks_already_observe_the_terminal_state() {
|
||||
assert_eq!(receiver.recv().unwrap(), expected);
|
||||
// Observers registered after release see the same settled boundary.
|
||||
let observed = context.clone();
|
||||
context.defer_until_response_flush(move || assert_eq!(observed.state(), expected));
|
||||
context.defer_until_response_settled(move || assert_eq!(observed.state(), expected));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2389,7 +2389,7 @@ impl TestContext {
|
||||
for publication in &publications {
|
||||
if let Some(output) = publication.command_output_dependency() {
|
||||
let ready = self.state.scheduler_events_ready();
|
||||
output.defer_until_response_flush(move || ready.notify_one());
|
||||
output.defer_until_response_settled(move || ready.notify_one());
|
||||
}
|
||||
}
|
||||
self.pending_protocol_scheduler_work.extend(publications);
|
||||
|
||||
Reference in New Issue
Block a user