77import static java .lang .System .Logger .Level .WARNING ;
88
99import java .io .StringReader ;
10+ import java .lang .System .Logger .Level ;
11+ import java .net .ConnectException ;
1012import java .time .Instant ;
1113import java .util .Map ;
14+ import java .util .concurrent .atomic .AtomicInteger ;
1215import java .util .concurrent .atomic .AtomicReference ;
1316import java .util .concurrent .locks .LockSupport ;
1417import 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 );
0 commit comments