-
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.
- Loading branch information
Showing
6 changed files
with
68 additions
and
53 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
29 changes: 22 additions & 7 deletions
29
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 |
---|---|---|
@@ -1,16 +1,31 @@ | ||
package com.github.loki4j.client.pipeline; | ||
|
||
import java.util.Random; | ||
import java.util.function.Supplier; | ||
import java.util.function.Function; | ||
|
||
class Jitter implements Supplier<Long> { | ||
class Jitter { | ||
|
||
private static final int MAX_JITTER = 1000; | ||
private final ThreadLocal<Random> random = ThreadLocal.withInitial(Random::new); | ||
private final Function<Integer, Long> randomSupplier; | ||
private final int maxJitter; | ||
|
||
@Override | ||
public Long get() { | ||
return Long.valueOf(random.get().nextInt(MAX_JITTER)); | ||
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
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