This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract BackOff capping into BoundedBackOff. Add BackOff to TCP SSL sender. Add unit tests for BackOff, reformat code, Javadoc. Original pull request: #249
- Loading branch information
Showing
9 changed files
with
230 additions
and
36 deletions.
There are no files selected for viewing
34 changes: 33 additions & 1 deletion
34
src/main/java/biz/paluch/logging/gelf/intern/sender/BackOff.java
This file contains 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 |
---|---|---|
@@ -1,5 +1,37 @@ | ||
package biz.paluch.logging.gelf.intern.sender; | ||
|
||
public interface BackOff { | ||
/** | ||
* Provide a {@link BackOffExecution} that indicates the rate at which an operation should be retried. | ||
* | ||
* <p> | ||
* Users of this interface are expected to use it like this: | ||
* | ||
* <pre class="code"> | ||
* BackOffExecution exec = backOff.start(); | ||
* | ||
* // In the operation recovery/retry loop: | ||
* long waitInterval = exec.nextBackOff(); | ||
* if (waitInterval == BackOffExecution.STOP) { | ||
* // do not retry operation | ||
* } | ||
* else { | ||
* // sleep, e.g. Thread.sleep(waitInterval) | ||
* // retry operation | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* Once the underlying operation has completed successfully, the execution instance can be simply discarded. | ||
* | ||
* @author netudima | ||
* @see BackOffExecution | ||
*/ | ||
interface BackOff { | ||
|
||
/** | ||
* Start a new back off execution. | ||
* | ||
* @return a fresh {@link BackOffExecution} ready to be used | ||
*/ | ||
BackOffExecution start(); | ||
} |
19 changes: 17 additions & 2 deletions
19
src/main/java/biz/paluch/logging/gelf/intern/sender/BackOffExecution.java
This file contains 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 |
---|---|---|
@@ -1,8 +1,23 @@ | ||
package biz.paluch.logging.gelf.intern.sender; | ||
|
||
public interface BackOffExecution { | ||
/** | ||
* Represent a particular back-off execution. | ||
* | ||
* <p> | ||
* Implementations do not need to be thread safe. | ||
* | ||
* @author netudima | ||
* @see BackOff | ||
*/ | ||
interface BackOffExecution { | ||
|
||
/** | ||
* Return value of {@link #nextBackOff()} that indicates that the operation should not be retried. | ||
*/ | ||
long STOP = -1; | ||
|
||
/** | ||
* @return time in ms to wait before a next attempt | ||
*/ | ||
int nextBackOff(); | ||
long nextBackOff(); | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/biz/paluch/logging/gelf/intern/sender/BoundedBackOff.java
This file contains 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,57 @@ | ||
package biz.paluch.logging.gelf.intern.sender; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Bounded {@link BackOff} implementation that return a {@link BackOffExecution#STOP} signal if the total awaited time is | ||
* greater than the specified limit. The max time can exceeded in favor to backoff created by the delegate. Any subsequent | ||
* backoff requests return {@link BackOffExecution#STOP}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
class BoundedBackOff implements BackOff { | ||
|
||
private final BackOff delegate; | ||
|
||
private final long capMs; | ||
|
||
public BoundedBackOff(BackOff delegate, long timeout, TimeUnit timeUnit) { | ||
this.delegate = delegate; | ||
this.capMs = timeUnit.toMillis(timeout); | ||
} | ||
|
||
@Override | ||
public BackOffExecution start() { | ||
return new BoundedBackOffExecution(delegate.start(), capMs); | ||
} | ||
|
||
static class BoundedBackOffExecution implements BackOffExecution { | ||
|
||
private final BackOffExecution delegate; | ||
|
||
private final long capMs; | ||
|
||
private long pastBackOff; | ||
|
||
public BoundedBackOffExecution(BackOffExecution delegate, long capMs) { | ||
this.delegate = delegate; | ||
this.capMs = capMs; | ||
} | ||
|
||
@Override | ||
public long nextBackOff() { | ||
|
||
if (pastBackOff >= capMs) { | ||
return STOP; | ||
} | ||
|
||
long backOff = delegate.nextBackOff(); | ||
|
||
pastBackOff += backOff; | ||
|
||
return backOff; | ||
} | ||
|
||
} | ||
|
||
} |
29 changes: 19 additions & 10 deletions
29
src/main/java/biz/paluch/logging/gelf/intern/sender/ConstantBackOff.java
This file contains 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 |
---|---|---|
@@ -1,19 +1,28 @@ | ||
package biz.paluch.logging.gelf.intern.sender; | ||
|
||
public class ConstantBackOff implements BackOff { | ||
private final int backoffTimeMs; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public ConstantBackOff(int backoffTimeMs) { | ||
this.backoffTimeMs = backoffTimeMs; | ||
/** | ||
* Constant {@link BackOff} implementation. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
class ConstantBackOff implements BackOff, BackOffExecution { | ||
|
||
private final long backoffTimeMs; | ||
|
||
public ConstantBackOff(long backoffTime, TimeUnit timeUnit) { | ||
this.backoffTimeMs = timeUnit.toMillis(backoffTime); | ||
} | ||
|
||
@Override | ||
public long nextBackOff() { | ||
return backoffTimeMs; | ||
} | ||
|
||
@Override | ||
public BackOffExecution start() { | ||
return new BackOffExecution() { | ||
@Override | ||
public int nextBackOff() { | ||
return backoffTimeMs; | ||
} | ||
}; | ||
return this; | ||
} | ||
|
||
} |
This file contains 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 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 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
33 changes: 33 additions & 0 deletions
33
src/test/java/biz/paluch/logging/gelf/intern/sender/BoundedBackOffUnitTests.java
This file contains 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,33 @@ | ||
package biz.paluch.logging.gelf.intern.sender; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link BoundedBackOff}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
class BoundedBackOffUnitTests { | ||
|
||
BoundedBackOff backOff = new BoundedBackOff(new ConstantBackOff(10, TimeUnit.SECONDS), 15, TimeUnit.SECONDS); | ||
|
||
@Test | ||
void shouldPassThruBackoff() { | ||
assertThat(backOff.start().nextBackOff()).isEqualTo(TimeUnit.SECONDS.toMillis(10)); | ||
} | ||
|
||
@Test | ||
void shouldCapBackoff() { | ||
|
||
BackOffExecution backOffExecution = backOff.start(); | ||
|
||
assertThat(backOffExecution.nextBackOff()).isEqualTo(TimeUnit.SECONDS.toMillis(10)); | ||
assertThat(backOffExecution.nextBackOff()).isEqualTo(TimeUnit.SECONDS.toMillis(10)); | ||
assertThat(backOffExecution.nextBackOff()).isEqualTo(BackOffExecution.STOP); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/biz/paluch/logging/gelf/intern/sender/ConstantBackOffUnitTests.java
This file contains 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,24 @@ | ||
package biz.paluch.logging.gelf.intern.sender; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link ConstantBackOff}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
class ConstantBackOffUnitTests { | ||
|
||
@Test | ||
void shouldReturnConstantBackoff() { | ||
|
||
ConstantBackOff backOff = new ConstantBackOff(10, TimeUnit.SECONDS); | ||
|
||
assertThat(backOff.start().nextBackOff()).isEqualTo(TimeUnit.SECONDS.toMillis(10)); | ||
} | ||
|
||
} |