-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
59 lines (45 loc) · 2.43 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Functional Implementation of Michael Nygaard's circuit breaker
stability pattern using Clojure protocols and data types
A circuit breaker acts as an intemediary between a caller and a callee.
Typically the callee is an interface to an integration point, and
the caller is a client of that integration point.
If the circuit breaker detects a number of failures in the callee it
'trips' (i.e., transitions to the open state) and prevents further
calls to the callee for a fixed period (fail fast).
After this period it lets a single call go though and transitions
to the initial state or back to the open state depending on the outcome.
The circuit breaker is parameterized by a policy that decides
* how many failures are needed to trip
* how long before letting the single probe call pass
* what type of Exceptions from the integration point should
be considered errors to the circuit breaker
(e.g., one may want to exclude security related exceptions)
---------
The implementation uses Clojure protocols and records (data type)
to implement a functional state machine.
The current state of a circuitbreaker is held in a clojure atom,
to updating is lock-free. States are immutable (record) objects
that satisfy the CircuitBreakerTransitions protocol. The protocol
functions are pure (transition) functions that map a state to a new state.
This implementation is easy to use from Java.
For an example see src/C.java and notes below.
Compiling.
Use leiningen 1.3.x.
krukow:~/Projects/clojure/circuitbreaker$ lein compile
Listening for transport dt_socket at address: 8030
Compiling net.higher-order.integration.circuit-breaker.states
Compiling net.higher-order.integration.circuit-breaker.atomic
Compiling net.higher-order.integration.circuit-breaker.java
Compiling net.higher-order.integration.circuit-breaker.java.AtomicCircuitBreaker
To compile Java example.
krukow:~/Projects/clojure/circuitbreaker$ javac -cp lib/*:src:classes src/C.java -d classes/
To run:
krukow:~/Projects/clojure/circuitbreaker$ java -cp lib/*:src:classes C
Invoked with: KARL
KARL
Invoked with: 42
42
#:net.higher-order.integration.circuit-breaker.states.ClosedState{:policy #:net.higher-order.integration.circuit-breaker.states.TransitionPolicy{:max-fail-count 5, :timeout 5000, :is-error #<states$make_transition_policy$fn__91 net.higher_order.integration.circuit_breaker.states$make_transition_policy$fn__91@67c7980c>}, :fail-count 0}
null arg
...
krukow:~/Projects/clojure/circuitbreaker$