Search in sources :

Example 1 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class DecoratorsTest method shouldThrowTimeoutExceptionAndPropagateMDCContext.

@Test
public void shouldThrowTimeoutExceptionAndPropagateMDCContext() {
    TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    MDC.put("key", "ValueShouldPropagateThreadBoundary");
    MDC.put("key2", "value2");
    final Map<String, String> contextMap = MDC.getCopyOfContextMap();
    ContextAwareScheduledThreadPoolExecutor scheduledThreadPool = ContextAwareScheduledThreadPoolExecutor.newScheduledThreadPool().corePoolSize(1).build();
    CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
        assertThat(Thread.currentThread().getName()).isEqualTo("bulkhead-helloBackend-1");
        Thread.sleep(1000);
        return "Bla";
    }).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, scheduledThreadPool).withCircuitBreaker(circuitBreaker).get();
    final CompletableFuture<String> completableFuture = completionStage.toCompletableFuture().exceptionally(throwable -> {
        if (throwable != null) {
            assertThat(Thread.currentThread().getName()).isEqualTo("ContextAwareScheduledThreadPool-1");
            assertThat(MDC.getCopyOfContextMap()).hasSize(2).containsExactlyEntriesOf(contextMap);
            return MDC.getCopyOfContextMap().get("key");
        }
        return null;
    });
    waitAtMost(2, TimeUnit.SECONDS).until(matches(() -> assertThat(completableFuture).isCompletedWithValue("ValueShouldPropagateThreadBoundary")));
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) ContextAwareScheduledThreadPoolExecutor(io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor) Test(org.junit.Test)

Example 2 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class DecoratorsTest method testDecorateCompletionStageWithTimeoutExceptionFallback.

@Test
public void testDecorateCompletionStageWithTimeoutExceptionFallback() throws ExecutionException, InterruptedException {
    TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
        Thread.sleep(1000);
        return "Bla";
    }).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, Executors.newSingleThreadScheduledExecutor()).withCircuitBreaker(circuitBreaker).withFallback(TimeoutException.class, (e) -> "Fallback").get();
    String result = completionStage.toCompletableFuture().get();
    assertThat(result).isEqualTo("Fallback");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CheckedFunction(io.github.resilience4j.core.functions.CheckedFunction) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) TestThreadLocalContextHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder.TestThreadLocalContextHolder) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BDDMockito.given(org.mockito.BDDMockito.given) Arrays.asList(java.util.Arrays.asList) Cache(io.github.resilience4j.cache.Cache) Duration(java.time.Duration) Map(java.util.Map) HelloWorldService(io.github.resilience4j.test.HelloWorldService) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) HelloWorldException(io.github.resilience4j.test.HelloWorldException) Before(org.junit.Before) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CheckedRunnable(io.github.resilience4j.core.functions.CheckedRunnable) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CheckedSupplier(io.github.resilience4j.core.functions.CheckedSupplier) Retry(io.github.resilience4j.retry.Retry) Awaitility.matches(com.jayway.awaitility.Awaitility.matches) java.util.concurrent(java.util.concurrent) ContextAwareScheduledThreadPoolExecutor(io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor) BDDMockito.then(org.mockito.BDDMockito.then) Test(org.junit.Test) IOException(java.io.IOException) AsyncHelloWorldService(io.github.resilience4j.test.AsyncHelloWorldService) TestThreadLocalContextPropagatorWithHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder) Awaitility.waitAtMost(com.jayway.awaitility.Awaitility.waitAtMost) Mockito(org.mockito.Mockito) Try(io.vavr.control.Try) TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) MDC(org.slf4j.MDC) CallNotPermittedException(io.github.resilience4j.circuitbreaker.CallNotPermittedException) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Test(org.junit.Test)

Example 3 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class AbstractTimeLimiterMetrics method registerMetrics.

protected void registerMetrics(MeterRegistry meterRegistry, TimeLimiter timeLimiter, List<Tag> customTags) {
    // Remove previous meters before register
    removeMetrics(meterRegistry, timeLimiter.getName());
    Counter successes = Counter.builder(names.getCallsMetricName()).description("The number of successful calls").tag(TagNames.NAME, timeLimiter.getName()).tag(TagNames.KIND, KIND_SUCCESSFUL).tags(customTags).register(meterRegistry);
    Counter failures = Counter.builder(names.getCallsMetricName()).description("The number of failed calls").tag(TagNames.NAME, timeLimiter.getName()).tag(TagNames.KIND, KIND_FAILED).tags(customTags).register(meterRegistry);
    Counter timeouts = Counter.builder(names.getCallsMetricName()).description("The number of timed out calls").tag(TagNames.NAME, timeLimiter.getName()).tag(TagNames.KIND, KIND_TIMEOUT).tags(customTags).register(meterRegistry);
    timeLimiter.getEventPublisher().onSuccess(event -> successes.increment()).onError(event -> failures.increment()).onTimeout(event -> timeouts.increment());
    List<Meter.Id> ids = Arrays.asList(successes.getId(), failures.getId(), timeouts.getId());
    meterIdMap.put(timeLimiter.getName(), new HashSet<>(ids));
}
Also used : Counter(io.micrometer.core.instrument.Counter) HashSet(java.util.HashSet) Tag(io.micrometer.core.instrument.Tag) Arrays(java.util.Arrays) List(java.util.List) TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Objects.requireNonNull(java.util.Objects.requireNonNull) Meter(io.micrometer.core.instrument.Meter) Counter(io.micrometer.core.instrument.Counter)

Example 4 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class TimeLimiterMetricsTest method shouldRecordSuccesses.

@Test
public void shouldRecordSuccesses() {
    TimeLimiter timeLimiter = TimeLimiter.of(TimeLimiterConfig.ofDefaults());
    metricRegistry.registerAll(TimeLimiterMetrics.ofTimeLimiter(timeLimiter));
    timeLimiter.onSuccess();
    timeLimiter.onSuccess();
    assertThat(metricRegistry).hasMetricsSize(3);
    assertThat(metricRegistry).counter(DEFAULT_PREFIX + SUCCESSFUL).hasValue(2L);
    assertThat(metricRegistry).counter(DEFAULT_PREFIX + FAILED).hasValue(0L);
    assertThat(metricRegistry).counter(DEFAULT_PREFIX + TIMEOUT).hasValue(0L);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Test(org.junit.Test)

Example 5 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class TimeLimiterMetricsTest method given.

@Override
protected TimeLimiter given(String prefix, MetricRegistry metricRegistry) {
    TimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry.ofDefaults();
    TimeLimiter timeLimiter = timeLimiterRegistry.timeLimiter("testLimit");
    metricRegistry.registerAll(TimeLimiterMetrics.ofTimeLimiterRegistry(prefix, timeLimiterRegistry));
    return timeLimiter;
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry)

Aggregations

TimeLimiter (io.github.resilience4j.timelimiter.TimeLimiter)37 Test (org.junit.Test)31 TimeLimiterRegistry (io.github.resilience4j.timelimiter.TimeLimiterRegistry)9 Duration (java.time.Duration)7 ThreadPoolBulkhead (io.github.resilience4j.bulkhead.ThreadPoolBulkhead)4 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)4 Counter (io.micrometer.core.instrument.Counter)4 ContextAwareScheduledThreadPoolExecutor (io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor)3 TimeLimiterConfig (io.github.resilience4j.timelimiter.TimeLimiterConfig)3 TimeLimiterEvent (io.github.resilience4j.timelimiter.event.TimeLimiterEvent)3 DefaultEventConsumerRegistry (io.github.resilience4j.consumer.DefaultEventConsumerRegistry)2 TestThreadLocalContextPropagatorWithHolder (io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder)2 List (java.util.List)2 TimeoutException (java.util.concurrent.TimeoutException)2 Awaitility.matches (com.jayway.awaitility.Awaitility.matches)1 Awaitility.waitAtMost (com.jayway.awaitility.Awaitility.waitAtMost)1 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)1 BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)1 Cache (io.github.resilience4j.cache.Cache)1 CallNotPermittedException (io.github.resilience4j.circuitbreaker.CallNotPermittedException)1