Timed wrappers for interceptors and handlers.
Wraps any function with a timeout budget so it can't hang the caller. Primary use: event system interceptors that may call external services.
wrap-timed — wrap any (fn [x] -> x) with timeout->timed-interceptor — interceptor with timeout on :before/:aftertimed-handler — handler with timeout + fallbackClojureScript note: the JVM impls run f on a future and bound it with a
timed deref, cancelling on timeout. Node is single-threaded, so synchronous
work can not be interrupted — the :cljs branches run f to completion and treat
the timeout as advisory (no future-cancel, no passthrough/fallback path).
Timed wrappers for interceptors and handlers. Wraps any function with a timeout budget so it can't hang the caller. Primary use: event system interceptors that may call external services. - `wrap-timed` — wrap any (fn [x] -> x) with timeout - `->timed-interceptor` — interceptor with timeout on :before/:after - `timed-handler` — handler with timeout + fallback ClojureScript note: the JVM impls run f on a `future` and bound it with a timed `deref`, cancelling on timeout. Node is single-threaded, so synchronous work can not be interrupted — the :cljs branches run f to completion and treat the timeout as advisory (no `future-cancel`, no passthrough/fallback path).
(->timed-interceptor & {:keys [id before after timeout-ms] :as m})Create an interceptor with a timeout budget on :before and/or :after.
If either phase exceeds timeout-ms, it is cancelled and the context passes through unchanged (graceful degradation).
Usage: (->timed-interceptor :id :hk/smart-search-enrichment :timeout-ms 15000 :after (fn [ctx] ...expensive enrichment...))
On timeout: logs warning, context passes through, chain continues.
Create an interceptor with a timeout budget on :before and/or :after.
If either phase exceeds timeout-ms, it is cancelled and the context
passes through unchanged (graceful degradation).
Usage:
(->timed-interceptor
:id :hk/smart-search-enrichment
:timeout-ms 15000
:after (fn [ctx] ...expensive enrichment...))
On timeout: logs warning, context passes through, chain continues.(timed-handler f {:keys [timeout-ms fallback name] :or {name "handler"}})Wrap a handler function with a timeout + fallback. Unlike wrap-timed (which passes through input), this returns a specific fallback value on timeout.
(def safe-search (timed-handler search-fn {:timeout-ms 10000 :fallback {:error "timeout"}})) (safe-search params) ;; => result or {:error "timeout"}
ClojureScript: runs f synchronously and returns its result; the timeout is advisory, so the fallback is never reached for synchronous work.
Wrap a handler function with a timeout + fallback.
Unlike wrap-timed (which passes through input), this returns a
specific fallback value on timeout.
(def safe-search
(timed-handler search-fn {:timeout-ms 10000 :fallback {:error "timeout"}}))
(safe-search params) ;; => result or {:error "timeout"}
ClojureScript: runs f synchronously and returns its result; the timeout is
advisory, so the fallback is never reached for synchronous work.(wrap-timed f {:keys [timeout-ms name] :or {name "anonymous"}})Wrap f with a timeout. Returns a new function that:
Options: :timeout-ms — max execution time (required) :name — diagnostic label (optional)
(def safe-enrich (wrap-timed enrich-fn {:timeout-ms 15000 :name "enrichment"}))
ClojureScript: runs f synchronously and returns its result; the timeout is advisory (synchronous node work cannot be interrupted).
Wrap f with a timeout. Returns a new function that:
- Runs f in a future
- Returns f's result if it completes within timeout-ms
- Returns passthrough (the original input) on timeout
- Logs a warning on timeout
Options:
:timeout-ms — max execution time (required)
:name — diagnostic label (optional)
(def safe-enrich (wrap-timed enrich-fn {:timeout-ms 15000 :name "enrichment"}))
ClojureScript: runs f synchronously and returns its result; the timeout is
advisory (synchronous node work cannot be interrupted).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 |