|
| 1 | +package dev.openfeature.contrib.providers.flagd.resolver.common.backoff; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | + |
| 5 | +import java.util.concurrent.ThreadLocalRandom; |
| 6 | + |
| 7 | +/** |
| 8 | + * A service that provides backoff functionality. |
| 9 | + */ |
| 10 | +public class BackoffService { |
| 11 | + public static final int DEFAULT_MAX_JITTER = 0x1 << 8; // 256; Random likes boundaries that are a power of 2 |
| 12 | + |
| 13 | + @Getter |
| 14 | + private final BackoffStrategy strategy; |
| 15 | + |
| 16 | + @Getter |
| 17 | + private final int maxJitter; |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates a new BackoffService with the given strategy and default maximum jitter. |
| 21 | + * The default maximum jitter is 256. |
| 22 | + * |
| 23 | + * @param strategy The backoff strategy to use |
| 24 | + */ |
| 25 | + public BackoffService(BackoffStrategy strategy) { |
| 26 | + this(strategy, DEFAULT_MAX_JITTER); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a new BackoffService with the given strategy and maximum jitter. |
| 31 | + * |
| 32 | + * @param strategy The backoff strategy to use |
| 33 | + * @param maxJitter The maximum jitter value |
| 34 | + */ |
| 35 | + public BackoffService(BackoffStrategy strategy, int maxJitter) { |
| 36 | + this.strategy = strategy; |
| 37 | + this.maxJitter = maxJitter; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns the current backoff time in milliseconds. |
| 42 | + * This backoff time will be used in waitUntilNextAttempt. |
| 43 | + * |
| 44 | + * @return the current backoff time in milliseconds |
| 45 | + */ |
| 46 | + public long getCurrentBackoffMillis() { |
| 47 | + return strategy.getCurrentBackoffMillis(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns a random jitter value between 0 and maxJitter. |
| 52 | + * |
| 53 | + * @return a random jitter value |
| 54 | + */ |
| 55 | + public long getRandomJitter() { |
| 56 | + if (maxJitter == 0) { |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + return ThreadLocalRandom.current().nextInt(maxJitter); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Resets the backoff strategy to its initial state. |
| 65 | + */ |
| 66 | + public void reset() { |
| 67 | + strategy.reset(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns whether the backoff strategy has more attempts left. |
| 72 | + * @return true if the backoff strategy has more attempts left, false otherwise |
| 73 | + */ |
| 74 | + public boolean shouldRetry() { |
| 75 | + return !strategy.isExhausted(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Bolocks the current thread until the next attempt should be made. |
| 80 | + * The time to wait is determined by the backoff strategy and a random jitter. |
| 81 | + * |
| 82 | + * @throws InterruptedException if the thread is interrupted while waiting |
| 83 | + */ |
| 84 | + public void waitUntilNextAttempt() throws InterruptedException { |
| 85 | + long retryDelay = getCurrentBackoffMillis() + getRandomJitter(); |
| 86 | + strategy.nextBackoff(); |
| 87 | + |
| 88 | + Thread.sleep(retryDelay); |
| 89 | + } |
| 90 | +} |
0 commit comments