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; |
import java.util.concurrent.atomic.AtomicLong; |
5 |
|
|
6 |
|
import dk.thoerup.circuitbreaker.config.BreakerConfig; |
7 |
|
import dk.thoerup.circuitbreaker.config.StaticConfig; |
8 |
|
|
9 |
public class AccountingCircuitBreaker extends CircuitBreaker { |
public class AccountingCircuitBreaker extends CircuitBreaker { |
10 |
|
|
11 |
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 |
12 |
|
private AtomicInteger retripCount = new AtomicInteger(0); |
13 |
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 |
14 |
private AtomicInteger totalFailureCount = 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 |
15 |
private AtomicInteger totalCallCount = new AtomicInteger(0); |
private AtomicInteger totalCallCount = new AtomicInteger(0); |
16 |
|
|
17 |
private AtomicLong lastTrip = new AtomicLong(0); |
private AtomicLong lastTrip = new AtomicLong(0); |
18 |
|
private AtomicLong lastRetrip = new AtomicLong(0); |
19 |
|
private AtomicLong lastFailure = new AtomicLong(0); |
20 |
|
private AtomicLong lastReset = new AtomicLong(0); |
21 |
|
|
22 |
private long lastResetCounters = 0; |
private long lastResetCounters = 0; |
23 |
|
|
24 |
public AccountingCircuitBreaker(String name, int threshold, long timeoutMS) { |
@Deprecated |
25 |
super(name, threshold, timeoutMS); |
public AccountingCircuitBreaker(String name, int treshold, int timeout) { |
26 |
|
this(name, new StaticConfig(treshold, timeout) ) ; |
27 |
|
} |
28 |
|
|
29 |
|
public AccountingCircuitBreaker(String name, BreakerConfig config) { |
30 |
|
super(name, config); |
31 |
|
|
32 |
resetCounters(); |
resetCounters(); |
33 |
} |
} |
43 |
blockCount.incrementAndGet(); |
blockCount.incrementAndGet(); |
44 |
} else { |
} else { |
45 |
totalFailureCount.incrementAndGet(); |
totalFailureCount.incrementAndGet(); |
46 |
|
lastFailure.set( System.currentTimeMillis() ); |
47 |
} |
} |
48 |
throw e; |
throw e; |
49 |
} |
} |
58 |
lastTrip.set( System.currentTimeMillis() ); |
lastTrip.set( System.currentTimeMillis() ); |
59 |
} |
} |
60 |
|
|
61 |
|
@Override |
62 |
|
public void retripBreaker() { |
63 |
|
super.retripBreaker(); |
64 |
|
retripCount.incrementAndGet(); |
65 |
|
lastRetrip.set( System.currentTimeMillis() ); |
66 |
|
} |
67 |
|
|
68 |
|
|
69 |
|
@Override |
70 |
|
public void reset() { |
71 |
|
super.reset(); |
72 |
|
lastReset.set( System.currentTimeMillis()); |
73 |
|
} |
74 |
|
|
75 |
public int getTripCount() { |
public int getTripCount() { |
76 |
return tripCount.get(); |
return tripCount.get(); |
77 |
} |
} |
78 |
|
|
79 |
|
public int getRetripCount() { |
80 |
|
return retripCount.get(); |
81 |
|
} |
82 |
|
|
83 |
public int getBlockCount() { |
public int getBlockCount() { |
84 |
return blockCount.get(); |
return blockCount.get(); |
85 |
} |
} |
96 |
return lastTrip.get(); |
return lastTrip.get(); |
97 |
} |
} |
98 |
|
|
99 |
|
public long getLastRetrip() { |
100 |
|
return lastRetrip.get(); |
101 |
|
} |
102 |
|
|
103 |
|
public long getLastFailure() { |
104 |
|
return lastFailure.get(); |
105 |
|
} |
106 |
|
|
107 |
|
public long getLastReset() { |
108 |
|
return lastReset.get(); |
109 |
|
} |
110 |
|
|
111 |
public int getTotalCallCount() { |
public int getTotalCallCount() { |
112 |
return totalCallCount.get(); |
return totalCallCount.get(); |
113 |
} |
} |
114 |
|
|
115 |
public void resetCounters() { |
public void resetCounters() { |
116 |
tripCount.set(0); |
tripCount.set(0); |
117 |
|
retripCount.set(0); |
118 |
blockCount.set(0); |
blockCount.set(0); |
119 |
totalFailureCount.set(0); |
totalFailureCount.set(0); |
120 |
totalCallCount.set(0); |
totalCallCount.set(0); |
121 |
lastTrip.set(0); |
lastTrip.set(0); |
122 |
|
lastRetrip.set(0); |
123 |
|
lastReset.set(0); |
124 |
|
lastFailure.set(0); |
125 |
|
|
126 |
lastResetCounters = System.currentTimeMillis(); |
lastResetCounters = System.currentTimeMillis(); |
127 |
} |
} |