Skip to content

Commit 43d4812

Browse files
committed
Generalize delay calculation checks for positive / negative case (though only positive is used)
1 parent 1ce9440 commit 43d4812

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/main/java/net/tascalate/concurrent/delays/ExponentialDelayPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Duration delay(RetryContext context) {
5555
double factor = Math.pow(multiplier, context.getRetryCount());
5656
return DurationCalcs.safeTransform(
5757
initialDelay,
58-
(amount, dimIdx) -> DurationCalcs.toBoolean((double)Long.MAX_VALUE / amount > factor),
58+
(amount, dimIdx) -> DurationCalcs.toBoolean((double)Long.MAX_VALUE / Math.abs(amount) > Math.abs(factor)),
5959
(amount, dimIdx) -> (long)(amount * factor)
6060
);
6161
}

src/main/java/net/tascalate/concurrent/delays/ProportionalRandomDelayPolicy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public ProportionalRandomDelayPolicy(DelayPolicy target, double multiplier, Rand
5959
}
6060

6161
@Override
62-
long addRandomJitter(long initialDelay, double randomizer, int dimIdx) {
62+
long addRandomJitter(long amount, double randomizer, int dimIdx) {
6363
double randomMultiplier = (1 - 2 * randomizer) * multiplier;
64-
return Math.max(0, (long) (initialDelay * (1 + randomMultiplier)));
64+
return Math.max(0, (long) (amount * (1 + randomMultiplier)));
6565
}
6666

6767
@Override
68-
boolean checkBounds(long initialDelay, double randomizer, int dimIdx) {
68+
boolean checkBounds(long amount, double randomizer, int dimIdx) {
6969
double randomMultiplier = (1 - 2 * randomizer) * multiplier;
70-
return (double)Long.MAX_VALUE / initialDelay > 1 + randomMultiplier;
70+
return (double)Long.MAX_VALUE / Math.abs(amount) > Math.abs(1 + randomMultiplier);
7171
}
7272
}

src/main/java/net/tascalate/concurrent/delays/RandomDelayPolicy.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,16 @@ private RandomDelayPolicy(DelayPolicy target, Supplier<Random> randomSource) {
4848

4949
@Override
5050
public Duration delay(RetryContext context) {
51-
Duration initialDelay = target.delay(context);
5251
double randomizer = random().nextDouble();
5352
return DurationCalcs.safeTransform(
54-
initialDelay,
53+
target.delay(context),
5554
(amount, dimIdx) -> DurationCalcs.toBoolean(checkBounds(amount, randomizer, (int)dimIdx)),
5655
(amount, dimIdx) -> addRandomJitter(amount, randomizer, (int)dimIdx)
5756
);
5857
}
5958

60-
abstract long addRandomJitter(long initialDelay, double randomizer, int dimIdx);
61-
abstract boolean checkBounds(long initialDelay, double randomizer, int dimIdx);
59+
abstract long addRandomJitter(long amount, double randomizer, int dimIdx);
60+
abstract boolean checkBounds(long amount, double randomizer, int dimIdx);
6261

6362
protected Random random() {
6463
return randomSource.get();

src/main/java/net/tascalate/concurrent/delays/UniformRandomDelayPolicy.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,20 @@ public UniformRandomDelayPolicy(DelayPolicy target, Duration range, Random rando
6868
}
6969

7070
@Override
71-
long addRandomJitter(long initialDelay, double randomizer, int dimIdx) {
71+
long addRandomJitter(long amount, double randomizer, int dimIdx) {
7272
long rangeNormalized = DurationCalcs.safeExtractAmount(range, dimIdx);
7373
double uniformRandom = (1 - randomizer * 2) * rangeNormalized;
74-
return Math.max(0, (long) (initialDelay + uniformRandom));
74+
return Math.max(0, (long) (amount + uniformRandom));
7575
}
7676

7777
@Override
78-
boolean checkBounds(long initialDelay, double randomizer, int dimIdx) {
78+
boolean checkBounds(long amount, double randomizer, int dimIdx) {
7979
long rangeNormalized = DurationCalcs.safeExtractAmount(range, dimIdx);
8080
double uniformRandom = (1 - randomizer * 2) * rangeNormalized;
81-
return Long.MAX_VALUE - initialDelay > uniformRandom;
81+
if (uniformRandom < 0) {
82+
return -Long.MAX_VALUE + Math.abs(amount) < uniformRandom;
83+
} else {
84+
return Long.MAX_VALUE - Math.abs(amount) > uniformRandom;
85+
}
8286
}
8387
}

0 commit comments

Comments
 (0)