-
Notifications
You must be signed in to change notification settings - Fork 586
Exponential backoff for recovery retry interval #309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/rabbitmq/client/RecoveryDelayHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package com.rabbitmq.client; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A RecoveryDelayHandler is used to tell automatic recovery how long to sleep between reconnect attempts. | ||
| * | ||
| * @since 4.3.0 | ||
| */ | ||
| public interface RecoveryDelayHandler { | ||
|
|
||
| /** | ||
| * Get the time to sleep (in milliseconds) before attempting to reconnect and recover again. | ||
| * This method will be called with recoveryAttempts=0 before the first recovery attempt and then again after each failed recovery. | ||
| * | ||
| * @param recoveryAttempts | ||
| * The number of recovery attempts so far. | ||
| * @return the delay in milliseconds | ||
| */ | ||
| long getDelay(final int recoveryAttempts); | ||
|
|
||
| /** | ||
| * Basic implementation of {@link RecoveryDelayHandler} that returns the {@link ConnectionFactory#getNetworkRecoveryInterval() network recovery interval} each time. | ||
| */ | ||
| public static class DefaultRecoveryDelayHandler implements RecoveryDelayHandler { | ||
|
|
||
| private final long networkRecoveryInterval; | ||
|
|
||
| /** | ||
| * Default Constructor | ||
| * @param networkRecoveryInterval | ||
| * recovery delay time in millis | ||
| */ | ||
| public DefaultRecoveryDelayHandler(final long networkRecoveryInterval) { | ||
| this.networkRecoveryInterval = networkRecoveryInterval; | ||
| } | ||
|
|
||
| @Override | ||
| public long getDelay(int recoveryAttempts) { | ||
| return networkRecoveryInterval; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Backoff implementation of {@link RecoveryDelayHandler} that uses the Fibonacci sequence (by default) to increase the recovery delay time after each failed attempt. | ||
| * You can optionally use your own backoff sequence. | ||
| */ | ||
| public static class ExponentialBackoffDelayHandler implements RecoveryDelayHandler { | ||
|
|
||
| private final List<Long> sequence; | ||
|
|
||
| /** | ||
| * Default Constructor. Uses the fibonacci sequence: {0, 1000, 1000, 2000, 3000, 5000, 8000, 13000, 21000}. | ||
| */ | ||
| public ExponentialBackoffDelayHandler() { | ||
| sequence = Arrays.asList(0L, 1000L, 1000L, 2000L, 3000L, 5000L, 8000L, 13000L, 21000L); | ||
| } | ||
|
|
||
| /** | ||
| * Constructor for passing your own backoff sequence | ||
| * | ||
| * @param sequence | ||
| * List of recovery delay values in milliseconds. | ||
| * @throws IllegalArgumentException if the sequence is null or empty | ||
| */ | ||
| public ExponentialBackoffDelayHandler(final List<Long> sequence) { | ||
| if (sequence == null || sequence.isEmpty()) | ||
| throw new IllegalArgumentException(); | ||
| this.sequence = Collections.unmodifiableList(sequence); | ||
| } | ||
|
|
||
| @Override | ||
| public long getDelay(int recoveryAttempts) { | ||
| return sequence.get(recoveryAttempts >= sequence.size() ? sequence.size() - 1 : recoveryAttempts); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/java/com/rabbitmq/client/test/RecoveryDelayHandlerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.rabbitmq.client.test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
|
|
||
| import com.rabbitmq.client.RecoveryDelayHandler; | ||
| import com.rabbitmq.client.RecoveryDelayHandler.DefaultRecoveryDelayHandler; | ||
| import com.rabbitmq.client.RecoveryDelayHandler.ExponentialBackoffDelayHandler; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class RecoveryDelayHandlerTest { | ||
|
|
||
| @Test | ||
| public void testDefaultRecoveryDelayHandler() { | ||
| final RecoveryDelayHandler handler = new DefaultRecoveryDelayHandler(5000); | ||
| assertEquals(5000L, handler.getDelay(0)); | ||
| assertEquals(5000L, handler.getDelay(1)); | ||
| assertEquals(5000L, handler.getDelay(Integer.MAX_VALUE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExponentialBackoffDelayHandler_default() { | ||
| final RecoveryDelayHandler handler = new ExponentialBackoffDelayHandler(); | ||
| assertEquals(0, handler.getDelay(0)); | ||
| assertEquals(1000L, handler.getDelay(1)); | ||
| assertEquals(1000L, handler.getDelay(2)); | ||
| assertEquals(2000L, handler.getDelay(3)); | ||
| assertEquals(3000L, handler.getDelay(4)); | ||
| assertEquals(5000L, handler.getDelay(5)); | ||
| assertEquals(8000L, handler.getDelay(6)); | ||
| assertEquals(13000L, handler.getDelay(7)); | ||
| assertEquals(21000L, handler.getDelay(8)); | ||
| assertEquals(21000L, handler.getDelay(9)); | ||
| assertEquals(21000L, handler.getDelay(Integer.MAX_VALUE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExponentialBackoffDelayHandler_sequence() { | ||
| final RecoveryDelayHandler handler = new ExponentialBackoffDelayHandler(Arrays.asList(1L, 2L)); | ||
| assertEquals(1, handler.getDelay(0)); | ||
| assertEquals(2, handler.getDelay(1)); | ||
| assertEquals(2, handler.getDelay(2)); | ||
| assertEquals(2, handler.getDelay(Integer.MAX_VALUE)); | ||
| } | ||
|
|
||
| @Test(expected=IllegalArgumentException.class) | ||
| public void testExponentialBackoffDelayHandler_sequence_null() { | ||
| new ExponentialBackoffDelayHandler(null); | ||
| } | ||
|
|
||
| @Test(expected=IllegalArgumentException.class) | ||
| public void testExponentialBackoffDelayHandler_sequence_empty() { | ||
| new ExponentialBackoffDelayHandler(Collections.<Long>emptyList()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thoughts on flipping the order of these 2 lines? Should it be delivering the exception before sleeping?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep it the way it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I think at some point we will have to do it. The way it works right now is a bit weird and can complicate additional manual recovery steps.