Skip to content

Commit 563071b

Browse files
committed
[AWS AppConfig] Fix #228 - Expect ConnectException on shutdown
- Don't log ConnectException on first occurrence as it's expected during shutdown so treat this as expected. - Reduce logging retries at startup to DEBUG to reduce log "noise"
1 parent 5204586 commit 563071b

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

avaje-aws-appconfig/src/main/java/io/avaje/config/appconfig/AppConfigFetcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.avaje.config.appconfig;
22

3+
import java.net.ConnectException;
34
import java.net.URI;
45

56
interface AppConfigFetcher {
@@ -8,7 +9,7 @@ static AppConfigFetcher.Builder builder() {
89
return new DAppConfigFetcher.Builder();
910
}
1011

11-
Result fetch() throws FetchException;
12+
Result fetch() throws ConnectException, FetchException;
1213

1314
URI uri();
1415

avaje-aws-appconfig/src/main/java/io/avaje/config/appconfig/AppConfigPlugin.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import static java.lang.System.Logger.Level.WARNING;
88

99
import java.io.StringReader;
10+
import java.lang.System.Logger.Level;
11+
import java.net.ConnectException;
1012
import java.time.Instant;
1113
import java.util.Map;
14+
import java.util.concurrent.atomic.AtomicInteger;
1215
import java.util.concurrent.atomic.AtomicReference;
1316
import java.util.concurrent.locks.LockSupport;
1417
import java.util.concurrent.locks.ReentrantLock;
@@ -57,6 +60,7 @@ static final class Loader {
5760
private final AppConfigFetcher fetcher;
5861
private final ConfigParser yamlParser;
5962
private final ConfigParser propertiesParser;
63+
private final AtomicInteger connectErrorCount = new AtomicInteger();
6064
private final ReentrantLock lock = new ReentrantLock();
6165
private final AtomicReference<Instant> validUntil;
6266
private final long nextRefreshSeconds;
@@ -98,14 +102,15 @@ static final class Loader {
98102
int initialLoad() {
99103
lock.lock();
100104
try {
101-
AppConfigFetcher.FetchException lastAttempt = null;
105+
Exception lastAttempt = null;
102106
for (int i = 1; i < 11; i++) {
103107
try {
104108
loadAndPublish();
105109
return i;
106-
} catch (AppConfigFetcher.FetchException e) {
110+
} catch (Exception e) {
111+
// often seeing this with apps that start quickly (and AppConfig sidecar not up yet)
107112
lastAttempt = e;
108-
log.log(INFO, "retrying, load attempt {0} got {1}", i, e.getMessage());
113+
log.log(DEBUG, "retrying, load attempt {0} got {1}", i, e.getMessage());
109114
LockSupport.parkNanos(250_000_000); // 250 millis
110115
}
111116
}
@@ -135,6 +140,11 @@ private void performReload() {
135140
}
136141
loadAndPublish();
137142

143+
} catch (ConnectException e) {
144+
// expected during shutdown when AppConfig sidecar shuts down before the app
145+
int errCount = connectErrorCount.incrementAndGet();
146+
Level level = errCount > 1 ? WARNING : INFO;
147+
log.log(level, "Failed to fetch from AwsAppConfig - likely shutdown in progress");
138148
} catch (Exception e) {
139149
log.log(ERROR, "Error fetching or processing AwsAppConfig", e);
140150
} finally {
@@ -145,7 +155,7 @@ private void performReload() {
145155
/**
146156
* Load and publish the configuration from AWS AppConfig.
147157
*/
148-
private void loadAndPublish() throws AppConfigFetcher.FetchException {
158+
private void loadAndPublish() throws AppConfigFetcher.FetchException, ConnectException {
149159
AppConfigFetcher.Result result = fetcher.fetch();
150160
if (currentVersion.equals(result.version())) {
151161
log.log(TRACE, "AwsAppConfig unchanged, version {0}", currentVersion);

avaje-aws-appconfig/src/main/java/io/avaje/config/appconfig/DAppConfigFetcher.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.avaje.config.appconfig;
22

3-
import java.io.IOException;
3+
import java.net.ConnectException;
44
import java.net.URI;
55
import java.net.http.HttpClient;
66
import java.net.http.HttpRequest;
@@ -23,7 +23,7 @@ public URI uri() {
2323
}
2424

2525
@Override
26-
public AppConfigFetcher.Result fetch() throws FetchException {
26+
public AppConfigFetcher.Result fetch() throws ConnectException, FetchException {
2727
HttpRequest request = HttpRequest.newBuilder()
2828
.uri(uri)
2929
.GET()
@@ -36,7 +36,9 @@ public AppConfigFetcher.Result fetch() throws FetchException {
3636
String body = res.body();
3737
return new DResult(version, contentType, body);
3838

39-
} catch (IOException | InterruptedException e) {
39+
} catch (ConnectException e) {
40+
throw e; // expected on shutdown
41+
} catch (Exception e) {
4042
throw new FetchException(e);
4143
}
4244
}

0 commit comments

Comments
 (0)