Skip to content

Commit

Permalink
Polish CircuitBreaker interface and update comments
Browse files Browse the repository at this point in the history
- Only carry context in tryPass/onComplete method (this might be generic in upcoming versions)

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Aug 18, 2020
1 parent ee2772b commit 55e038c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void performChecking(Context context, ResourceWrapper r) throws BlockException {
return;
}
for (CircuitBreaker cb : circuitBreakers) {
if (!cb.tryPass(context, r)) {
if (!cb.tryPass(context)) {
throw new DegradeException(cb.getRule().getLimitApp(), cb.getRule());
}
}
Expand All @@ -72,7 +72,7 @@ public void exit(Context context, ResourceWrapper r, int count, Object... args)
if (curEntry.getBlockError() == null) {
// passed request
for (CircuitBreaker circuitBreaker : circuitBreakers) {
circuitBreaker.onRequestComplete(context, r);
circuitBreaker.onRequestComplete(context);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public State currentState() {
}

@Override
public boolean tryPass(Context context, ResourceWrapper r) {
public boolean tryPass(Context context) {
// Template implementation.
if (currentState.get() == State.CLOSED) {
return true;
}
if (currentState.get() == State.OPEN) {
// For half-open state we allow a request for trial.
// For half-open state we allow a request for probing.
return retryTimeoutArrived() && fromOpenToHalfOpen(context);
}
return false;
Expand Down Expand Up @@ -106,16 +106,16 @@ protected boolean fromOpenToHalfOpen(Context context) {
notifyObservers(State.OPEN, State.HALF_OPEN, null);
Entry entry = context.getCurEntry();
entry.whenComplete(new BiConsumer<Context, Entry>() {

@Override
public void accept(Context context, Entry entry) {
// Note: This works as a temporary workaround for https://github.com/alibaba/Sentinel/issues/1638
// Without the hook, the circuit breaker won't recover from half-open state in some circumstances
// when the request is actually blocked by upcoming rules (not only degrade rules).
if (entry.getBlockError() != null) {
// Fallback to OPEN due to detecting request is blocked
currentState.compareAndSet(State.HALF_OPEN, State.OPEN);
notifyObservers(State.HALF_OPEN, State.OPEN, 1.0d);
return;
}

}
});
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public interface CircuitBreaker {
/**
* Acquires permission of an invocation only if it is available at the time of invoking.
*
* @param context
* @param r
* @param context context of current invocation
* @return {@code true} if permission was acquired and {@code false} otherwise
*/
boolean tryPass(Context context, ResourceWrapper r);
boolean tryPass(Context context);

/**
* Get current state of the circuit breaker.
Expand All @@ -50,12 +49,12 @@ public interface CircuitBreaker {
State currentState();

/**
* Called when a `passed` invocation finished.
* <p>Record a completed request with the context and handle state transformation of the circuit breaker.</p>
* <p>Called when a <strong>passed</strong> invocation finished.</p>
*
* @param context context of current invocation
* @param wrapper current resource
*/
void onRequestComplete(Context context, ResourceWrapper wrapper);
void onRequestComplete(Context context);

/**
* Circuit breaker state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void resetStat() {
}

@Override
public void onRequestComplete(Context context, ResourceWrapper r) {
public void onRequestComplete(Context context) {
Entry entry = context.getCurEntry();
if (entry == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void resetStat() {
}

@Override
public void onRequestComplete(Context context, ResourceWrapper wrapper) {
public void onRequestComplete(Context context) {
SlowRequestCounter counter = slidingCounter.currentWindow().value();
Entry entry = context.getCurEntry();
if (entry == null) {
Expand Down

0 comments on commit 55e038c

Please sign in to comment.