sunng87 / diehard

Clojure resilience library for flexible retry, circuit breaker and rate limiter
Eclipse Public License 2.0
330 stars 27 forks source link

Timout support #31

Closed jainsahab closed 4 years ago

jainsahab commented 4 years ago

Fix for #27

jainsahab commented 4 years ago

Providing wrapper function over Failsafe's timeout functionality, Create a timeout using deftimeout macro and use one of the get-with-timeout, run-with-timeout, get-async-with-timeout or run-async-with-timeout to exucute and retrieve the result.

(ns diehard.example
  (:require [diehard.core :refer :all]
            [diehard.timeout :as dt])
  (:import (java.time Duration)))

(defn -main []
  (deftimeout timeout (Duration/ofSeconds 4) {:on-success (fn [_]
                                                            (println "executed successfully"))})
  (println (dt/get-with-timeout timeout (fn []
                                          (Thread/sleep 3000)
                                          "result")))
  (dt/run-with-timeout timeout (fn []
                                 (Thread/sleep 3000)
                                 (println "result"))))

Apart from get-with-timeout and run-with-timeout, this library also offers get-async-with-timeout and run-async-with-timeout to execute a block asynchronously against a timeout.

jainsahab commented 4 years ago

Hi @sunng87 , I am done with the #27 solution. Could you please review it and let me know if there is any feedback from your side? Thanks. :)

sunng87 commented 4 years ago

Thanks @jainsahab ! This looks good to me overall. I will merge it for now. But I may update some of apis before final release.

jainsahab commented 4 years ago

Thanks @sunng87 , Waiting for the release of latest version. :)

anyayunli commented 4 years ago

Excellent work! Waiting for release +1 !!