09 / CONTROL
shutdown
A ShutdownCoordinator that drains in-flight zRPC calls: dispatch_graceful counts each call for its duration, initiate_shutdown makes new calls come back Unavailable while in-flight ones finish, and is_drained reports when the last one has completed.
WHERE IT SITS
INTERFACE
struct ShutdownCoordinatorstructA graceful-shutdown coordinator for a zRPC server: once shutdown is initiated the server stops admitting new calls, but calls already in flight are allowed to run to completion. A call brackets its work between begin_call and end_call; begin_call returns false when the server is shutting down, which the transport surfaces as Unavailable — exactly the status a client sees once a server has stopped listening. The server is fully drained once shutdown has been initiated and no calls remain in flight. The counter is plain mutable state, which is safe under moonbitlang/async's cooperative single-threaded scheduling: begin_call/end_call never yield, so the count is only observed at await points between them.
fn ShutdownCoordinator::new() -> ShutdownCoordinatorfnA coordinator that is serving normally with no calls in flight.
fn ShutdownCoordinator::begin_call(self : ShutdownCoordinator) -> BoolfnAdmit a new call: register it as in-flight and return true, unless shutdown has been initiated, in which case the call is refused (false) and the count is left untouched.
fn ShutdownCoordinator::end_call(self : ShutdownCoordinator) -> UnitfnMark an admitted call finished, dropping it from the in-flight count. Only call it for a call that begin_call admitted; the count never goes below zero.
fn ShutdownCoordinator::initiate_shutdown( self : ShutdownCoordinator) -> UnitfnBegin the graceful shutdown: from now on begin_call refuses new calls while in-flight ones keep running. Idempotent.
fn ShutdownCoordinator::is_shutting_down( self : ShutdownCoordinator) -> BoolfnWhether shutdown has been initiated.
fn ShutdownCoordinator::in_flight(self : ShutdownCoordinator) -> Int32fnThe number of calls currently in flight.
fn ShutdownCoordinator::is_drained(self : ShutdownCoordinator) -> BoolfnWhether the server is fully drained: shutdown initiated and no call in flight. A supervisor loops on this (yielding between checks) to know the last in-flight RPC has finished and the process may exit.