-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exponential backoff and jitter for retries (#204)
- Loading branch information
Showing
8 changed files
with
202 additions
and
62 deletions.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
loki-client/src/main/java/com/github/loki4j/client/pipeline/Jitter.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,31 @@ | ||
package com.github.loki4j.client.pipeline; | ||
|
||
import java.util.Random; | ||
import java.util.function.Function; | ||
|
||
class Jitter { | ||
|
||
private final Function<Integer, Long> randomSupplier; | ||
private final int maxJitter; | ||
|
||
Jitter(int maxJitter) { | ||
randomSupplier = maxJitter > 0 | ||
? new RandomFunction() | ||
: ignored -> 0L; | ||
this.maxJitter = maxJitter; | ||
} | ||
|
||
long generate() { | ||
return randomSupplier.apply(maxJitter); | ||
} | ||
|
||
private static class RandomFunction implements Function<Integer, Long> { | ||
private final ThreadLocal<Random> random = ThreadLocal.withInitial(Random::new); | ||
|
||
@Override | ||
public Long apply(Integer maxJitter) { | ||
return Long.valueOf(random.get().nextInt(maxJitter)); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.