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