|
10 | 10 | import java.util.concurrent.atomic.AtomicBoolean; |
11 | 11 |
|
12 | 12 | class ConfigFetcher implements Closeable { |
| 13 | + |
| 14 | + private static final long RETRY_DELAY_MS = 50; |
| 15 | + |
| 16 | + private static final long EVICT_ALL_THRESHOLD_NS = 30_000_000_000L; // 30 seconds in nanoseconds |
| 17 | + |
13 | 18 | private final AtomicBoolean isClosed = new AtomicBoolean(false); |
14 | 19 | private final ConfigCatLogger logger; |
15 | 20 | private final OkHttpClient httpClient; |
16 | 21 | private final String mode; |
17 | 22 |
|
| 23 | + private long lastEvictAllTimestamp = Long.MIN_VALUE; |
| 24 | + |
18 | 25 | private final String sdkKey; |
19 | 26 | private final boolean urlIsCustom; |
20 | 27 |
|
@@ -45,7 +52,7 @@ public CompletableFuture<FetchResponse> fetchAsync(String eTag) { |
45 | 52 | } |
46 | 53 |
|
47 | 54 | private CompletableFuture<FetchResponse> executeFetchAsync(int executionCount, String eTag) { |
48 | | - return this.getResponseAsync(eTag).thenComposeAsync(fetchResponse -> { |
| 55 | + return this.fetchWithRetryAsync(eTag).thenComposeAsync(fetchResponse -> { |
49 | 56 | if (!fetchResponse.isFetched()) { |
50 | 57 | return CompletableFuture.completedFuture(fetchResponse); |
51 | 58 | } |
@@ -97,64 +104,101 @@ private CompletableFuture<FetchResponse> getResponseAsync(final String eTag) { |
97 | 104 | this.httpClient.newCall(request).enqueue(new Callback() { |
98 | 105 | @Override |
99 | 106 | public void onFailure(@NotNull Call call, @NotNull IOException e) { |
100 | | - int logEventId = 1103; |
101 | | - Object message = ConfigCatLogMessages.getFetchFailedDueToUnexpectedError(null); |
102 | | - if (!isClosed.get()) { |
103 | | - if (e instanceof SocketTimeoutException) { |
104 | | - logEventId = 1102; |
105 | | - message = ConfigCatLogMessages.getFetchFailedDueToRequestTimeout(httpClient.connectTimeoutMillis(), httpClient.readTimeoutMillis(), httpClient.writeTimeoutMillis(), null); |
| 107 | + FetchResponse fetchResponse = null; |
| 108 | + try{ |
| 109 | + int logEventId = 1103; |
| 110 | + Object message = ConfigCatLogMessages.getFetchFailedDueToUnexpectedError(null); |
| 111 | + if (!isClosed.get()) { |
| 112 | + if (e instanceof SocketTimeoutException) { |
| 113 | + logEventId = 1102; |
| 114 | + message = ConfigCatLogMessages.getFetchFailedDueToRequestTimeout(httpClient.connectTimeoutMillis(), httpClient.readTimeoutMillis(), httpClient.writeTimeoutMillis(), null); |
| 115 | + } |
| 116 | + logger.error(logEventId, message, e); |
| 117 | + } |
| 118 | + fetchResponse = FetchResponse.failed(message, false, null, true); |
| 119 | + } finally { |
| 120 | + if(fetchResponse == null) { |
| 121 | + FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getFetchFailedDueToUnexpectedError(null); |
| 122 | + fetchResponse = FetchResponse.failed(formattableLogMessage,false, null, false); |
106 | 123 | } |
107 | | - logger.error(logEventId, message, e); |
| 124 | + future.complete(fetchResponse); |
108 | 125 | } |
109 | | - future.complete(FetchResponse.failed(message, false, null)); |
110 | 126 | } |
111 | 127 |
|
112 | 128 | @Override |
113 | 129 | public void onResponse(@NotNull Call call, @NotNull Response response) { |
114 | 130 | String cfRayId = null; |
| 131 | + FetchResponse fetchResponse = null; |
115 | 132 | try (ResponseBody body = response.body()) { |
116 | 133 | cfRayId = response.header("CF-RAY"); |
117 | 134 | if (response.code() == 200) { |
118 | 135 | String content = body != null ? body.string() : null; |
119 | 136 | String eTag = response.header("ETag"); |
120 | 137 | Result<Config> result = deserializeConfig(content, cfRayId); |
121 | 138 | if (result.error() != null) { |
122 | | - future.complete(FetchResponse.failed(result.error(), false, cfRayId)); |
123 | | - return; |
| 139 | + fetchResponse = FetchResponse.failed(result.error(), false, cfRayId, false); |
| 140 | + } else { |
| 141 | + fetchResponse = FetchResponse.fetched(new Entry(result.value(), eTag, content, System.currentTimeMillis()), cfRayId); |
| 142 | + logger.debug("Fetch was successful: new config fetched."); |
124 | 143 | } |
125 | | - logger.debug("Fetch was successful: new config fetched."); |
126 | | - future.complete(FetchResponse.fetched(new Entry(result.value(), eTag, content, System.currentTimeMillis()), cfRayId)); |
127 | 144 | } else if (response.code() == 304) { |
| 145 | + fetchResponse = FetchResponse.notModified(cfRayId); |
128 | 146 | if(cfRayId != null) { |
129 | 147 | logger.debug(String.format("Fetch was successful: config not modified. %s", ConfigCatLogMessages.getCFRayIdPostFix(cfRayId))); |
130 | 148 | } else { |
131 | 149 | logger.debug("Fetch was successful: config not modified."); |
132 | 150 | } |
133 | | - future.complete(FetchResponse.notModified(cfRayId)); |
134 | 151 | } else if (response.code() == 403 || response.code() == 404) { |
135 | 152 | FormattableLogMessage message = ConfigCatLogMessages.getFetchFailedDueToInvalidSDKKey(cfRayId); |
| 153 | + fetchResponse = FetchResponse.failed(message, true, cfRayId, false); |
136 | 154 | logger.error(1100, message); |
137 | | - future.complete(FetchResponse.failed(message, true, cfRayId)); |
138 | 155 | } else { |
139 | 156 | FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getFetchFailedDueToUnexpectedHttpResponse(response.code(), response.message(), cfRayId); |
| 157 | + fetchResponse = FetchResponse.failed(formattableLogMessage, false, cfRayId, true); |
140 | 158 | logger.error(1101, formattableLogMessage); |
141 | | - future.complete(FetchResponse.failed(formattableLogMessage, false, cfRayId)); |
142 | 159 | } |
143 | 160 | } catch (SocketTimeoutException e) { |
144 | 161 | FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getFetchFailedDueToRequestTimeout(httpClient.connectTimeoutMillis(), httpClient.readTimeoutMillis(), httpClient.writeTimeoutMillis(), cfRayId); |
| 162 | + fetchResponse = FetchResponse.failed(formattableLogMessage, false, cfRayId, true); |
145 | 163 | logger.error(1102, formattableLogMessage, e); |
146 | | - future.complete(FetchResponse.failed(formattableLogMessage, false, cfRayId)); |
147 | 164 | } catch (Exception e) { |
148 | 165 | FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getFetchFailedDueToUnexpectedError(cfRayId); |
| 166 | + fetchResponse = FetchResponse.failed(formattableLogMessage, false, cfRayId, true); |
149 | 167 | logger.error(1103, formattableLogMessage, e); |
150 | | - future.complete(FetchResponse.failed(formattableLogMessage, false, cfRayId)); |
| 168 | + } finally { |
| 169 | + if(fetchResponse == null) { |
| 170 | + FormattableLogMessage formattableLogMessage = ConfigCatLogMessages.getFetchFailedDueToUnexpectedError(cfRayId); |
| 171 | + fetchResponse = FetchResponse.failed(formattableLogMessage,false, cfRayId, false); |
| 172 | + } |
| 173 | + future.complete(fetchResponse); |
151 | 174 | } |
152 | 175 | } |
153 | 176 | }); |
154 | 177 |
|
155 | 178 | return future; |
156 | 179 | } |
157 | 180 |
|
| 181 | + private CompletableFuture<FetchResponse> fetchWithRetryAsync(final String eTag) { |
| 182 | + return this.getResponseAsync(eTag).thenComposeAsync(response -> { |
| 183 | + if (response.shouldRetry()) { |
| 184 | + try { |
| 185 | + long now = System.nanoTime(); |
| 186 | + if (lastEvictAllTimestamp == Long.MIN_VALUE || (now - lastEvictAllTimestamp) >= EVICT_ALL_THRESHOLD_NS) { |
| 187 | + this.httpClient.connectionPool().evictAll(); |
| 188 | + lastEvictAllTimestamp = now; |
| 189 | + } |
| 190 | + Thread.sleep(RETRY_DELAY_MS); |
| 191 | + return this.getResponseAsync(eTag); |
| 192 | + } catch (InterruptedException e) { |
| 193 | + this.logger.error(0, "Thread interrupted.", e); |
| 194 | + Thread.currentThread().interrupt(); |
| 195 | + return CompletableFuture.completedFuture(response); |
| 196 | + } |
| 197 | + } |
| 198 | + return CompletableFuture.completedFuture(response); |
| 199 | + }); |
| 200 | + } |
| 201 | + |
158 | 202 | @Override |
159 | 203 | public void close() throws IOException { |
160 | 204 | if (!this.isClosed.compareAndSet(false, true)) { |
|
0 commit comments