1 |
package dk.thoerup.curcuitbreaker; |
package dk.thoerup.circuitbreaker; |
2 |
|
|
3 |
import java.util.concurrent.atomic.AtomicInteger; |
import java.util.concurrent.atomic.AtomicInteger; |
4 |
|
import java.util.concurrent.atomic.AtomicLong; |
5 |
|
|
6 |
|
import dk.thoerup.circuitbreaker.config.BreakerConfig; |
7 |
|
|
8 |
public class AccountingCircuitBreaker extends CircuitBreaker { |
public class AccountingCircuitBreaker extends CircuitBreaker { |
9 |
|
|
10 |
private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped |
private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped |
11 |
|
private AtomicInteger retripCount = new AtomicInteger(0); |
12 |
private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend |
private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend |
13 |
private AtomicInteger failureCount = new AtomicInteger(0); //how many times has the backend thrown an exception |
private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception |
14 |
|
private AtomicInteger totalCallCount = new AtomicInteger(0); |
15 |
|
|
16 |
|
private AtomicLong lastTrip = new AtomicLong(0); |
17 |
|
private AtomicLong lastRetrip = new AtomicLong(0); |
18 |
|
private AtomicLong lastFailure = new AtomicLong(0); |
19 |
|
private AtomicLong lastReset = new AtomicLong(0); |
20 |
|
|
21 |
private long lastReset = 0; |
private long lastResetCounters = 0; |
22 |
|
|
23 |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
public AccountingCircuitBreaker(String name, BreakerConfig config) { |
24 |
super(name, threshold, timeoutMS); |
super(name, config); |
25 |
|
|
26 |
|
resetCounters(); |
27 |
} |
} |
28 |
|
|
29 |
@Override |
@Override |
30 |
public Object invoke(CircuitInvocation invocation) throws Throwable { |
public Object invoke(CircuitInvocation invocation) throws Exception { |
31 |
Object result; |
Object result; |
32 |
try { |
try { |
33 |
|
totalCallCount.incrementAndGet(); |
34 |
result = super.invoke(invocation); |
result = super.invoke(invocation); |
35 |
} catch (Throwable t) { |
} catch (Exception e) { |
36 |
if (t instanceof CircuitBreakerException) { |
if (e instanceof CircuitBreakerException) { |
37 |
blockCount.incrementAndGet(); |
blockCount.incrementAndGet(); |
38 |
} else { |
} else { |
39 |
failureCount.incrementAndGet(); |
totalFailureCount.incrementAndGet(); |
40 |
|
lastFailure.set( System.currentTimeMillis() ); |
41 |
} |
} |
42 |
throw t; |
throw e; |
43 |
} |
} |
44 |
|
|
45 |
return result; |
return result; |
49 |
public void tripBreaker() { |
public void tripBreaker() { |
50 |
super.tripBreaker(); |
super.tripBreaker(); |
51 |
tripCount.incrementAndGet(); |
tripCount.incrementAndGet(); |
52 |
|
lastTrip.set( System.currentTimeMillis() ); |
53 |
|
} |
54 |
|
|
55 |
|
@Override |
56 |
|
public void retripBreaker() { |
57 |
|
super.retripBreaker(); |
58 |
|
retripCount.incrementAndGet(); |
59 |
|
lastRetrip.set( System.currentTimeMillis() ); |
60 |
} |
} |
61 |
|
|
62 |
|
|
63 |
|
@Override |
64 |
|
public void reset() { |
65 |
|
super.reset(); |
66 |
|
lastReset.set( System.currentTimeMillis()); |
67 |
|
} |
68 |
|
|
69 |
public int getTripCount() { |
public int getTripCount() { |
70 |
return tripCount.get(); |
return tripCount.get(); |
71 |
} |
} |
72 |
|
|
73 |
|
public int getRetripCount() { |
74 |
|
return retripCount.get(); |
75 |
|
} |
76 |
|
|
77 |
public int getBlockCount() { |
public int getBlockCount() { |
78 |
return blockCount.get(); |
return blockCount.get(); |
79 |
} |
} |
80 |
|
|
81 |
public int getFailureCount() { |
public int getTotalFailureCount() { |
82 |
return failureCount.get(); |
return totalFailureCount.get(); |
83 |
|
} |
84 |
|
|
85 |
|
public long getLastResetCounters() { |
86 |
|
return lastResetCounters; |
87 |
|
} |
88 |
|
|
89 |
|
public long getLastTrip() { |
90 |
|
return lastTrip.get(); |
91 |
|
} |
92 |
|
|
93 |
|
public long getLastRetrip() { |
94 |
|
return lastRetrip.get(); |
95 |
|
} |
96 |
|
|
97 |
|
public long getLastFailure() { |
98 |
|
return lastFailure.get(); |
99 |
} |
} |
100 |
|
|
101 |
public long getLastReset() { |
public long getLastReset() { |
102 |
return lastReset; |
return lastReset.get(); |
103 |
|
} |
104 |
|
|
105 |
|
public int getTotalCallCount() { |
106 |
|
return totalCallCount.get(); |
107 |
} |
} |
108 |
|
|
109 |
public void resetCounters() { |
public void resetCounters() { |
110 |
tripCount.set(0); |
tripCount.set(0); |
111 |
|
retripCount.set(0); |
112 |
blockCount.set(0); |
blockCount.set(0); |
113 |
failureCount.set(0); |
totalFailureCount.set(0); |
114 |
|
totalCallCount.set(0); |
115 |
|
lastTrip.set(0); |
116 |
|
lastRetrip.set(0); |
117 |
|
lastReset.set(0); |
118 |
|
lastFailure.set(0); |
119 |
|
|
120 |
lastReset = System.currentTimeMillis(); |
lastResetCounters = System.currentTimeMillis(); |
121 |
} |
} |
122 |
|
|
123 |
} |
} |