private Object handleJoinPoint(ProceedingJoinPoint proceedingJoinPoint, io.github.resilience4j.circuitbreaker.CircuitBreaker circuitBreaker, String methodName) throws Throwable { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try { Object returnValue = proceedingJoinPoint.proceed(); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); return returnValue; } catch (Throwable throwable) { long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); if (logger.isDebugEnabled()) { logger.debug("Invocation of method '" + methodName + "' failed!", throwable); } throw throwable; } }
/** * Returns a consumer which is decorated by a CircuitBreaker. * @param circuitBreaker the CircuitBreaker * @param consumer the original consumer * @param <T> the type of the input to the consumer * * @return a consumer which is decorated by a CircuitBreaker. */ static <T> Consumer<T> decorateConsumer(CircuitBreaker circuitBreaker, Consumer<T> consumer){ return (t) -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try { consumer.accept(t); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); } catch (Throwable throwable) { long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
/** * Returns a function which is decorated by a CircuitBreaker. * @param circuitBreaker the CircuitBreaker * @param function the original function * @param <T> the type of the input to the function * @param <R> the type of the result of the function * @return a function which is decorated by a CircuitBreaker. */ static <T, R> Function<T, R> decorateFunction(CircuitBreaker circuitBreaker, Function<T, R> function){ return (T t) -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try{ R returnValue = function.apply(t); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); return returnValue; } catch (Throwable throwable){ long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
/** * Returns a runnable which is decorated by a CircuitBreaker. * * @param circuitBreaker the CircuitBreaker * @param runnable the original runnable * * @return a runnable which is decorated by a CircuitBreaker. */ static Runnable decorateRunnable(CircuitBreaker circuitBreaker, Runnable runnable){ return () -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try{ runnable.run(); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); } catch (Throwable throwable){ long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
private void markSuccess() { if (wasCallPermitted()) { circuitBreaker.onSuccess(stopWatch.stop().getProcessingDuration().toNanos()); } } }
/** * Returns a callable which is decorated by a CircuitBreaker. * * @param circuitBreaker the CircuitBreaker * @param callable the original Callable * @param <T> the result type of callable * * @return a supplier which is decorated by a CircuitBreaker. */ static <T> Callable<T> decorateCallable(CircuitBreaker circuitBreaker, Callable<T> callable){ return () -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try { T returnValue = callable.call(); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); return returnValue; } catch (Throwable throwable) { long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
/** * Returns a supplier which is decorated by a CircuitBreaker. * * @param circuitBreaker the CircuitBreaker * @param supplier the original supplier * @param <T> the type of results supplied by this supplier * * @return a supplier which is decorated by a CircuitBreaker. */ static <T> Supplier<T> decorateSupplier(CircuitBreaker circuitBreaker, Supplier<T> supplier){ return () -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try { T returnValue = supplier.get(); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); return returnValue; } catch (Throwable throwable) { long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
private void markSuccess() { if (wasCallPermitted()) { circuitBreaker.onSuccess(stopWatch.stop().getProcessingDuration().toNanos()); } }
private void markSuccess() { if (wasCallPermitted()) { circuitBreaker.onSuccess(stopWatch.stop().getProcessingDuration().toNanos()); } }
/** * Returns a runnable which is decorated by a CircuitBreaker. * * @param circuitBreaker the CircuitBreaker * @param runnable the original runnable * * @return a runnable which is decorated by a CircuitBreaker. */ static CheckedRunnable decorateCheckedRunnable(CircuitBreaker circuitBreaker, CheckedRunnable runnable){ return () -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try{ runnable.run(); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); } catch (Throwable throwable){ long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
/** * Returns a function which is decorated by a CircuitBreaker. * * @param circuitBreaker the CircuitBreaker * @param function the original function * @param <T> the type of the input to the function * @param <R> the type of the result of the function * @return a function which is decorated by a CircuitBreaker. */ static <T, R> CheckedFunction1<T, R> decorateCheckedFunction(CircuitBreaker circuitBreaker, CheckedFunction1<T, R> function){ return (T t) -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try{ R returnValue = function.apply(t); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); return returnValue; } catch (Throwable throwable){ long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
/** * Returns a consumer which is decorated by a CircuitBreaker. * @param circuitBreaker the CircuitBreaker * @param consumer the original consumer * @param <T> the type of the input to the consumer * * @return a consumer which is decorated by a CircuitBreaker. */ static <T> CheckedConsumer<T> decorateCheckedConsumer(CircuitBreaker circuitBreaker, CheckedConsumer<T> consumer){ return (t) -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try { consumer.accept(t); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); } catch (Throwable throwable) { long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
/** * Returns a supplier which is decorated by a CircuitBreaker. * * @param circuitBreaker the CircuitBreaker * @param supplier the original supplier * @param <T> the type of results supplied by this supplier * @return a supplier which is decorated by a CircuitBreaker. */ static <T> CheckedFunction0<T> decorateCheckedSupplier(CircuitBreaker circuitBreaker, CheckedFunction0<T> supplier){ return () -> { CircuitBreakerUtils.isCallPermitted(circuitBreaker); long start = System.nanoTime(); try { T returnValue = supplier.apply(); long durationInNanos = System.nanoTime() - start; circuitBreaker.onSuccess(durationInNanos); return returnValue; } catch (Throwable throwable) { long durationInNanos = System.nanoTime() - start; circuitBreaker.onError(durationInNanos, throwable); throw throwable; } }; }
promise.completeExceptionally(throwable); } else { circuitBreaker.onSuccess(durationInNanos); promise.complete(result);
future.fail(result.cause()); } else { circuitBreaker.onSuccess(durationInNanos); future.complete(result.result());
@Override public Response<T> execute() throws IOException { CircuitBreakerUtils.isCallPermitted(circuitBreaker); final StopWatch stopWatch = StopWatch.start(circuitBreaker.getName()); try { final Response<T> response = call.execute(); if (responseSuccess.test(response)) { circuitBreaker.onSuccess(stopWatch.stop().getProcessingDuration().toNanos()); } else { final Throwable throwable = new Throwable("Response error: HTTP " + response.code() + " - " + response.message()); circuitBreaker.onError(stopWatch.stop().getProcessingDuration().toNanos(), throwable); } return response; } catch (Throwable throwable) { circuitBreaker.onError(stopWatch.stop().getProcessingDuration().toNanos(), throwable); throw throwable; } } };
@Test public void shouldConsumeOnEvent() { circuitBreaker.getEventPublisher() .onEvent(this::logEventType); circuitBreaker.onSuccess(1000); then(logger).should(times(1)).info("SUCCESS"); }
@Test public void shouldConsumeOnSuccessEvent() { circuitBreaker.getEventPublisher() .onSuccess(this::logEventType); circuitBreaker.onSuccess(1000); then(logger).should(times(1)).info("SUCCESS"); }
@Test public void shouldNotProduceEventsInDisabledState() { //Given circuitBreaker = CircuitBreaker.of("test", CircuitBreakerConfig.custom() .ringBufferSizeInClosedState(1).build()); circuitBreaker.getEventPublisher() .onEvent(this::logEventType); //When we transition to disabled circuitBreaker.transitionToDisabledState(); //And we execute other calls that should generate events circuitBreaker.onError(1000, new IOException("BAM!")); circuitBreaker.onError(1000, new IOException("BAM!")); circuitBreaker.isCallPermitted(); circuitBreaker.onSuccess(0); circuitBreaker.onError(1000, new IOException("BAM!")); //Then we do not produce events then(logger).should(times(1)).info("STATE_TRANSITION"); then(logger).should(times(0)).info("NOT_PERMITTED"); then(logger).should(times(0)).info("SUCCESS"); then(logger).should(times(0)).info("ERROR"); then(logger).should(times(0)).info("IGNORED_ERROR"); }
circuitBreaker.onSuccess(0); circuitBreaker.onError(0, new WebServiceException("Bla")); circuitBreaker.onError(0, new IOException("Bla"));