Thursday, 6 November 2014

Dynamic typing - Timeout

Another piece of useful functionality made easy by dynamic typing and clojure.async. Adding a timeout to a function.


(ns clojure-study.ideas.timeouter
(:require [clojure.core.async :as async]))
(defn add-timeout
"f - the original function
timeout-ms - timeout in milliseconds
timeout-value - the value the function should return in case of timeout"
[f timeout-ms timeout-value]
(fn [& args]
(let [timeout-channel (async/timeout timeout-ms)
result-channel (async/thread (apply f args))
[r c] (async/alts!! [timeout-channel result-channel])]
(if (= c timeout-channel)
timeout-value ;; if timeout
r)))) ;; if successful
;; ----- THAT'S IT. LET'S SEE A DEMONSTRATION ------------
;a slow function
(defn slow-fn []
(Thread/sleep 100)
:slow)
;a fast function
(defn fast-fn []
(Thread/sleep 10)
:fast)
;add 50 ms timeouts to both, returning the :timeout fall-back value
(def slow-with-timeout (add-timeout slow-fn 50 :timeout))
(def fast-with-timeout (add-timeout fast-fn 50 :timeout))
;see the results
(println "Here comes the slow:" (slow-with-timeout))
;Here comes the slow: :timeout
(println "Here comes the fast:" (fast-with-timeout))
;Here comes the fast: :fast
view raw timeouter.clj hosted with ❤ by GitHub

No comments :

Post a Comment