Skip to content

Instantly share code, notes, and snippets.

@daveray
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daveray/5a8e2c825755960a04aa to your computer and use it in GitHub Desktop.
Save daveray/5a8e2c825755960a04aa to your computer and use it in GitHub Desktop.
hystrix clj snippets
(def user-prefs
{:type :command
:group-key :Cassandra
:command-key :GetUserPreferences
:run-fn (fn [user-id] ... do request ...)
:fallback-fn (fn [user-id] { ... default prefs ...})
:cache-key-fn (fn [user-id] (str user-id)) })
(require '[com.netflix.hystrix.core :as hystrix])
; Execute
(-> user-prefs
hystrix/command ; normalize
(hystrix/execute 123456))
;=> {... user prefs ...}
; Queue Future
(-> user-prefs
hystrix/command
(hystrix/queue 123456))
;=> Future<{... user prefs ...}>
; Rx Observable
(-> user-prefs
hystrix/command
(hystrix/observe 123456))
;=> rx.Observable<{... user prefs ...}>
Reduce Boilerplate with a macro
(defn user-prefs
[user-id]
... do request ...)
(user-prefs 123456)
;=> {... user prefs ...}
(hystrix/defcommand user-prefs
[user-id]
... do request ...)
(user-prefs 123456)
;=> {... user prefs ...}
(hystrix/queue #'user-prefs 123456)
;=> Future<{... user prefs ...}>
(hystrix/observe #'user-prefs 123456)
;=> rx.Observable<{... user prefs ...}>
(hystrix/defcommand user-prefs
{:hystrix/group-key :Cassandra
:hystrix/command-key :GetUserPreferences
:hystrix/fallback-fn (fn [user-id] { ... default prefs ...})
:hystrix/cache-key-fn (fn [user-id] (str user-id)) }
[user-id]
... do request ...)
(hystrix/defcommand user-prefs
{:hystrix/init-fn (fn [_ ^HystrixComand$Setter setter]
... customize setter ...)}
[user-id]
... do request ...)
(let [^HystrixCommand e (hystrix/instantiate #'user-prefs 123456)]
... do something with raw HystrixCommand ...)
(hystrix/defcommand enrich-videos
[videos]
(for [v videos]
(get-title v)))
(binding [*out* my-writer]
(printing-command))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment