@@ -38,6 +38,7 @@ type Config struct {
3838 API string `yaml:"api"`
3939 APITimeoutMs int `yaml:"apiTimeoutMs"`
4040 IgnoreAPITimeout bool `yaml:"ignoreApiTimeout"`
41+ IgnoreAPIFailures bool `yaml:"ignoreApiFailures"`
4142 IPGeolocationHTTPHeaderField string `yaml:"ipGeolocationHttpHeaderField"`
4243 XForwardedForReverseProxy bool `yaml:"xForwardedForReverseProxy"`
4344 CacheSize int `yaml:"cacheSize"`
@@ -74,6 +75,7 @@ type GeoBlock struct {
7475 apiURI string
7576 apiTimeoutMs int
7677 ignoreAPITimeout bool
78+ ignoreAPIFailures bool
7779 iPGeolocationHTTPHeaderField string
7880 xForwardedForReverseProxy bool
7981 forceMonthlyUpdate bool
@@ -162,6 +164,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
162164 apiURI : config .API ,
163165 apiTimeoutMs : config .APITimeoutMs ,
164166 ignoreAPITimeout : config .IgnoreAPITimeout ,
167+ ignoreAPIFailures : config .IgnoreAPIFailures ,
165168 iPGeolocationHTTPHeaderField : config .IPGeolocationHTTPHeaderField ,
166169 xForwardedForReverseProxy : config .XForwardedForReverseProxy ,
167170 forceMonthlyUpdate : config .ForceMonthlyUpdate ,
@@ -270,19 +273,26 @@ func (a *GeoBlock) allowDenyIPAddress(requestIPAddr *net.IP, req *http.Request)
270273
271274func (a * GeoBlock ) allowDenyCachedRequestIP (requestIPAddr * net.IP , req * http.Request ) (bool , string ) {
272275 ipAddressString := requestIPAddr .String ()
273- cacheEntry , ok := a .database .Get (ipAddressString )
276+ cacheEntry , cacheHit := a .database .Get (ipAddressString )
274277
275278 var entry ipEntry
276279 var err error
277- if ! ok {
280+ if ! cacheHit {
278281 entry , err = a .createNewIPEntry (req , ipAddressString )
279- if err != nil && ! (os .IsTimeout (err ) && a .ignoreAPITimeout ) {
282+ if err != nil {
283+ if a .ignoreAPIFailures {
284+ a .infoLogger .Printf ("%s: request allowed [%s] due to API failure" , a .name , requestIPAddr )
285+ return true , ""
286+ }
287+
288+ if os .IsTimeout (err ) && a .ignoreAPITimeout {
289+ a .infoLogger .Printf ("%s: request allowed [%s] due to API timeout" , a .name , requestIPAddr )
290+ // TODO: this was previously an immediate response to the client
291+ return true , ""
292+ }
293+
280294 a .infoLogger .Printf ("%s: request denied [%s] due to error: %s" , a .name , requestIPAddr , err )
281295 return false , ""
282- } else if os .IsTimeout (err ) && a .ignoreAPITimeout {
283- a .infoLogger .Printf ("%s: request allowed [%s] due to API timeout" , a .name , requestIPAddr )
284- // TODO: this was previously an immediate response to the client
285- return true , ""
286296 }
287297 } else {
288298 entry = cacheEntry .(ipEntry )
@@ -296,6 +306,10 @@ func (a *GeoBlock) allowDenyCachedRequestIP(requestIPAddr *net.IP, req *http.Req
296306 if time .Since (entry .Timestamp ).Hours () >= numberOfHoursInMonth && a .forceMonthlyUpdate {
297307 entry , err = a .createNewIPEntry (req , ipAddressString )
298308 if err != nil {
309+ if a .ignoreAPIFailures {
310+ a .infoLogger .Printf ("%s: request allowed [%s] due to API failure" , a .name , requestIPAddr )
311+ return true , ""
312+ }
299313 a .infoLogger .Printf ("%s: request denied [%s] due to error: %s" , a .name , requestIPAddr , err )
300314 return false , ""
301315 }
@@ -464,6 +478,10 @@ func (a *GeoBlock) callGeoJS(ipAddress string) (string, error) {
464478 return "" , err
465479 }
466480
481+ if res .StatusCode != http .StatusOK {
482+ return "" , fmt .Errorf ("API response status code: %d" , res .StatusCode )
483+ }
484+
467485 if res .Body != nil {
468486 defer res .Body .Close ()
469487 }
0 commit comments