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 15, 2019
1 parent 2a1c5be commit e61fa46
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 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 @@ -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,7 +9,7 @@
*/
public class RateLimiterBuilder {

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

Expand All @@ -30,7 +30,7 @@ public static RateLimiterBuilder newBuilder() {
* @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;
Expand Down
16 changes: 12 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,15 @@

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.*;
import java.time.Duration;
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 +43,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 +60,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 @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -28,7 +29,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 +97,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 e61fa46

Please sign in to comment.