-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfallback.go
More file actions
39 lines (31 loc) · 991 Bytes
/
Copy pathfallback.go
File metadata and controls
39 lines (31 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"errors"
"net"
"net/http"
)
// FallbackPolicy controls when the proxy serves stale cached content
// instead of returning an error when upstream is unavailable.
type FallbackPolicy struct {
OnConnectionError bool // timeouts, DNS failures, connection refused
On5xx bool // HTTP 5xx responses from upstream
OnAnyError bool // any non-200/304 response
}
// ShouldFallback returns true if the policy allows serving stale content
// for the given error or HTTP status code.
func (p FallbackPolicy) ShouldFallback(err error, statusCode int) bool {
if p.OnAnyError && (err != nil || (statusCode != http.StatusOK && statusCode != http.StatusNotModified)) {
return true
}
if err != nil && p.OnConnectionError && isConnectionError(err) {
return true
}
if statusCode >= 500 && statusCode < 600 && p.On5xx {
return true
}
return false
}
func isConnectionError(err error) bool {
var netErr net.Error
return errors.As(err, &netErr)
}