Skip to content

Commit 4597484

Browse files
authored
access-control: fail-open on gate timeout (#1188)
1 parent 577da03 commit 4597484

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

handlers/accesscontrol/access-control.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,17 @@ func (ac *AccessControlHandlersCollection) cachePlaybackAccessControlInfo(playba
349349
}
350350

351351
func (g *GateClient) QueryGate(body []byte) (bool, GateConfig, error) {
352-
353352
gateConfig := GateConfig{
354353
MaxAge: 0,
355354
StaleWhileRevalidate: 0,
356355
RateLimit: 0,
357356
RefreshInterval: 0,
358357
}
359358

360-
req, err := http.NewRequest("POST", g.gateURL, bytes.NewReader(body))
359+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
360+
defer cancel()
361+
362+
req, err := http.NewRequestWithContext(ctx, "POST", g.gateURL, bytes.NewReader(body))
361363
if err != nil {
362364
return false, gateConfig, err
363365
}
@@ -366,10 +368,18 @@ func (g *GateClient) QueryGate(body []byte) (bool, GateConfig, error) {
366368

367369
res, err := g.Client.Do(req)
368370
if err != nil {
371+
// If the timeout is exceeded, simulate a 2XX status code with 2 minute cache expiration
372+
if err == context.DeadlineExceeded {
373+
glog.Infof("queryGate timeout exceeded. Setting default cache expiration to 2 minutes.")
374+
gateConfig.MaxAge = 120
375+
gateConfig.StaleWhileRevalidate = 600
376+
gateConfig.RefreshInterval = 60
377+
return true, gateConfig, nil
378+
}
369379
return false, gateConfig, err
370380
}
371-
372381
defer res.Body.Close()
382+
373383
cc, err := cacheobject.ParseResponseCacheControl(res.Header.Get("Cache-Control"))
374384
if err != nil {
375385
return false, gateConfig, err

0 commit comments

Comments
 (0)