Diffinery

homeflare/celldpublic

celld/crates/logic/schedule.rs
57 lines · 2.4 KBRaw History
1// Copyright 2026 Deno Land Inc. Apache-2.0 license.
2
3//! Cell-isolate dispatch decisions, reified sans-IO. A cell isolate runs one
4//! event at a time. A top-level Worker fetch takes the resident-isolate fast
5//! path only when the isolate is idle; if the isolate is already pumping an
6//! actor event, the fetch must reschedule to the stateless Worker pool — never
7//! run nested — carrying its request identity so the reply still lands
8//! (`js.rs`). The executor and the production run loop hold the isolate
9//! channels and the pool; this is the pure routing they consult, so
10//! deterministic harnesses can drive it directly.
11//!
12//! Small protocol sequencing choices also live here when the executor owns
13//! the bytes but not the decision. That keeps the shell mechanical and lets
14//! callers exercise the same branch production takes.
15
16/// Where a top-level Worker fetch runs when it reaches a cell isolate.
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum Route {
19 /// The isolate is idle: run the fetch on the resident isolate (fast path).
20 OnIsolate,
21 /// The isolate is pumping an actor event: hand the fetch to the stateless
22 /// Worker pool with its request identity preserved. A Worker event never
23 /// runs nested in an isolate already running an actor event.
24 RescheduleToPool,
25}
26
27/// Route a top-level Worker fetch reaching a cell isolate. The load-bearing
28/// invariant: a Worker event must never execute nested in an isolate already
29/// pumping an actor event, so a busy isolate always reschedules to the pool.
30pub fn route_worker_fetch(isolate_active: bool) -> Route {
31 if isolate_active {
32 Route::RescheduleToPool
33 } else {
34 Route::OnIsolate
35 }
36}
37
38/// Select the protocol close code the shell must echo after the application
39/// close handler has run and all of its output has been written.
40///
41/// An application-selected close wins. Otherwise RFC 6455 requires a clean
42/// peer close to receive a close response; 1005 is the local sentinel for a
43/// frame with no status and cannot itself appear on the wire, so it becomes
44/// the normal close code 1000. An abnormal transport end receives no frame.
45pub fn websocket_echo_close(
46 peer_code: u16,
47 peer_was_clean: bool,
48 handler_sent_close: bool,
49) -> Option<u16> {
50 if !peer_was_clean || handler_sent_close {
51 None
52 } else if peer_code == 1005 {
53 Some(1000)
54 } else {
55 Some(peer_code)
56 }
57}