Skip to content

Commit 7678cd3

Browse files
maarekRobWin
authored andcommitted
Add documentation for TimeLimiter (ReactiveX#152)
* Add documentation for TimeLimiter * Update timelimiter.adoc Fix text errors.
1 parent b86f106 commit 7678cd3

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

resilience4j-documentation/src/docs/asciidoc/core_guide.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Core modules:
99
* resilience4j-bulkhead: Bulkheading
1010
* resilience4j-retry: Automatic retrying
1111
* resilience4j-cache: Response caching
12+
* resilience4j-timelimiter: Timeout handling
1213
1314
Add-on modules
1415

@@ -25,3 +26,4 @@ include::core_guides/ratelimiter.adoc[]
2526
include::core_guides/bulkhead.adoc[]
2627
include::core_guides/retry.adoc[]
2728
include::core_guides/cache.adoc[]
29+
include::core_guides/timeout.adoc[]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
=== TimeLimiter
2+
3+
==== Introduction
4+
5+
The TimeLimiter API sets a time limit on the execution of a supplied future. The decorated supplied
6+
future can be chained with a CircuitBreaker to trip the circuit breaker if the supplied
7+
future's timeout has exceeded.
8+
9+
The TimeLimiter provides the flexibility to handle custom future implementations such as ones that
10+
can cancel its execution gracefully. The TimeLimiter by default is configured to cancel the future
11+
if an exception occurs. This is configurable within the configuration.
12+
13+
==== Examples
14+
[source,java]
15+
----
16+
// For example, you want to restrict the execution of a long running task to 60 seconds.
17+
TimeLimiterConfig config = TimeLimiterConfig.custom()
18+
.timeoutDuration(Duration.ofSeconds(60))
19+
.cancelRunningFuture(true)
20+
.build();
21+
22+
// Create TimeLimiter
23+
TimeLimiter timeLimiter = TimeLimiter.of(config);
24+
----
25+
26+
===== Using a TimeLimiter
27+
28+
TimeLimiter takes a Future Supplier and returns a Callable that will unwrap the Future and attempt
29+
to retrieve the future's value within the configured timeout period. If the timeout is reached, then
30+
an exception is thrown upstream and TimeLimiter, if configured, will attempt to cancel the future.
31+
32+
[source,java]
33+
----
34+
ExecutorService executorService = Executors.newSingleThreadExecutor();
35+
36+
// Wrap your call to BackendService.doSomething() in a future provided by your executor
37+
Supplier<Future<Integer>> futureSupplier = () -> executorService.submit(backendService::doSomething)
38+
39+
// Decorate your supplier so that the future can be retrieved and executed upon
40+
Callable restrictedCall = TimeLimiter
41+
.decorateFutureSupplier(timeLimiter, futureSupplier);
42+
43+
Try.of(restrictedCall.call)
44+
.onFailure(throwable -> LOG.info("A timeout possibly occurred."));
45+
----
46+
47+
===== TimeLimiter and CircuitBreaker
48+
49+
The following example shows how to apply a timelimit to a circuit breaker callable.
50+
51+
[source,java]
52+
----
53+
Callable restrictedCall = TimeLimiter
54+
.decorateFutureSupplier(timeLimiter, futureSupplier);
55+
56+
// Decorate the restricted callable with a CircuitBreaker
57+
Callable chainedCallable = CircuitBreaker.decorateCallable(circuitBreaker, restrictedCall);
58+
59+
Try.of(chainedCallable::call)
60+
.onFailure(throwable -> LOG.info("We might have timed out or the circuit breaker has opened."));
61+
----

resilience4j-documentation/src/docs/asciidoc/getting_started.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Core modules:
1414
* resilience4j-bulkhead: Bulkheading
1515
* resilience4j-retry: Automatic retrying
1616
* resilience4j-cache: Response caching
17+
* resilience4j-timelimiter: Timeout handling
1718
1819
Add-on modules
1920

@@ -39,6 +40,7 @@ compile "io.github.resilience4j:resilience4j-ratelimiter:{release-version}"
3940
compile "io.github.resilience4j:resilience4j-retry:{release-version}"
4041
compile "io.github.resilience4j:resilience4j-bulkhead:{release-version}"
4142
compile "io.github.resilience4j:resilience4j-cache:{release-version}"
43+
compile "io.github.resilience4j:resilience4j-timelimiter:{release-version}"
4244
----
4345

4446
==== Snapshot
@@ -91,6 +93,11 @@ repositories {
9193
<artifactId>resilience4j-cache</artifactId>
9294
<version>{release-version}</version>
9395
</dependency>
96+
<dependency>
97+
<groupId>io.github.resilience4j</groupId>
98+
<artifactId>resilience4j-timelimiter</artifactId>
99+
<version>{release-version}</version>
100+
</dependency>
94101
----
95102

96103
==== Snapshot

resilience4j-documentation/src/docs/asciidoc/introduction.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Core modules:
1212
* resilience4j-bulkhead: Bulkheading
1313
* resilience4j-retry: Automatic retrying (sync and async)
1414
* resilience4j-cache: Response caching
15+
* resilience4j-timelimiter: Timeout handling
1516
1617
Add-on modules
1718

0 commit comments

Comments
 (0)