use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.
the class StateTransitionMetricsTest method setUp.
@Before
public void setUp() throws Exception {
CircuitBreakerConfig config = CircuitBreakerConfig.custom().waitDurationInOpenState(Duration.ofSeconds(1)).failureRateThreshold(50).ringBufferSizeInHalfOpenState(3).ringBufferSizeInClosedState(10).build();
circuitBreaker = CircuitBreakerRegistry.ofDefaults().circuitBreaker("test", config);
metricRegistry.registerAll(CircuitBreakerMetrics.ofCircuitBreaker(circuitBreaker));
}
use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.
the class CircularEventConsumerTest method shouldBufferAllEvents.
@Test
public void shouldBufferAllEvents() {
// Given
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().ringBufferSizeInClosedState(3).recordFailure(throwable -> API.Match(throwable).of(Case($(instanceOf(WebServiceException.class)), true), Case($(), false))).build();
CircuitBreaker circuitBreaker = CircuitBreaker.of("testName", circuitBreakerConfig);
CircularEventConsumer<CircuitBreakerEvent> ringBuffer = new CircularEventConsumer<>(10);
circuitBreaker.getEventPublisher().onEvent(ringBuffer);
assertThat(ringBuffer.getBufferedEvents()).isEmpty();
// When
circuitBreaker.onSuccess(0);
circuitBreaker.onError(0, new WebServiceException("Bla"));
circuitBreaker.onError(0, new IOException("Bla"));
circuitBreaker.onError(0, new WebServiceException("Bla"));
// Then
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(2);
circuitBreaker.reset();
CircuitBreaker.Metrics resetMetrics = circuitBreaker.getMetrics();
assertThat(resetMetrics.getNumberOfBufferedCalls()).isEqualTo(0);
assertThat(resetMetrics.getNumberOfFailedCalls()).isEqualTo(0);
// Should store 3 events, because circuit emits 2 error events and one state transition event
assertThat(ringBuffer.getBufferedEvents()).hasSize(7);
assertThat(ringBuffer.getBufferedEvents()).extracting("eventType").containsExactly(Type.SUCCESS, Type.ERROR, Type.IGNORED_ERROR, Type.ERROR, Type.STATE_TRANSITION, Type.STATE_TRANSITION, Type.RESET);
// ringBuffer.getBufferedEvents().forEach(event -> LOG.info(event.toString()));
}
use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.
the class CircuitBreakerHealthIndicator method addDetails.
private Health.Builder addDetails(Health.Builder builder, CircuitBreaker circuitBreaker) {
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
CircuitBreakerConfig config = circuitBreaker.getCircuitBreakerConfig();
builder.withDetail(FAILURE_RATE, metrics.getFailureRate() + "%").withDetail(FAILURE_RATE_THRESHOLD, config.getFailureRateThreshold() + "%").withDetail(MAX_BUFFERED_CALLS, metrics.getMaxNumberOfBufferedCalls()).withDetail(BUFFERED_CALLS, metrics.getNumberOfBufferedCalls()).withDetail(FAILED_CALLS, metrics.getNumberOfFailedCalls()).withDetail(NOT_PERMITTED, metrics.getNumberOfNotPermittedCalls());
return builder;
}
use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project java-chassis by ServiceComb.
the class CircuitBreakerHandler method getCircuitBreaker.
private CircuitBreaker getCircuitBreaker(CircuitBreakerPolicy policy) {
LOGGER.info("applying new policy: {}", policy.toString());
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().failureRateThreshold(policy.getFailureRateThreshold()).slowCallRateThreshold(policy.getSlowCallRateThreshold()).waitDurationInOpenState(Duration.parse(policy.getWaitDurationInOpenState())).slowCallDurationThreshold(Duration.parse(policy.getSlowCallDurationThreshold())).permittedNumberOfCallsInHalfOpenState(policy.getPermittedNumberOfCallsInHalfOpenState()).minimumNumberOfCalls(policy.getMinimumNumberOfCalls()).slidingWindowType(policy.getSlidingWindowTypeEnum()).slidingWindowSize(Integer.valueOf(policy.getSlidingWindowSize())).build();
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
return circuitBreakerRegistry.circuitBreaker(policy.getName(), circuitBreakerConfig);
}
use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.
the class CircuitBreakerEventEmitterTest method testEmitter.
@Test
public void testEmitter() throws IOException {
CircuitBreakerConfig config = CircuitBreakerConfig.custom().ringBufferSizeInClosedState(3).ringBufferSizeInHalfOpenState(2).failureRateThreshold(66).waitDurationInOpenState(Duration.ofSeconds(1)).recordFailure(e -> !(e instanceof IllegalArgumentException)).build();
CircuitBreaker circuitBreaker = CircuitBreakerRegistry.ofDefaults().circuitBreaker("test", config);
Runnable run = decorateRunnable(circuitBreaker, () -> System.out.println("."));
Runnable fail = decorateRunnable(circuitBreaker, () -> {
throw new ConcurrentModificationException();
});
Runnable ignore = decorateRunnable(circuitBreaker, () -> {
throw new IllegalArgumentException();
});
SseEmitter sseEmitter = createSseEmitter(ReactorAdapter.toFlux(circuitBreaker.getEventPublisher()));
TestHandler handler = new TestHandler();
sseEmitter.initialize(handler);
exec(run, 2);
exec(ignore, 1);
exec(fail, 3);
circuitBreaker.reset();
exec(run, 2);
circuitBreaker.reset();
sseEmitter.complete();
assert handler.isCompleted;
exec(run, 2);
List<CircuitBreakerEvent.Type> events = handler.events.stream().map(CircuitBreakerEventDTO::getType).collect(toList());
then(events).containsExactly(SUCCESS, SUCCESS, IGNORED_ERROR, ERROR, ERROR, STATE_TRANSITION, NOT_PERMITTED, STATE_TRANSITION, RESET, SUCCESS, SUCCESS, RESET);
}
Aggregations