@@ -82,51 +82,51 @@ func (l *loader) GetConfig(ctx context.Context, appID api.AppID) (out api.AppCon
8282 // Send API request with cached eTag.
8383 // At first, the item.config.ETag() is empty string.
8484 newConfig , err := l .api .GetAppConfig (appID , item .config .ETag ()).Send (ctx )
85- if err != nil {
86- // The config hasn't been modified, extend expiration, return cached version
87- notModifierErr := api.NotModifiedError {}
88- if errors .As (err , & notModifierErr ) {
89- item .ExtendExpiration (now , notModifierErr .MaxAge )
90- return item .config , false , nil
91- }
85+ if err == nil {
86+ // Cache the loaded configuration
87+ item .config = * newConfig
88+ item .ExtendExpiration (now , item .config .MaxAge ())
89+ return item .config , true , nil
90+ }
9291
93- // Only the not found error is expected
94- var apiErr * api.Error
95- if errors .As (err , & apiErr ) && apiErr .StatusCode () != http .StatusNotFound {
96- // Log other errors
97- l .logger .Errorf (ctx , `unable to load configuration for application "%s": %s` , appID , err .Error ())
98-
99- // Keep the proxy running for a limited time in case of an API outage.
100- // The item.expiresAt may be zero, if there is no cached version, then the condition is skipped.
101- if now .Before (item .expiresAt .Add (staleCacheFallbackDuration )) {
102- l .logger .Warnf (ctx , `using stale cache for app "%s": %s` , appID , err .Error ())
103- return item .config , false , nil
104- }
105- }
92+ // The config hasn't been modified, extend expiration, return cached version
93+ notModifierErr := api.NotModifiedError {}
94+ if errors .As (err , & notModifierErr ) {
95+ item .ExtendExpiration (now , notModifierErr .MaxAge )
96+ return item .config , false , nil
97+ }
98+
99+ // Only the not found error is expected
100+ var apiErr * api.Error
101+ if errors .As (err , & apiErr ) && apiErr .StatusCode () != http .StatusNotFound {
102+ // Log other errors
103+ l .logger .Errorf (ctx , `unable to load configuration for application "%s": %s` , appID , err .Error ())
106104
107- // Handle not found error
108- if apiErr != nil && apiErr .StatusCode () == http .StatusNotFound {
109- err = svcErrors .NewResourceNotFoundError ("application" , appID .String (), "stack" ).Wrap (err )
110- return api.AppConfig {}, false , err
105+ // Keep the proxy running for a limited time in case of an API outage.
106+ // The item.expiresAt may be zero, if there is no cached version, then the condition is skipped.
107+ if now .Before (item .expiresAt .Add (staleCacheFallbackDuration )) {
108+ l .logger .Warnf (ctx , `using stale cache for app "%s": %s` , appID , err .Error ())
109+ return item .config , false , nil
111110 }
111+ }
112112
113- // Return the error if:
114- // - It is not found error.
115- // - There is no cached version.
116- // - The staleCacheFallbackDuration has been exceeded.
117- return api.AppConfig {}, false , svcErrors .
118- NewServiceUnavailableError (errors .PrefixErrorf (err ,
119- `unable to load configuration for application "%s"` , appID ,
120- )).
121- WithUserMessage (fmt .Sprintf (
122- `Unable to load configuration for application "%s".` , appID ,
123- ))
113+ // Handle not found error
114+ if apiErr != nil && apiErr .StatusCode () == http .StatusNotFound {
115+ err = svcErrors .NewResourceNotFoundError ("application" , appID .String (), "stack" ).Wrap (err )
116+ return api.AppConfig {}, false , err
124117 }
125118
126- // Cache the loaded configuration
127- item .config = * newConfig
128- item .ExtendExpiration (now , item .config .MaxAge ())
129- return item .config , true , nil
119+ // Return the error if:
120+ // - It is not found error.
121+ // - There is no cached version.
122+ // - The staleCacheFallbackDuration has been exceeded.
123+ return api.AppConfig {}, false , svcErrors .
124+ NewServiceUnavailableError (errors .PrefixErrorf (err ,
125+ `unable to load configuration for application "%s"` , appID ,
126+ )).
127+ WithUserMessage (fmt .Sprintf (
128+ `Unable to load configuration for application "%s".` , appID ,
129+ ))
130130}
131131
132132func (v * cachedAppProxyConfig ) ExtendExpiration (now time.Time , maxAge time.Duration ) {
0 commit comments