Skip to content

Commit 2f71963

Browse files
authored
Issue ReactiveX#137: Refactored Retry so that it can be shared and used for mu… (ReactiveX#138)
* Issue ReactiveX#137: Refactored Retry so that it can be shared and used for multiple requests.
1 parent 6f854a5 commit 2f71963

File tree

38 files changed

+1130
-895
lines changed

38 files changed

+1130
-895
lines changed

resilience4j-bulkhead/src/main/java/io/github/resilience4j/bulkhead/Bulkhead.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.function.Supplier;
3636

3737
/**
38+
* A Bulkhead instance is thread-safe can be used to decorate multiple requests.
39+
*
3840
* A {@link Bulkhead} represent an entity limiting the amount of parallel operations. It does not assume nor does it mandate usage
3941
* of any particular concurrency and/or io model. These details are left for the client to manage. This bulkhead, depending on the
4042
* underlying concurrency/io model can be used to shed load, and, where it makes sense, limit resource use (i.e. limit amount of

resilience4j-cache/src/main/java/io/github/resilience4j/cache/Cache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package io.github.resilience4j.cache;
2020

2121
import io.github.resilience4j.cache.event.CacheEvent;
22-
import io.github.resilience4j.cache.internal.CacheContext;
22+
import io.github.resilience4j.cache.internal.CacheImpl;
2323
import io.reactivex.Flowable;
2424
import io.vavr.CheckedFunction0;
2525
import io.vavr.CheckedFunction1;
@@ -72,7 +72,7 @@ public interface Cache<K, V> {
7272
*/
7373
static <K,V> Cache<K,V> of(javax.cache.Cache<K, V> cache){
7474
Objects.requireNonNull(cache, "Cache must not be null");
75-
return new CacheContext<>(cache);
75+
return new CacheImpl<>(cache);
7676
}
7777

7878
/**

resilience4j-cache/src/main/java/io/github/resilience4j/cache/internal/CacheContext.java renamed to resilience4j-cache/src/main/java/io/github/resilience4j/cache/internal/CacheImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
import java.util.concurrent.atomic.LongAdder;
3636
import java.util.function.Supplier;
3737

38-
public class CacheContext<K, V> implements Cache<K,V> {
38+
public class CacheImpl<K, V> implements Cache<K,V> {
3939

40-
private static final Logger LOG = LoggerFactory.getLogger(CacheContext.class);
40+
private static final Logger LOG = LoggerFactory.getLogger(CacheImpl.class);
4141

4242
private final javax.cache.Cache<K, V> cache;
4343
private final FlowableProcessor<CacheEvent> eventPublisher;
4444
private final CacheMetrics metrics;
4545

46-
public CacheContext(javax.cache.Cache<K, V> cache) {
46+
public CacheImpl(javax.cache.Cache<K, V> cache) {
4747
this.cache = cache;
4848
PublishProcessor<CacheEvent> publisher = PublishProcessor.create();
4949
this.eventPublisher = publisher.toSerialized();

resilience4j-circuitbreaker/src/main/java/io/github/resilience4j/circuitbreaker/CircuitBreaker.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.function.Supplier;
3636

3737
/**
38+
* A CircuitBreaker instance is thread-safe can be used to decorate multiple requests.
39+
*
3840
* A {@link CircuitBreaker} manages the state of a backend system.
3941
* The CircuitBreaker is implemented via a finite state machine with three states: CLOSED, OPEN and HALF_OPEN.
4042
* The CircuitBreaker does not know anything about the backend’s state by itself, but uses the information provided by the decorators via

resilience4j-documentation/src/docs/asciidoc/core_guides/retry.adoc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ RetryConfig config = RetryConfig.custom()
1212
.maxAttempts(3)
1313
.waitDuration(Duration.ofMillis(500))
1414
.build();
15-
Retry retryContext = Retry.of("id", config);
15+
Retry retry = Retry.of("id", config);
1616
----
1717

18-
In order to create a custom `Retry` context, you can use the Retry context builder. You can configure the maximum number of retry attempts and the wait duration between successive attempts. Furthermore, you can configure a custom Predicate which evaluates if an exception should trigger a retry.
18+
In order to create a custom-configured `Retry`, you can use the RetryConfig builder. You can configure the maximum number of retry attempts and the wait duration between successive attempts. Furthermore, you can configure a custom Predicate which evaluates if an exception should trigger a retry.
1919

2020
[source,java]
2121
----
2222
RetryConfig config = RetryConfig.custom()
2323
.maxAttempts(2)
2424
.waitDurationInOpenState(Duration.ofMillis(100))
25-
.retryOnException(throwable -> Match.of(throwable)
26-
.whenType(WebServiceException.class).then(false)
27-
.otherwise(true).get())
25+
.retryOnException(throwable -> API.Match(throwable).of(
26+
API.Case($(Predicates.instanceOf(WebServiceException.class)), true),
27+
API.Case($(), false)))
2828
.build();
2929
----
3030

@@ -39,9 +39,9 @@ HelloWorldService helloWorldService = mock(HelloWorldService.class);
3939
given(helloWorldService.sayHelloWorld()).willThrow(new WebServiceException("BAM!"));
4040
4141
// Create a Retry with default configuration
42-
Retry retryContext = Retry.ofDefaults("id");
42+
Retry retry = Retry.ofDefaults("id");
4343
// Decorate the invocation of the HelloWorldService
44-
CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retryContext, helloWorldService::sayHelloWorld);
44+
CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retry, helloWorldService::sayHelloWorld);
4545
4646
// When I invoke the function
4747
Try<String> result = Try.of(retryableSupplier).recover((throwable) -> "Hello world from recovery function");
@@ -69,9 +69,9 @@ The RetryContext emits a stream of RetryEvents to any Observer/Consumer who subs
6969

7070
[source,java]
7171
----
72-
Retry retryContext = Retry.ofDefaults("id");
72+
Retry retry = Retry.ofDefaults("id");
7373
CircularEventConsumer<RetryEvent> circularEventConsumer = new CircularEventConsumer<>(10);
74-
retryContext.getEventStream()
74+
retry.getEventStream()
7575
.subscribe(circularEventConsumer);
7676
7777
List<RetryEvent> bufferedEvents = circularEventConsumer.getBufferedEvents();

resilience4j-metrics/src/main/java/io/github/resilience4j/metrics/CircuitBreakerMetrics.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
*/
1919
package io.github.resilience4j.metrics;
2020

21-
import static com.codahale.metrics.MetricRegistry.name;
22-
import static java.util.Objects.requireNonNull;
23-
2421
import com.codahale.metrics.Gauge;
2522
import com.codahale.metrics.Metric;
2623
import com.codahale.metrics.MetricRegistry;
@@ -31,12 +28,20 @@
3128

3229
import java.util.Map;
3330

31+
import static com.codahale.metrics.MetricRegistry.name;
32+
import static java.util.Objects.requireNonNull;
33+
3434
/**
3535
* An adapter which exports {@link CircuitBreaker.Metrics} as Dropwizard Metrics Gauges.
3636
*/
3737
public class CircuitBreakerMetrics implements MetricSet {
3838

3939
private static final String DEFAULT_PREFIX = "resilience4j.circuitbreaker";
40+
public static final String SUCCESSFUL = "successful";
41+
public static final String FAILED = "failed";
42+
public static final String NOT_PERMITTED = "not_permitted";
43+
public static final String BUFFERED = "buffered";
44+
public static final String BUFFERED_MAX = "buffered_max";
4045
private final MetricRegistry metricRegistry = new MetricRegistry();
4146

4247
private CircuitBreakerMetrics(Iterable<CircuitBreaker> circuitBreakers) {
@@ -50,15 +55,15 @@ private CircuitBreakerMetrics(String prefix, Iterable<CircuitBreaker> circuitBre
5055
String name = circuitBreaker.getName();
5156
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
5257

53-
metricRegistry.register(name(prefix, name, "successful"),
58+
metricRegistry.register(name(prefix, name, SUCCESSFUL),
5459
(Gauge<Integer>) metrics::getNumberOfSuccessfulCalls);
55-
metricRegistry.register(name(prefix, name, "failed"),
60+
metricRegistry.register(name(prefix, name, FAILED),
5661
(Gauge<Integer>) metrics::getNumberOfFailedCalls);
57-
metricRegistry.register(name(prefix, name, "not_permitted"),
62+
metricRegistry.register(name(prefix, name, NOT_PERMITTED),
5863
(Gauge<Long>) metrics::getNumberOfNotPermittedCalls);
59-
metricRegistry.register(name(prefix, name, "buffered"),
64+
metricRegistry.register(name(prefix, name, BUFFERED),
6065
(Gauge<Integer>) metrics::getNumberOfBufferedCalls);
61-
metricRegistry.register(name(prefix, name, "buffered_max"),
66+
metricRegistry.register(name(prefix, name, BUFFERED_MAX),
6267
(Gauge<Integer>) metrics::getMaxNumberOfBufferedCalls);
6368
}
6469
);

resilience4j-metrics/src/main/java/io/github/resilience4j/metrics/RetryMetrics.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
public class RetryMetrics implements MetricSet {
1717

18+
public static final String SUCCESSFUL_CALLS_WITHOUT_RETRY = "successful_calls_without_retry";
19+
public static final String SUCCESSFUL_CALLS_WITH_RETRY = "successful_calls_with_retry";
20+
public static final String FAILED_CALLS_WITHOUT_RETRY = "failed_calls_without_retry";
21+
public static final String FAILED_CALLS_WITH_RETRY = "failed_calls_with_retry";
1822
private final MetricRegistry metricRegistry = new MetricRegistry();
1923
private static final String DEFAULT_PREFIX = "resilience4j.retry";
2024

@@ -29,9 +33,14 @@ private RetryMetrics(String prefix, Iterable<Retry> retries){
2933
String name = retry.getName();
3034
Retry.Metrics metrics = retry.getMetrics();
3135

32-
metricRegistry.register(name(prefix, name, "retry_max_ratio"),
33-
new RetryRatio(metrics.getNumAttempts(), metrics.getMaxAttempts()));
34-
36+
metricRegistry.register(name(prefix, name, SUCCESSFUL_CALLS_WITHOUT_RETRY),
37+
(Gauge<Long>) metrics::getNumberOfSuccessfulCallsWithoutRetryAttempt);
38+
metricRegistry.register(name(prefix, name, SUCCESSFUL_CALLS_WITH_RETRY),
39+
(Gauge<Long>) metrics::getNumberOfSuccessfulCallsWithRetryAttempt);
40+
metricRegistry.register(name(prefix, name, FAILED_CALLS_WITHOUT_RETRY),
41+
(Gauge<Long>) metrics::getNumberOfFailedCallsWithoutRetryAttempt);
42+
metricRegistry.register(name(prefix, name, FAILED_CALLS_WITH_RETRY),
43+
(Gauge<Long>) metrics::getNumberOfFailedCallsWithRetryAttempt);
3544
});
3645
}
3746

0 commit comments

Comments
 (0)