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 6b07318
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public BackOffPolicyBuilder random(boolean random) {

/**
* The {@link Sleeper} instance to be used to back off. Policies default to
* {@link ThreadWaitSleeper}.
* {@link NonBlockingSleeper}.
* @param sleeper the {@link Sleeper} instance
* @return this
*/
Expand Down
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 NonBlockingSleeper();

/**
* 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 NonBlockingSleeper();

public FixedBackOffPolicy withSleeper(Sleeper sleeper) {
FixedBackOffPolicy res = new FixedBackOffPolicy();
Expand All @@ -56,7 +56,7 @@ public FixedBackOffPolicy withSleeper(Sleeper sleeper) {

/**
* Public setter for the {@link Sleeper} strategy.
* @param sleeper the sleeper to set defaults to {@link ThreadWaitSleeper}.
* @param sleeper the sleeper to set defaults to {@link NonBlockingSleeper}.
*/
public void setSleeper(Sleeper sleeper) {
this.sleeper = sleeper;
Expand Down
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 NonBlockingSleeper 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 @@ -20,9 +20,11 @@
* Simple {@link Sleeper} implementation that just blocks the current Thread with sleep
* period.
*
* @deprecated in favor of {@link NonBlockingSleeper}
* @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 NonBlockingSleeper();

public UniformRandomBackOffPolicy withSleeper(Sleeper sleeper) {
UniformRandomBackOffPolicy res = new UniformRandomBackOffPolicy();
Expand All @@ -65,7 +65,7 @@ public UniformRandomBackOffPolicy withSleeper(Sleeper sleeper) {

/**
* Public setter for the {@link Sleeper} strategy.
* @param sleeper the sleeper to set defaults to {@link ThreadWaitSleeper}.
* @param sleeper the sleeper to set defaults to {@link NonBlockingSleeper}.
*/
public void setSleeper(Sleeper sleeper) {
this.sleeper = sleeper;
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 NonBlockingSleeperTests {

@Test
public void testSingleBackOff() throws Exception {
long backOffPeriod = 50;
NonBlockingSleeper strategy = new NonBlockingSleeper();
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 6b07318

Please sign in to comment.