Skip to main content

Lifecycle

FileBroker::new records a path, connect opens the file, each subscription opens a reader, and shutdown ends the writer; StdioBroker attaches the process's pipes on first use.

The file broker's three states as types, and what each transition does to the stream fileBrokerOn the fileFileBroker::new(path)ConnectedFileBrokerClosed = ()nothing to reportRecords the pathand settings, no I/OOpens the file orcreates it; primesa fresh oneEach subscription:a consumer, or areplay readerEach publish:append and flushWaits for publishes,writes the end markif set, flushes,syncs to the deviceA publisher kept from before answers NotConnected,and a seek through an old seeker is refused
The diagram as text
  1. Broker: FileBroker::new (path); ConnectedFileBroker; Closed = () nothing to report
  2. On the file: Records the path and settings, no I/O; Opens the file or creates it; primes a fresh one; Each subscription: a consumer, or a replay reader; Each publish: append and flush; Waits for publishes, writes the end mark if set, flushes, syncs to the device
  3. FileBroker::new (path) → ConnectedFileBroker: connect(self)
  4. ConnectedFileBroker → Closed = () nothing to report: shutdown(self)
  5. Records the path and settings, no I/O → Opens the file or creates it; primes a fresh one
  6. Opens the file or creates it; primes a fresh one → Each subscription: a consumer, or a replay reader
  7. Each subscription: a consumer, or a replay reader → Each publish: append and flush
  8. Each publish: append and flush → Waits for publishes, writes the end mark if set, flushes, syncs to the device
  9. A publisher kept from before answers NotConnected, and a seek through an old seeker is refused

new does no I/O, so the service's builder stays synchronous. The runtime connects the broker when the app starts. Connect creates the file when it is missing, or fails under existing_only. It checks the beacon interval, opens the client's streamer and one producer, and writes the priming message into a file that holds nothing yet.

Each subscription opens its reader on the runtime connect ran on: a consumer at the tip for a live subscription, or a reader of the whole file for a replay. A publish appends and flushes, as Writer draws.

Shutdown marks the connection closed first, so a publish that starts after it fails. It then waits for the publishes in flight and takes the producer out. The client ends the file's writer: it writes the end-of-stream mark when it is set, flushes, and syncs the file to the device. The crate drops its producer only after that and asks the client once more. A handle dropped late would otherwise end the writer of the next connection to the same file.

After shutdown, a publisher kept from before returns NotConnected, and a seek through a seeker kept from before is refused. Neither reports a success against the closed file.

The stdio broker​

The stdio broker's three states as types, and what each transition does to the process's pipesBrokerOn the pipesStdioBroker::new()ConnectedStdioBrokerClosed = ()nothing to reportRecords loopback,touches no pipeFirst subscription:the stdin threadFirst publish:the stdout threadEnds every stdiostream of the processA publisher kept from before answers NotConnected
The diagram as text
  1. Broker: StdioBroker::new(); ConnectedStdioBroker; Closed = () nothing to report
  2. On the pipes: Records loopback, touches no pipe; First subscription: the stdin thread; First publish: the stdout thread; Ends every stdio stream of the process
  3. StdioBroker::new() → ConnectedStdioBroker: connect(self)
  4. ConnectedStdioBroker → Closed = () nothing to report: shutdown(self)
  5. Records loopback, touches no pipe → First subscription: the stdin thread
  6. First subscription: the stdin thread → First publish: the stdout thread
  7. First publish: the stdout thread → Ends every stdio stream of the process
  8. A publisher kept from before answers NotConnected

Connect records whether the broker loops back and touches no pipe. The first subscription in the process starts the client's stdin thread, and the first publish its stdout thread, as Pipe draws. Both threads serve the whole process, so a shutdown ends every stdio consumer and producer in it, those of other brokers included.