Lifecycle protocol and bracket macros for composable resource management.
Provides:
Lifecycle protocol — start!/stop! for any managed resource.
with-lifecycle — Single-resource bracket pattern.
Calls start! on entry, stop! in finally (always).
with-lifecycle-scope / scope-start! — Multi-resource LIFO cleanup.
Resources stopped in reverse start order on scope exit.
Reference implementations:
ManagedExecutor — wraps ScheduledThreadPoolExecutorManagedChannel — wraps core.async channelDesigned to prevent leaked executors, unclosed channels, and other resource leaks that cause GC death spirals.
Lifecycle protocol and bracket macros for composable resource management. Provides: 1. `Lifecycle` protocol — `start!`/`stop!` for any managed resource. 2. `with-lifecycle` — Single-resource bracket pattern. Calls start! on entry, stop! in finally (always). 3. `with-lifecycle-scope` / `scope-start!` — Multi-resource LIFO cleanup. Resources stopped in reverse start order on scope exit. 4. Reference implementations: - `ManagedExecutor` — wraps ScheduledThreadPoolExecutor - `ManagedChannel` — wraps core.async channel Designed to prevent leaked executors, unclosed channels, and other resource leaks that cause GC death spirals.
(->managed-channel buf-size)Create an unstarted ManagedChannel wrapping a core.async buffered channel. Call start! to create the channel.
Create an unstarted ManagedChannel wrapping a core.async buffered channel. Call start! to create the channel.
(->managed-executor pool-size)Create an unstarted ManagedExecutor wrapping a ScheduledThreadPoolExecutor. Call start! to create the thread pool.
Create an unstarted ManagedExecutor wrapping a ScheduledThreadPoolExecutor. Call start! to create the thread pool.
(put! this val)Put a value onto the channel. Returns true/false via core.async.
Put a value onto the channel. Returns true/false via core.async.
(take! this)Take a value from the channel. Returns a channel that yields the value.
Take a value from the channel. Returns a channel that yields the value.
(start! this)Start resource. Returns started resource or throws.
Start resource. Returns started resource or throws.
(stop! this)Stop/close resource. Idempotent. Returns stopped resource or throws.
Stop/close resource. Idempotent. Returns stopped resource or throws.
(lifecycle-state resource)Return the current lifecycle state of a resource. Convention: state is stored in an atom at key :state. Returns :stopped, :started, or :error.
Return the current lifecycle state of a resource. Convention: state is stored in an atom at key :state. Returns :stopped, :started, or :error.
(new-lifecycle-scope)Create a new lifecycle scope. Returns an atom holding a stack of started Lifecycle resources.
Create a new lifecycle scope. Returns an atom holding a stack of started Lifecycle resources.
(scope-start! scope resource)Start a Lifecycle resource within a scope. Calls start! on the resource, pushes the started resource onto the scope stack, and returns the started resource.
Usage: (scope-start! scope (->managed-executor 4))
Start a Lifecycle resource within a scope. Calls start! on the resource, pushes the started resource onto the scope stack, and returns the started resource. Usage: (scope-start! scope (->managed-executor 4))
(scope-stop! scope)Stop all resources in a scope in LIFO order. Errors during stop are collected but do not halt the sweep. Returns vector of any stop errors (empty on success).
Stop all resources in a scope in LIFO order. Errors during stop are collected but do not halt the sweep. Returns vector of any stop errors (empty on success).
(started? resource)True if resource is in :started state.
True if resource is in :started state.
(stopped? resource)True if resource is in :stopped state.
True if resource is in :stopped state.
(schedule! this f delay-ms)Schedule a zero-arg fn after delay-ms. Returns a ScheduledFuture.
Schedule a zero-arg fn after delay-ms. Returns a ScheduledFuture.
(schedule-at-fixed-rate! this f initial-delay-ms period-ms)Schedule a zero-arg fn at fixed rate. Returns a ScheduledFuture.
Schedule a zero-arg fn at fixed rate. Returns a ScheduledFuture.
(submit! this f)Submit a zero-arg fn for execution. Returns a Future.
Submit a zero-arg fn for execution. Returns a Future.
(with-lifecycle [sym init-expr] & body)Bracket pattern for a single Lifecycle resource.
Calls start! on entry, stop! in finally (always — even on exception). Stop! errors are caught and printed as warnings; they do not mask the original exception.
Usage: (with-lifecycle [executor (->managed-executor 4)] (submit! executor task))
Expansion: (let [raw (->managed-executor 4) executor (start! raw)] (try (submit! executor task) (finally (try (stop! executor) (catch Throwable t ...)))))
Bracket pattern for a single Lifecycle resource.
Calls start! on entry, stop! in finally (always — even on exception).
Stop! errors are caught and printed as warnings; they do not mask
the original exception.
Usage:
(with-lifecycle [executor (->managed-executor 4)]
(submit! executor task))
Expansion:
(let [raw (->managed-executor 4)
executor (start! raw)]
(try
(submit! executor task)
(finally
(try (stop! executor) (catch Throwable t ...)))))(with-lifecycle-scope [scope-sym] & body)Multi-resource scope with LIFO cleanup on exit.
Usage: (with-lifecycle-scope [scope] (let [exec (scope-start! scope (->managed-executor 4)) ch (scope-start! scope (->managed-channel 100))] ;; both auto-stopped in LIFO order on exit (submit! exec task))) ;; ch stopped first, then exec (LIFO)
Multi-resource scope with LIFO cleanup on exit.
Usage:
(with-lifecycle-scope [scope]
(let [exec (scope-start! scope (->managed-executor 4))
ch (scope-start! scope (->managed-channel 100))]
;; both auto-stopped in LIFO order on exit
(submit! exec task)))
;; ch stopped first, then exec (LIFO)cljdoc 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 |