|
| 1 | +/* |
| 2 | + * Copyright 2019 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.internal; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | + |
| 22 | +import com.google.api.client.util.BackOff; |
| 23 | +import com.google.api.client.util.ExponentialBackOff; |
| 24 | +import com.google.api.client.util.Sleeper; |
| 25 | +import com.google.common.annotations.VisibleForTesting; |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +/** |
| 31 | + * Configures when and how HTTP requests should be retried. |
| 32 | + */ |
| 33 | +public final class RetryConfig { |
| 34 | + |
| 35 | + private static final int INITIAL_INTERVAL_MILLIS = 500; |
| 36 | + |
| 37 | + private final List<Integer> retryStatusCodes; |
| 38 | + private final boolean retryOnIOExceptions; |
| 39 | + private final int maxRetries; |
| 40 | + private final Sleeper sleeper; |
| 41 | + private final ExponentialBackOff.Builder backOffBuilder; |
| 42 | + |
| 43 | + private RetryConfig(Builder builder) { |
| 44 | + if (builder.retryStatusCodes != null) { |
| 45 | + this.retryStatusCodes = ImmutableList.copyOf(builder.retryStatusCodes); |
| 46 | + } else { |
| 47 | + this.retryStatusCodes = ImmutableList.of(); |
| 48 | + } |
| 49 | + |
| 50 | + this.retryOnIOExceptions = builder.retryOnIOExceptions; |
| 51 | + checkArgument(builder.maxRetries >= 0, "maxRetries must not be negative"); |
| 52 | + this.maxRetries = builder.maxRetries; |
| 53 | + this.sleeper = checkNotNull(builder.sleeper); |
| 54 | + this.backOffBuilder = new ExponentialBackOff.Builder() |
| 55 | + .setInitialIntervalMillis(INITIAL_INTERVAL_MILLIS) |
| 56 | + .setMaxIntervalMillis(builder.maxIntervalMillis) |
| 57 | + .setMultiplier(builder.backOffMultiplier) |
| 58 | + .setRandomizationFactor(0); |
| 59 | + |
| 60 | + // Force validation of arguments by building the BackOff object |
| 61 | + this.backOffBuilder.build(); |
| 62 | + } |
| 63 | + |
| 64 | + List<Integer> getRetryStatusCodes() { |
| 65 | + return retryStatusCodes; |
| 66 | + } |
| 67 | + |
| 68 | + boolean isRetryOnIOExceptions() { |
| 69 | + return retryOnIOExceptions; |
| 70 | + } |
| 71 | + |
| 72 | + int getMaxRetries() { |
| 73 | + return maxRetries; |
| 74 | + } |
| 75 | + |
| 76 | + int getMaxIntervalMillis() { |
| 77 | + return backOffBuilder.getMaxIntervalMillis(); |
| 78 | + } |
| 79 | + |
| 80 | + double getBackOffMultiplier() { |
| 81 | + return backOffBuilder.getMultiplier(); |
| 82 | + } |
| 83 | + |
| 84 | + Sleeper getSleeper() { |
| 85 | + return sleeper; |
| 86 | + } |
| 87 | + |
| 88 | + BackOff newBackOff() { |
| 89 | + return backOffBuilder.build(); |
| 90 | + } |
| 91 | + |
| 92 | + public static Builder builder() { |
| 93 | + return new Builder(); |
| 94 | + } |
| 95 | + |
| 96 | + public static final class Builder { |
| 97 | + |
| 98 | + private List<Integer> retryStatusCodes; |
| 99 | + private boolean retryOnIOExceptions; |
| 100 | + private int maxRetries; |
| 101 | + private int maxIntervalMillis = (int) TimeUnit.MINUTES.toMillis(2); |
| 102 | + private double backOffMultiplier = 2.0; |
| 103 | + private Sleeper sleeper = Sleeper.DEFAULT; |
| 104 | + |
| 105 | + private Builder() { } |
| 106 | + |
| 107 | + /** |
| 108 | + * Sets a list of HTTP status codes that should be retried. If null or empty, HTTP requests |
| 109 | + * will not be retried as long as they result in some HTTP response message. |
| 110 | + * |
| 111 | + * @param retryStatusCodes A list of status codes. |
| 112 | + * @return This builder. |
| 113 | + */ |
| 114 | + public Builder setRetryStatusCodes(List<Integer> retryStatusCodes) { |
| 115 | + this.retryStatusCodes = retryStatusCodes; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Sets whether requests should be retried on IOExceptions. |
| 121 | + * |
| 122 | + * @param retryOnIOExceptions A boolean indicating whether to retry on IOExceptions. |
| 123 | + * @return This builder. |
| 124 | + */ |
| 125 | + public Builder setRetryOnIOExceptions(boolean retryOnIOExceptions) { |
| 126 | + this.retryOnIOExceptions = retryOnIOExceptions; |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Maximum number of retry attempts for a request. This is the cumulative total for all retries |
| 132 | + * regardless of their cause (I/O errors and HTTP error responses). |
| 133 | + * |
| 134 | + * @param maxRetries A non-negative integer. |
| 135 | + * @return This builder. |
| 136 | + */ |
| 137 | + public Builder setMaxRetries(int maxRetries) { |
| 138 | + this.maxRetries = maxRetries; |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Maximum interval to wait before a request should be retried. Must be at least 500 |
| 144 | + * milliseconds. Defaults to 2 minutes. |
| 145 | + * |
| 146 | + * @param maxIntervalMillis Interval in milliseconds. |
| 147 | + * @return This builder. |
| 148 | + */ |
| 149 | + public Builder setMaxIntervalMillis(int maxIntervalMillis) { |
| 150 | + this.maxIntervalMillis = maxIntervalMillis; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Factor by which the retry interval is multiplied when employing exponential back |
| 156 | + * off to delay consecutive retries of the same request. Must be at least 1. Defaults |
| 157 | + * to 2. |
| 158 | + * |
| 159 | + * @param backOffMultiplier Multiplication factor for exponential back off. |
| 160 | + * @return This builder. |
| 161 | + */ |
| 162 | + public Builder setBackOffMultiplier(double backOffMultiplier) { |
| 163 | + this.backOffMultiplier = backOffMultiplier; |
| 164 | + return this; |
| 165 | + } |
| 166 | + |
| 167 | + @VisibleForTesting |
| 168 | + Builder setSleeper(Sleeper sleeper) { |
| 169 | + this.sleeper = sleeper; |
| 170 | + return this; |
| 171 | + } |
| 172 | + |
| 173 | + public RetryConfig build() { |
| 174 | + return new RetryConfig(this); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments