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