Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

Commit

Permalink
Change timeout & invocations params type from int to long
Browse files Browse the repository at this point in the history
  • Loading branch information
mityi committed Jul 21, 2019
1 parent 2a1c5be commit cff0c41
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ See [LICENSE](LICENSE).
* discuss with the authors on an issue ticket prior to doing anything big
* follow the style, naming and structure conventions of the rest of the project
* make commits atomic and easy to merge
* verify all tests are passing. Build the project with `mvn clean install` to do this.
* verify all tests are passing. Build the project with `./mvnw clean install` to do this.

## Copyright

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ public class Inconsistents {

/**
* Retry invocation of a supplier repeatedly until it returns a consistent result for a sufficient time period.
*
* <p>
* This is intended for calls to components that take an unknown amount of time to stabilise, and where
* repeated checks are the only way to detect that a stable state has been reached.
*
* @param consistentTime how long the result should be consistent for before it is returned
* @param totalTimeout how long in total to wait for stabilisation to occur
* @param timeUnit time unit for time intervals
* @param lambda an UnreliableSupplier which should be called
* @param <T> the return type of the UnreliableSupplier
* @param totalTimeout how long in total to wait for stabilisation to occur
* @param timeUnit time unit for time intervals
* @param lambda an UnreliableSupplier which should be called
* @param <T> the return type of the UnreliableSupplier
* @return the result of the supplier if it returned a consistent result for the specified interval
*/
public static <T> T retryUntilConsistent(final int consistentTime, final int totalTimeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable<T> lambda) {
public static <T> T retryUntilConsistent(final long consistentTime, final long totalTimeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable<T> lambda) {

check("consistent time must be greater than 0", consistentTime > 0);
check("total timeout must be greater than 0", totalTimeout > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ConstantThroughputRateLimiter extends RateLimiter {

private final long timeBetweenInvocations;

ConstantThroughputRateLimiter(@NotNull Integer rate, @NotNull TimeUnit perTimeUnit) {
ConstantThroughputRateLimiter(@NotNull Long rate, @NotNull TimeUnit perTimeUnit) {
this.timeBetweenInvocations = perTimeUnit.toMillis(1) / rate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
*/
public class RateLimiterBuilder {

private Integer invocations;
private Long invocations;
private TimeUnit perTimeUnit;
private RateLimiterStrategy strategy;

private RateLimiterBuilder() { }
private RateLimiterBuilder() {
}

/**
* Obtain a new builder instance.
*
* @return a new builder
*/
public static RateLimiterBuilder newBuilder() {
Expand All @@ -26,18 +28,20 @@ public static RateLimiterBuilder newBuilder() {
/**
* Set the maximum rate that the limiter should allow, expressed as the number of invocations
* allowed in a given time period.
* @param invocations number of invocations
* @param perTimeUnit the time period in which this number of invocations are allowed
*
* @param invocations number of invocations
* @param perTimeUnit the time period in which this number of invocations are allowed
* @return the builder
*/
public RateLimiterBuilder withRate(final int invocations, final TimeUnit perTimeUnit) {
public RateLimiterBuilder withRate(final long invocations, final TimeUnit perTimeUnit) {
this.invocations = invocations;
this.perTimeUnit = perTimeUnit;
return this;
}

/**
* Configure the rate limiter to use a constant throughput strategy for rate limiting.
*
* @return the builder
*/
public RateLimiterBuilder withConstantThroughput() {
Expand All @@ -47,6 +51,7 @@ public RateLimiterBuilder withConstantThroughput() {

/**
* Build and obtain a configured rate limiter. A rate and rate limiting strategy must have been selected.
*
* @return the configured rate limiter instance
*/
public RateLimiter build() {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/org/rnorth/ducttape/timeouts/Timeouts.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import static org.rnorth.ducttape.Preconditions.check;
Expand Down Expand Up @@ -35,7 +42,7 @@ public Thread newThread(@NotNull Runnable r) {
* @param <T> return type of the lambda
* @return the result of the successful lambda expression call
*/
public static <T> T getWithTimeout(final int timeout, final TimeUnit timeUnit, @NotNull final Callable<T> lambda) {
public static <T> T getWithTimeout(final long timeout, final TimeUnit timeUnit, @NotNull final Callable<T> lambda) {

check("timeout must be greater than zero", timeout > 0);

Expand All @@ -52,15 +59,15 @@ public static <T> T getWithTimeout(final int timeout, final TimeUnit timeUnit, @
* @param timeUnit time unit for time interval
* @param lambda supplier lambda expression (may throw checked exceptions)
*/
public static void doWithTimeout(final int timeout, @NotNull final TimeUnit timeUnit, @NotNull final Runnable lambda) {
public static void doWithTimeout(final long timeout, @NotNull final TimeUnit timeUnit, @NotNull final Runnable lambda) {

check("timeout must be greater than zero", timeout > 0);

Future<?> future = EXECUTOR_SERVICE.submit(lambda);
callFuture(timeout, timeUnit, future);
}

private static <T> T callFuture(final int timeout, @NotNull final TimeUnit timeUnit, @NotNull final Future<T> future) {
private static <T> T callFuture(final long timeout, @NotNull final TimeUnit timeUnit, @NotNull final Future<T> future) {
try {
return future.get(timeout, timeUnit);
} catch (ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class Unreliables {
* @param <T> return type of the supplier
* @return the result of the successful lambda expression call
*/
public static <T> T retryUntilSuccess(final int timeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable<T> lambda) {
public static <T> T retryUntilSuccess(final long timeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable<T> lambda) {

check("timeout must be greater than zero", timeout > 0);

Expand Down Expand Up @@ -96,7 +96,7 @@ public static <T> T retryUntilSuccess(final int tryLimit, @NotNull final Callabl
* @param timeUnit time unit for time interval
* @param lambda supplier lambda expression
*/
public static void retryUntilTrue(final int timeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable<Boolean> lambda) {
public static void retryUntilTrue(final long timeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable<Boolean> lambda) {
retryUntilSuccess(timeout, timeUnit, () -> {
if (!lambda.call()) {
throw new RuntimeException("Not ready yet");
Expand Down

0 comments on commit cff0c41

Please sign in to comment.