--- layout: doc title: Timer facility ---
A general purpose, efficient timer is provided.
schedules body for invocation after given time and returns a
CancelableFutureTask. (cancel task)
will cancel the task if possible and
return true iff cancellation was successful.
schedules timeout-form for invocation after given timeout and wraps named fn so that calling it with any arguments also cancels the timeout if possible. If the timeout has already been invoked, the fn will not run and will immediately return nil
{% highlight clojure %} (with-timeout println 800 (println "Timeout task triggered") (Thread/sleep (rand-nth [900 700])) (println "Timeout task was cancelled")) (defn handler [req] (with-channel req channel (with-timeout send! 1000 (send! {:status 200 :body "service did not return in 1000ms"}) (let [result (call-a-long-running-service)] ;; Call `send!` cancels the timeout if possible. ;; If the timeout has already been invoked (the service takes more than 1000ms to return), ;; `send!` will not run and return nil immediately. (send! {:status 200 :body result}))))) {% endhighlight %}