Skip to content

Instantly share code, notes, and snippets.

@daveray
Created January 3, 2013 13:43
Show Gist options
  • Save daveray/4443563 to your computer and use it in GitHub Desktop.
Save daveray/4443563 to your computer and use it in GitHub Desktop.
Hystrix-clj
Functions for defining and executing Hystrix dependency commands and collapers.
The definition of commands and collapers is separated from their instantiation and execution.
They are represented as plain Clojure maps (see below) which are later instantiated into
functional HystrixCommand or HystrixCollapser instances.
A command definition map can be passed to the execute, and queue functions
to invoke the command.
HystrixCommand
A command is defined as a map with the following keys:
:type Always :command. Required.
:group-key A HystrixCommandGroupKey, string, or keyword. Required.
:command-key A HystrixCommandKey, string, or keyword. Required.
:thread-pool-key A HystrixThreadPoolKey, string, or keyword. Required.
:run-fn The function to run for the command. The function may have any number of
arguments. Required.
:fallback-fn A function with the same args as :run-fn that calculates a fallback
result when the command fails. Optional, defaults to a function that throws
UnsupportedOperationException.
:cache-key-fn A function which the same args as :run-fn that calculates a cache
key for the given args. Optional, defaults to nil, i.e. no caching.
For example, here's a definition for an addition command:
; Define the command
(def plus-command
{:type :command
:group-key :MyMathGroup
:command-key :plus
:run-fn +})
; Execute the command
(hystrix/execute plus-command 1 2 3 4 5)
;=> 15
; Queue the command
(def f (hystrix/queue plus-command 4 5))
;=> java.util.concurrent.Future/clojure.lang.IDeref
; Now you can deref the future as usual
@f ; or (.get f)
;=> 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment