#1510 Fix review comments
This commit is contained in:
@@ -69,12 +69,10 @@ public class App {
|
||||
var serverStartTime = System.nanoTime();
|
||||
|
||||
var delayedService = new DelayedRemoteService(serverStartTime, 5);
|
||||
//Set the circuit Breaker parameters
|
||||
var delayedServiceCircuitBreaker = new DefaultCircuitBreaker(delayedService, 3000, 2,
|
||||
2000 * 1000 * 1000);
|
||||
|
||||
var quickService = new QuickRemoteService();
|
||||
//Set the circuit Breaker parameters
|
||||
var quickServiceCircuitBreaker = new DefaultCircuitBreaker(quickService, 3000, 2,
|
||||
2000 * 1000 * 1000);
|
||||
|
||||
@@ -99,6 +97,7 @@ public class App {
|
||||
|
||||
//Wait for the delayed service to become responsive
|
||||
try {
|
||||
LOGGER.info("Waiting for delayed service to become responsive");
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
|
@@ -28,18 +28,18 @@ package com.iluwatar.circuitbreaker;
|
||||
*/
|
||||
public interface CircuitBreaker {
|
||||
|
||||
//Success response. Reset everything to defaults
|
||||
// Success response. Reset everything to defaults
|
||||
void recordSuccess();
|
||||
|
||||
//Failure response. Handle accordingly and change state if required.
|
||||
void recordFailure();
|
||||
// Failure response. Handle accordingly with response and change state if required.
|
||||
void recordFailure(String response);
|
||||
|
||||
//Get the current state of circuit breaker
|
||||
// Get the current state of circuit breaker
|
||||
String getState();
|
||||
|
||||
//Set the specific state manually.
|
||||
// Set the specific state manually.
|
||||
void setState(State state);
|
||||
|
||||
//Attempt to fetch response from the remote service.
|
||||
// Attempt to fetch response from the remote service.
|
||||
String attemptRequest() throws RemoteServiceException;
|
||||
}
|
||||
|
@@ -24,9 +24,8 @@
|
||||
package com.iluwatar.circuitbreaker;
|
||||
|
||||
/**
|
||||
* The delay based Circuit breaker implementation that works in a
|
||||
* CLOSED->OPEN-(retry_time_period)->HALF_OPEN->CLOSED flow with some retry time period for failed
|
||||
* services and a failure threshold for service to open
|
||||
* The delay based Circuit breaker implementation that works in a CLOSED->OPEN-(retry_time_period)->HALF_OPEN->CLOSED
|
||||
* flow with some retry time period for failed services and a failure threshold for service to open
|
||||
* circuit.
|
||||
*/
|
||||
public class DefaultCircuitBreaker implements CircuitBreaker {
|
||||
@@ -35,6 +34,7 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
|
||||
private final long retryTimePeriod;
|
||||
private final RemoteService service;
|
||||
long lastFailureTime;
|
||||
private String lastFailureResponse;
|
||||
int failureCount;
|
||||
private final int failureThreshold;
|
||||
private State state;
|
||||
@@ -64,7 +64,7 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
|
||||
this.failureCount = 0;
|
||||
}
|
||||
|
||||
//Reset everything to defaults
|
||||
// Reset everything to defaults
|
||||
@Override
|
||||
public void recordSuccess() {
|
||||
this.failureCount = 0;
|
||||
@@ -73,12 +73,14 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordFailure() {
|
||||
public void recordFailure(String response) {
|
||||
failureCount = failureCount + 1;
|
||||
this.lastFailureTime = System.nanoTime();
|
||||
// Cache the failure response for returning on open state
|
||||
this.lastFailureResponse = response;
|
||||
}
|
||||
|
||||
//Evaluate the current state based on failureThreshold, failureCount and lastFailureTime.
|
||||
// Evaluate the current state based on failureThreshold, failureCount and lastFailureTime.
|
||||
protected void evaluateState() {
|
||||
if (failureCount >= failureThreshold) { //Then something is wrong with remote service
|
||||
if ((System.nanoTime() - lastFailureTime) > retryTimePeriod) {
|
||||
@@ -132,8 +134,8 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
|
||||
public String attemptRequest() throws RemoteServiceException {
|
||||
evaluateState();
|
||||
if (state == State.OPEN) {
|
||||
// return cached response if no the circuit is in OPEN state
|
||||
return "This is stale response from API";
|
||||
// return cached response if the circuit is in OPEN state
|
||||
return this.lastFailureResponse;
|
||||
} else {
|
||||
// Make the API request if the circuit is not OPEN
|
||||
try {
|
||||
@@ -145,7 +147,7 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
|
||||
recordSuccess();
|
||||
return response;
|
||||
} catch (RemoteServiceException ex) {
|
||||
recordFailure();
|
||||
recordFailure(ex.getMessage());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user