|
| 1 | +package com.strategyobject.substrateclient.transport.ws; |
| 2 | + |
| 3 | +import com.google.common.base.Preconditions; |
| 4 | +import lombok.*; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | + |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | +import java.util.concurrent.atomic.LongAdder; |
| 9 | + |
| 10 | +/** |
| 11 | + * Represents an exponential backoff retry policy |
| 12 | + */ |
| 13 | +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) |
| 14 | +@Getter |
| 15 | +@Slf4j |
| 16 | +public class ExponentialBackoffReconnectionPolicy implements ReconnectionPolicy<LongAdder> { |
| 17 | + /** |
| 18 | + * Max number of attempts |
| 19 | + */ |
| 20 | + private final long maxAttempts; |
| 21 | + /** |
| 22 | + * Initial delay, the time to delay execution unit |
| 23 | + */ |
| 24 | + private final long delay; |
| 25 | + /** |
| 26 | + * The time unit of the delay parameter |
| 27 | + */ |
| 28 | + private final TimeUnit unit; |
| 29 | + /** |
| 30 | + * Max delay |
| 31 | + */ |
| 32 | + private final long maxDelay; |
| 33 | + /** |
| 34 | + * A multiplier that's applied to delay after every attempt |
| 35 | + */ |
| 36 | + private final double factor; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param context contains a reason of disconnection |
| 40 | + * @return a schedule for the next reconnection |
| 41 | + */ |
| 42 | + @Override |
| 43 | + public @NonNull Delay getNextDelay(@NonNull ReconnectionContext<LongAdder> context) { |
| 44 | + try { |
| 45 | + if (context.getPolicyContext().longValue() >= maxAttempts) { |
| 46 | + log.info("Provider won't reconnect more."); |
| 47 | + |
| 48 | + return Delay.NEVER; |
| 49 | + } |
| 50 | + |
| 51 | + var nextDelay = delay * Math.pow(factor, context.getPolicyContext().longValue()); |
| 52 | + nextDelay = Math.min(nextDelay, maxDelay); |
| 53 | + |
| 54 | + log.info("Provider will try to reconnect after: {} {}", nextDelay, unit); |
| 55 | + return Delay.of((long) nextDelay, unit); |
| 56 | + } finally { |
| 57 | + context.getPolicyContext().increment(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the counter of attempts |
| 63 | + */ |
| 64 | + @Override |
| 65 | + public LongAdder initContext() { |
| 66 | + return new LongAdder(); |
| 67 | + } |
| 68 | + |
| 69 | + public static Builder builder() { |
| 70 | + return new Builder(); |
| 71 | + } |
| 72 | + |
| 73 | + public static class Builder { |
| 74 | + private long delay = 15; |
| 75 | + private TimeUnit unit = TimeUnit.SECONDS; |
| 76 | + private long maxDelay = 150; |
| 77 | + private long maxAttempts = 10; |
| 78 | + private double factor = 2; |
| 79 | + |
| 80 | + Builder() { |
| 81 | + } |
| 82 | + |
| 83 | + public Builder retryAfter(long delay, TimeUnit unit) { |
| 84 | + Preconditions.checkArgument(delay >= 0); |
| 85 | + |
| 86 | + this.delay = delay; |
| 87 | + this.unit = unit; |
| 88 | + |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + public Builder withFactor(double factor) { |
| 93 | + Preconditions.checkArgument(factor > 0); |
| 94 | + |
| 95 | + this.factor = factor; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public Builder withMaxDelay(long maxDelay) { |
| 100 | + Preconditions.checkArgument(maxDelay >= 0); |
| 101 | + |
| 102 | + this.maxDelay = maxDelay; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public Builder notMoreThan(long maxAttempts) { |
| 107 | + Preconditions.checkArgument(maxAttempts >= 0); |
| 108 | + |
| 109 | + this.maxAttempts = maxAttempts; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public ExponentialBackoffReconnectionPolicy build() { |
| 114 | + return new ExponentialBackoffReconnectionPolicy( |
| 115 | + this.maxAttempts, |
| 116 | + this.delay, |
| 117 | + this.unit, |
| 118 | + this.maxDelay, |
| 119 | + this.factor |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments