Lifecycle-managed channel patterns: go-loops, pub/sub, and fresh-channel helpers.
Builds on hive-dsl.lifecycle to prevent the primary async leak pattern:
go-loops consuming channels that never terminate.
Provides:
managed-go-loop -- go-loop tied to a channel with lifecycle cleanup.
stop! closes the channel, go-loop reads nil and exits.
managed-pub-sub -- lifecycle-managed pub/sub with topic routing.
stop! closes all channels (pub + every sub).
with-go-loop -- bracket macro for temporary go-loops with auto-cleanup.
fresh-channels! -- create a map of named channels from specs,
for the channel-per-lifecycle start pattern.
Lifecycle-managed channel patterns: go-loops, pub/sub, and fresh-channel helpers. Builds on `hive-dsl.lifecycle` to prevent the primary async leak pattern: go-loops consuming channels that never terminate. Provides: 1. `managed-go-loop` -- go-loop tied to a channel with lifecycle cleanup. stop! closes the channel, go-loop reads nil and exits. 2. `managed-pub-sub` -- lifecycle-managed pub/sub with topic routing. stop! closes all channels (pub + every sub). 3. `with-go-loop` -- bracket macro for temporary go-loops with auto-cleanup. 4. `fresh-channels!` -- create a map of named channels from specs, for the channel-per-lifecycle start pattern.
(close-channels! channel-map)Close all channels in a channel map (as returned by fresh-channels!). Idempotent -- safe to call on already-closed channels.
Close all channels in a channel map (as returned by fresh-channels!). Idempotent -- safe to call on already-closed channels.
(fresh-channels! channel-specs)Create fresh channels for a lifecycle start. Returns map of name->chan. On stop, call (close-channels! result) to close all channels.
channel-specs: {:tx-chan {:buf 100} :ctrl-chan {:buf 1}}
Each spec supports: :buf -- buffer size (required, positive integer)
Create fresh channels for a lifecycle start. Returns map of name->chan.
On stop, call (close-channels! result) to close all channels.
channel-specs: {:tx-chan {:buf 100} :ctrl-chan {:buf 1}}
Each spec supports:
:buf -- buffer size (required, positive integer)(managed-go-loop handler-fn opts)Create a lifecycle-managed go-loop that consumes from a channel. Implements Lifecycle protocol. stop! closes the channel, which terminates the go-loop (it reads nil and exits).
handler-fn: (fn [msg] ...) called for each message opts: {:buf-size 100 :name "my-loop" :on-error (fn [e msg] ...)}
Create a lifecycle-managed go-loop that consumes from a channel.
Implements Lifecycle protocol. stop! closes the channel, which
terminates the go-loop (it reads nil and exits).
handler-fn: (fn [msg] ...) called for each message
opts: {:buf-size 100 :name "my-loop" :on-error (fn [e msg] ...)}(managed-pub-sub topic-fn opts)Lifecycle-managed pub/sub. publish! sends to topic, subscribe! returns a channel for a topic. stop! closes all channels. opts: {:buf-size 100 :name "events"}
Lifecycle-managed pub/sub. publish! sends to topic, subscribe! returns
a channel for a topic. stop! closes all channels.
opts: {:buf-size 100 :name "events"}(publish! managed-ps msg)Publish a message to the pub/sub. The message is put on the input channel; the pub routes it to matching subscribers via topic-fn.
Publish a message to the pub/sub. The message is put on the input channel; the pub routes it to matching subscribers via topic-fn.
(subscribe! managed-ps topic)(subscribe! managed-ps topic buf-size)Subscribe to a topic. Returns a core.async channel that receives messages matching the topic. The channel is tracked for lifecycle cleanup.
Subscribe to a topic. Returns a core.async channel that receives messages matching the topic. The channel is tracked for lifecycle cleanup.
(with-go-loop [sym handler-fn opts] & body)Bracket pattern for a temporary go-loop.
Creates a managed-go-loop, starts it, runs body, then stops it in finally. The loop binding is the started ManagedGoLoop -- use (lc/put! loop-sym msg) to send messages.
Usage: (with-go-loop [my-loop handler-fn {:buf-size 10}] (lc/put! my-loop {:type :event :data 42}) ...) ;; auto-stopped on scope exit
Bracket pattern for a temporary go-loop.
Creates a managed-go-loop, starts it, runs body, then stops it in finally.
The loop binding is the started ManagedGoLoop -- use (lc/put! loop-sym msg)
to send messages.
Usage:
(with-go-loop [my-loop handler-fn {:buf-size 10}]
(lc/put! my-loop {:type :event :data 42})
...)
;; auto-stopped on scope exitcljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |