Safe execution primitives — the antidote to bare @ and raw future.
Every bare @(future ...) or @(promise) is a potential hang.
This namespace provides bounded alternatives that always terminate:
deref-safe — deref with timeout + fallback (never hangs)deref-safe! — deref with timeout, throws on timeout (never hangs)safe-future — future with timeout + Result returnsafe-future! — future with timeout, throws on timeoutAll primitives return within their timeout budget. No exceptions.
Safe execution primitives — the antidote to bare @ and raw future. Every bare `@(future ...)` or `@(promise)` is a potential hang. This namespace provides bounded alternatives that always terminate: - `deref-safe` — deref with timeout + fallback (never hangs) - `deref-safe!` — deref with timeout, throws on timeout (never hangs) - `safe-future` — future with timeout + Result return - `safe-future!` — future with timeout, throws on timeout All primitives return within their timeout budget. No exceptions.
(deref-safe ref timeout-ms fallback)Deref with timeout and fallback. Never hangs.
(deref-safe my-promise 5000 []) ;; => value or [] after 5s (deref-safe my-future 10000 nil) ;; => value or nil after 10s
Deref with timeout and fallback. Never hangs. (deref-safe my-promise 5000 []) ;; => value or [] after 5s (deref-safe my-future 10000 nil) ;; => value or nil after 10s
(deref-safe! ref timeout-ms)Deref with timeout. Throws on timeout. Never hangs indefinitely.
(deref-safe! my-promise 5000) ;; => value or throws after 5s
Deref with timeout. Throws on timeout. Never hangs indefinitely. (deref-safe! my-promise 5000) ;; => value or throws after 5s
(safe-future opts & body)Execute body in a future with timeout. Returns Result.
(safe-future {:timeout-ms 5000} (expensive-computation)) ;; => (ok result) or (err :weave/timeout {...})
Execute body in a future with timeout. Returns Result.
(safe-future {:timeout-ms 5000}
(expensive-computation))
;; => (ok result) or (err :weave/timeout {...})(safe-future-call {:keys [timeout-ms name] :or {name "anonymous"}} f)Execute f in a future with timeout. Returns Result.
(safe-future-call {:timeout-ms 5000} #(expensive-computation)) ;; => (ok result) or (err :weave/timeout {...}) or (err :weave/exception {...})
Options: :timeout-ms — max execution time (required) :name — diagnostic label (optional)
Execute f in a future with timeout. Returns Result.
(safe-future-call {:timeout-ms 5000} #(expensive-computation))
;; => (ok result) or (err :weave/timeout {...}) or (err :weave/exception {...})
Options:
:timeout-ms — max execution time (required)
:name — diagnostic label (optional)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 |