Skip to content

Commit

Permalink
GH-388: add non blocking sleeper
Browse files Browse the repository at this point in the history
- fix (#388)
- add non blocking sleeper
  • Loading branch information
Alireza Hakimrabet committed May 23, 2024
1 parent 8339dfa commit 49762a6
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.retry.backoff;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
* Non-blocking {@link Sleeper} implementation that just sleep with specified
* backOffPeriod.
*
* @author Alireza Hakimrabet
*/
@SuppressWarnings("serial")
public class AsyncWaitSleeper implements Sleeper {

@Override
public void sleep(long backOffPeriod) throws InterruptedException {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
TimeUnit.MILLISECONDS.sleep(backOffPeriod);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Unexpected exception", e);
}
});
future.join();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class ExponentialBackOffPolicy implements SleepingBackOffPolicy<Exponenti
*/
private Supplier<Double> multiplierSupplier;

private Sleeper sleeper = new ThreadWaitSleeper();
private Sleeper sleeper = new AsyncWaitSleeper();

/**
* Public setter for the {@link Sleeper} strategy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class FixedBackOffPolicy extends StatelessBackOffPolicy implements Sleepi
*/
private Supplier<Long> backOffPeriod = () -> DEFAULT_BACK_OFF_PERIOD;

private Sleeper sleeper = new ThreadWaitSleeper();
private Sleeper sleeper = new AsyncWaitSleeper();

public FixedBackOffPolicy withSleeper(Sleeper sleeper) {
FixedBackOffPolicy res = new FixedBackOffPolicy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
* Simple {@link Sleeper} implementation that just blocks the current Thread with sleep
* period.
*
* @deprecated in favor of {@link AsyncWaitSleeper}
* @author Artem Bilan
* @since 1.1
*/
@Deprecated
@SuppressWarnings("serial")
public class ThreadWaitSleeper implements Sleeper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy

private final Random random = new Random(System.currentTimeMillis());

private Sleeper sleeper = new ThreadWaitSleeper();
private Sleeper sleeper = new AsyncWaitSleeper();

public UniformRandomBackOffPolicy withSleeper(Sleeper sleeper) {
UniformRandomBackOffPolicy res = new UniformRandomBackOffPolicy();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.retry.backoff;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Alireza Hakimrabet
*/
public class AsyncWaitSleeperTests {

@Test
public void testSingleBackOff() throws Exception {
long backOffPeriod = 50;
AsyncWaitSleeper strategy = new AsyncWaitSleeper();
long before = System.currentTimeMillis();
strategy.sleep(backOffPeriod);
long after = System.currentTimeMillis();
assertEqualsApprox(backOffPeriod, after - before, 25);
}

private void assertEqualsApprox(long desired, long actual, long variance) {
long lower = desired - variance;
long upper = desired + 2 * variance;
assertThat(lower).describedAs("Expected value to be between '%d' and '%d' but was '%d'", lower, upper, actual)
.isLessThanOrEqualTo(actual);
}

}

0 comments on commit 49762a6

Please sign in to comment.