Last active
August 29, 2015 14:04
hystrix clj snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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