forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for TimeLimiter (ReactiveX#152)
* Add documentation for TimeLimiter * Update timelimiter.adoc Fix text errors.
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
resilience4j-documentation/src/docs/asciidoc/core_guides/timelimiter.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
=== TimeLimiter | ||
|
||
==== Introduction | ||
|
||
The TimeLimiter API sets a time limit on the execution of a supplied future. The decorated supplied | ||
future can be chained with a CircuitBreaker to trip the circuit breaker if the supplied | ||
future's timeout has exceeded. | ||
|
||
The TimeLimiter provides the flexibility to handle custom future implementations such as ones that | ||
can cancel its execution gracefully. The TimeLimiter by default is configured to cancel the future | ||
if an exception occurs. This is configurable within the configuration. | ||
|
||
==== Examples | ||
[source,java] | ||
---- | ||
// For example, you want to restrict the execution of a long running task to 60 seconds. | ||
TimeLimiterConfig config = TimeLimiterConfig.custom() | ||
.timeoutDuration(Duration.ofSeconds(60)) | ||
.cancelRunningFuture(true) | ||
.build(); | ||
// Create TimeLimiter | ||
TimeLimiter timeLimiter = TimeLimiter.of(config); | ||
---- | ||
|
||
===== Using a TimeLimiter | ||
|
||
TimeLimiter takes a Future Supplier and returns a Callable that will unwrap the Future and attempt | ||
to retrieve the future's value within the configured timeout period. If the timeout is reached, then | ||
an exception is thrown upstream and TimeLimiter, if configured, will attempt to cancel the future. | ||
|
||
[source,java] | ||
---- | ||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
// Wrap your call to BackendService.doSomething() in a future provided by your executor | ||
Supplier<Future<Integer>> futureSupplier = () -> executorService.submit(backendService::doSomething) | ||
// Decorate your supplier so that the future can be retrieved and executed upon | ||
Callable restrictedCall = TimeLimiter | ||
.decorateFutureSupplier(timeLimiter, futureSupplier); | ||
Try.of(restrictedCall.call) | ||
.onFailure(throwable -> LOG.info("A timeout possibly occurred.")); | ||
---- | ||
|
||
===== TimeLimiter and CircuitBreaker | ||
|
||
The following example shows how to apply a timelimit to a circuit breaker callable. | ||
|
||
[source,java] | ||
---- | ||
Callable restrictedCall = TimeLimiter | ||
.decorateFutureSupplier(timeLimiter, futureSupplier); | ||
// Decorate the restricted callable with a CircuitBreaker | ||
Callable chainedCallable = CircuitBreaker.decorateCallable(circuitBreaker, restrictedCall); | ||
Try.of(chainedCallable::call) | ||
.onFailure(throwable -> LOG.info("We might have timed out or the circuit breaker has opened.")); | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters