From 3d8211e50313551fa8bcacbc9882c832e7c2cc84 Mon Sep 17 00:00:00 2001 From: Dmitry Rygalov Date: Tue, 16 Jul 2019 01:16:42 +0700 Subject: [PATCH] Change timeout & invocations params type from int to long --- docs/index.md | 2 +- .../ducttape/inconsistents/Inconsistents.java | 12 ++++++------ .../ratelimits/ConstantThroughputRateLimiter.java | 2 +- .../ducttape/ratelimits/RateLimiterBuilder.java | 15 ++++++++++----- .../org/rnorth/ducttape/timeouts/Timeouts.java | 15 +++++++++++---- .../rnorth/ducttape/unreliables/Unreliables.java | 4 ++-- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/docs/index.md b/docs/index.md index 0dd3038..b43cf1d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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 diff --git a/src/main/java/org/rnorth/ducttape/inconsistents/Inconsistents.java b/src/main/java/org/rnorth/ducttape/inconsistents/Inconsistents.java index c8695c7..994c971 100644 --- a/src/main/java/org/rnorth/ducttape/inconsistents/Inconsistents.java +++ b/src/main/java/org/rnorth/ducttape/inconsistents/Inconsistents.java @@ -15,18 +15,18 @@ public class Inconsistents { /** * Retry invocation of a supplier repeatedly until it returns a consistent result for a sufficient time period. - * + *

* 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 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 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 retryUntilConsistent(final int consistentTime, final int totalTimeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable lambda) { + public static T retryUntilConsistent(final long consistentTime, final long totalTimeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable lambda) { check("consistent time must be greater than 0", consistentTime > 0); check("total timeout must be greater than 0", totalTimeout > 0); diff --git a/src/main/java/org/rnorth/ducttape/ratelimits/ConstantThroughputRateLimiter.java b/src/main/java/org/rnorth/ducttape/ratelimits/ConstantThroughputRateLimiter.java index e0ee02c..77a751a 100644 --- a/src/main/java/org/rnorth/ducttape/ratelimits/ConstantThroughputRateLimiter.java +++ b/src/main/java/org/rnorth/ducttape/ratelimits/ConstantThroughputRateLimiter.java @@ -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; } diff --git a/src/main/java/org/rnorth/ducttape/ratelimits/RateLimiterBuilder.java b/src/main/java/org/rnorth/ducttape/ratelimits/RateLimiterBuilder.java index 9d27502..3dc23fa 100644 --- a/src/main/java/org/rnorth/ducttape/ratelimits/RateLimiterBuilder.java +++ b/src/main/java/org/rnorth/ducttape/ratelimits/RateLimiterBuilder.java @@ -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() { @@ -26,11 +28,12 @@ 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; @@ -38,6 +41,7 @@ public RateLimiterBuilder withRate(final int invocations, final TimeUnit perTime /** * Configure the rate limiter to use a constant throughput strategy for rate limiting. + * * @return the builder */ public RateLimiterBuilder withConstantThroughput() { @@ -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() { diff --git a/src/main/java/org/rnorth/ducttape/timeouts/Timeouts.java b/src/main/java/org/rnorth/ducttape/timeouts/Timeouts.java index 7737125..0d91b3b 100644 --- a/src/main/java/org/rnorth/ducttape/timeouts/Timeouts.java +++ b/src/main/java/org/rnorth/ducttape/timeouts/Timeouts.java @@ -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; @@ -35,7 +42,7 @@ public Thread newThread(@NotNull Runnable r) { * @param return type of the lambda * @return the result of the successful lambda expression call */ - public static T getWithTimeout(final int timeout, final TimeUnit timeUnit, @NotNull final Callable lambda) { + public static T getWithTimeout(final long timeout, final TimeUnit timeUnit, @NotNull final Callable lambda) { check("timeout must be greater than zero", timeout > 0); @@ -52,7 +59,7 @@ public static 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); @@ -60,7 +67,7 @@ public static void doWithTimeout(final int timeout, @NotNull final TimeUnit time callFuture(timeout, timeUnit, future); } - private static T callFuture(final int timeout, @NotNull final TimeUnit timeUnit, @NotNull final Future future) { + private static T callFuture(final long timeout, @NotNull final TimeUnit timeUnit, @NotNull final Future future) { try { return future.get(timeout, timeUnit); } catch (ExecutionException e) { diff --git a/src/main/java/org/rnorth/ducttape/unreliables/Unreliables.java b/src/main/java/org/rnorth/ducttape/unreliables/Unreliables.java index 03fb85a..199dfec 100644 --- a/src/main/java/org/rnorth/ducttape/unreliables/Unreliables.java +++ b/src/main/java/org/rnorth/ducttape/unreliables/Unreliables.java @@ -28,7 +28,7 @@ public abstract class Unreliables { * @param return type of the supplier * @return the result of the successful lambda expression call */ - public static T retryUntilSuccess(final int timeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable lambda) { + public static T retryUntilSuccess(final long timeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable lambda) { check("timeout must be greater than zero", timeout > 0); @@ -96,7 +96,7 @@ public static 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 lambda) { + public static void retryUntilTrue(final long timeout, @NotNull final TimeUnit timeUnit, @NotNull final Callable lambda) { retryUntilSuccess(timeout, timeUnit, () -> { if (!lambda.call()) { throw new RuntimeException("Not ready yet");