Skip to content

Commit

Permalink
Issue #331: CircuitBreaker not handle java.lang.Error, but only java.…
Browse files Browse the repository at this point in the history
…lang.Exception.
  • Loading branch information
Robert Winkler committed Apr 2, 2019
1 parent 626d9a4 commit 53199cb
Showing 1 changed file with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,11 @@ static <T> CheckedFunction0<T> decorateCheckedSupplier(CircuitBreaker circuitBre
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
return returnValue;
} catch (Throwable throwable) {
} catch (Exception exception) {
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);
throw exception;
}
};
}
Expand Down Expand Up @@ -439,12 +440,15 @@ static <T> Supplier<CompletionStage<T>> decorateCompletionStage(
try {
supplier.get().whenComplete((result, throwable) -> {
long durationInNanos = System.nanoTime() - start;
if (throwable != null) {
circuitBreaker.onError(durationInNanos, throwable);
promise.completeExceptionally(throwable);
} else {
if (result != null) {
circuitBreaker.onSuccess(durationInNanos);
promise.complete(result);
} else if (throwable instanceof Exception) {
circuitBreaker.onError(durationInNanos, throwable);
promise.completeExceptionally(throwable);
} else{
// Do not handle java.lang.Error
promise.completeExceptionally(throwable);
}
});
} catch (Throwable throwable) {
Expand Down Expand Up @@ -474,10 +478,11 @@ static CheckedRunnable decorateCheckedRunnable(CircuitBreaker circuitBreaker, Ch
runnable.run();
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
} catch (Throwable throwable){
} catch (Exception exception){
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);

This comment has been minimized.

Copy link
@SasiKathimanda

SasiKathimanda Jun 22, 2020

Hi, Because of this change, it break our testcase where incase of OutOfMemory Error the CB state is now changed from OPEN to CLOSE? is it valid?

This comment has been minimized.

Copy link
@RobWin

RobWin Jun 22, 2020

Member

The code should not catch any Java Errors, because an OutOfMemory exception is not temporary.
The CircuitBreaker should not catch any Java Errors by design.

throw exception;
}
};
}
Expand All @@ -500,10 +505,11 @@ static <T> Callable<T> decorateCallable(CircuitBreaker circuitBreaker, Callable<
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
return returnValue;
} catch (Throwable throwable) {
} catch (Exception exception) {
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);
throw exception;
}
};
}
Expand All @@ -526,10 +532,11 @@ static <T> Supplier<T> decorateSupplier(CircuitBreaker circuitBreaker, Supplier<
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
return returnValue;
} catch (Throwable throwable) {
} catch (Exception exception) {
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);
throw exception;
}
};
}
Expand All @@ -551,10 +558,11 @@ static <T> Consumer<T> decorateConsumer(CircuitBreaker circuitBreaker, Consumer<
consumer.accept(t);
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
} catch (Throwable throwable) {
} catch (Exception exception) {
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);
throw exception;
}
};
}
Expand All @@ -576,10 +584,11 @@ static <T> CheckedConsumer<T> decorateCheckedConsumer(CircuitBreaker circuitBrea
consumer.accept(t);
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
} catch (Throwable throwable) {
} catch (Exception exception) {
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);
throw exception;
}
};
}
Expand All @@ -600,10 +609,11 @@ static Runnable decorateRunnable(CircuitBreaker circuitBreaker, Runnable runnabl
runnable.run();
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
} catch (Throwable throwable){
} catch (Exception exception){
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);
throw exception;
}
};
}
Expand All @@ -626,10 +636,11 @@ static <T, R> Function<T, R> decorateFunction(CircuitBreaker circuitBreaker, Fun
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
return returnValue;
} catch (Throwable throwable){
} catch (Exception exception){
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);
throw exception;
}
};
}
Expand All @@ -652,10 +663,11 @@ static <T, R> CheckedFunction1<T, R> decorateCheckedFunction(CircuitBreaker circ
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onSuccess(durationInNanos);
return returnValue;
} catch (Throwable throwable){
} catch (Exception exception){
// Do not handle java.lang.Error
long durationInNanos = System.nanoTime() - start;
circuitBreaker.onError(durationInNanos, throwable);
throw throwable;
circuitBreaker.onError(durationInNanos, exception);
throw exception;
}
};
}
Expand Down

0 comments on commit 53199cb

Please sign in to comment.