Question

I'm learning about Hystrix and Clojure and don't understand how to (properly) set a timeout on a Hystrix command in Clojure.

I searched StackOverflow and the web more generally. I looked at Hystrix's Clojure wrapper source code (https://github.com/Netflix/Hystrix/blob/master/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj). There is a init-fn function parameter that looked promising, but the comments seem to suggest that this won't be a sustainable solution. But would this be a simple start?

I have a ridiculously simple Hystrix command running in Clojure and would appreciate help in extending this to set, say, a 200ms timeout:

(ns hystrix-timeout.core
  (:require [clojure.string :as str])
  (:require [com.netflix.hystrix.core :as hystrix])
  (:gen-class))


(defn my-primary [a]
  (if (= a true) (throw (Exception. "Primary failed")) (str "primary: " a)))

(defn my-fallback [a]
  (str "fallback: " a))

(hystrix/defcommand my-command
  {:hystrix/fallback-fn my-fallback}
  [a]
  (my-primary a))


(defn -main
  "Executes a simple Hystrix command. Will use a timeout when I know how to do this in Clojure."
  [& args]

  (println (my-command false))
  (println (my-command true))

  (System/exit 0)   ; Exit explicitly as Hystrix threads are still running.
)

I've put my lein project up at https://github.com/oliverbaier/learning/tree/master/hystrix-timeout in case this makes answering easier.

Thanks a lot, Oliver

Was it helpful?

Solution

The simplest route is to just use System properties. You can set either a global default:

(System/setProperty "hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds" "200")

or a command specific value:

(System/setProperty "hystrix.command.my-command.execution.isolation.thread.timeoutInMilliseconds" "200")

You can perform this in your -main method. If you are running a real web-app sans -main, you can add a ring init in project.clj :ring {:handler your-handler :init your-config-method} your-config-method will be invoked on startup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top