Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decorate Future<T> and Future<T> Supplier to return Callable<T>.
- Loading branch information
Showing
16 changed files
with
416 additions
and
495 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
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
File renamed without changes.
129 changes: 129 additions & 0 deletions
129
resilience4j-timelimiter/src/main/java/io/github/resilience4j/timelimiter/TimeLimiter.java
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,129 @@ | ||
package io.github.resilience4j.timelimiter; | ||
|
||
import io.github.resilience4j.timelimiter.internal.TimeLimiterContext; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A TimeLimiter decorator stops execution at a configurable rate. | ||
*/ | ||
public interface TimeLimiter { | ||
|
||
/** | ||
* Creates a TimeLimiter decorator with a default TimeLimiterConfig configuration. | ||
* | ||
* @return The {@link TimeLimiter} | ||
*/ | ||
static TimeLimiter ofDefaults() { | ||
return new TimeLimiterContext(TimeLimiterConfig.ofDefaults()); | ||
} | ||
|
||
/** | ||
* Creates a TimeLimiter decorator with a TimeLimiterConfig configuration. | ||
* | ||
* @param timeLimiterConfig the TimeLimiterConfig | ||
* @return The {@link TimeLimiter} | ||
*/ | ||
static TimeLimiter of(TimeLimiterConfig timeLimiterConfig) { | ||
return new TimeLimiterContext(timeLimiterConfig); | ||
} | ||
|
||
/** | ||
* Creates a TimeLimiter decorator with a timeout Duration. | ||
* | ||
* @param timeoutDuration the timeout Duration | ||
* @return The {@link TimeLimiter} | ||
*/ | ||
static TimeLimiter of(Duration timeoutDuration) { | ||
TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom() | ||
.timeoutDuration(timeoutDuration) | ||
.build(); | ||
|
||
return new TimeLimiterContext(timeLimiterConfig); | ||
} | ||
|
||
/** | ||
* Creates a Callable which is restricted by a TimeLimiter. | ||
* | ||
* @param timeLimiter the {@link TimeLimiter} | ||
* @param future the original future | ||
* @param <T> the type of results supplied supplier | ||
* @param <F> the future type supplied | ||
* @return a future which is restricted by a {@link TimeLimiter}. | ||
*/ | ||
static <T, F extends Future<T>> Callable<T> decorateFuture(final TimeLimiter timeLimiter, final F future) { | ||
return () -> { | ||
try { | ||
return future.get(timeLimiter.getTimeLimiterConfig().getTimeoutDuration().toMillis(), TimeUnit.MILLISECONDS); | ||
} catch (TimeoutException e) { | ||
if (timeLimiter.getTimeLimiterConfig().shouldCancelRunningFuture()) { | ||
future.cancel(true); | ||
} | ||
throw e; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a Callback which is restricted by a TimeLimiter. | ||
* | ||
* @param timeLimiter the {@link TimeLimiter} | ||
* @param futureSupplier the original future supplier | ||
* @param <T> the type of results supplied supplier | ||
* @param <F> the future type supplied | ||
* @return a future supplier which is restricted by a {@link TimeLimiter}. | ||
*/ | ||
static <T, F extends Future<T>> Callable<T> decorateFutureSupplier(TimeLimiter timeLimiter, Supplier<F> futureSupplier) { | ||
return () -> { | ||
Future<T> future = futureSupplier.get(); | ||
try { | ||
return future.get(timeLimiter.getTimeLimiterConfig().getTimeoutDuration().toMillis(), TimeUnit.MILLISECONDS); | ||
} catch (TimeoutException e) { | ||
if(timeLimiter.getTimeLimiterConfig().shouldCancelRunningFuture()){ | ||
future.cancel(true); | ||
} | ||
throw e; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Get the TimeLimiterConfig of this TimeLimiter decorator. | ||
* | ||
* @return the TimeLimiterConfig of this TimeLimiter decorator | ||
*/ | ||
TimeLimiterConfig getTimeLimiterConfig(); | ||
|
||
/** | ||
* Decorates and executes the decorated Future. | ||
* | ||
* @param future the original Future | ||
* | ||
* @return the result of the decorated Future. | ||
* @param <T> the result type of the future | ||
* @param <F> the type of Future | ||
* @throws Exception if unable to compute a result | ||
*/ | ||
default <T, F extends Future<T>> T executeFuture(F future) throws Exception { | ||
return decorateFuture(this, future).call(); | ||
} | ||
|
||
/** | ||
* Decorates and executes the decorated Future Supplier. | ||
* | ||
* @param futureSupplier the original future supplier | ||
* | ||
* @return the result of the Future. | ||
* @param <T> the result type of the future | ||
* @param <F> the type of Future | ||
* @throws Exception if unable to compute a result | ||
*/ | ||
default <T, F extends Future<T>> T executeFutureSupplier(Supplier<F> futureSupplier) throws Exception { | ||
return decorateFutureSupplier(this, futureSupplier).call(); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
...limiter/src/main/java/io/github/resilience4j/timelimiter/internal/TimeLimiterContext.java
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,17 @@ | ||
package io.github.resilience4j.timelimiter.internal; | ||
|
||
import io.github.resilience4j.timelimiter.TimeLimiter; | ||
import io.github.resilience4j.timelimiter.TimeLimiterConfig; | ||
|
||
public class TimeLimiterContext implements TimeLimiter { | ||
private final TimeLimiterConfig timeLimiterConfig; | ||
|
||
public TimeLimiterContext(TimeLimiterConfig timeLimiterConfig) { | ||
this.timeLimiterConfig = timeLimiterConfig; | ||
} | ||
|
||
@Override | ||
public TimeLimiterConfig getTimeLimiterConfig() { | ||
return timeLimiterConfig; | ||
} | ||
} |
Oops, something went wrong.