Skip to content

Commit 72ce78d

Browse files
authored
reverseproxy: SRV dynamic upstream failover (#5832)
* Implement grace period, but probably needs sync * Update cached freshness value * D'oh, actually use the grace period * Fix freshness math
1 parent 8f82047 commit 72ce78d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

modules/caddyhttp/reverseproxy/upstreams.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ type SRVUpstreams struct {
4949
// Results are cached between lookups. Default: 1m
5050
Refresh caddy.Duration `json:"refresh,omitempty"`
5151

52+
// If > 0 and there is an error with the lookup,
53+
// continue to use the cached results for up to
54+
// this long before trying again, (even though they
55+
// are stale) instead of returning an error to the
56+
// client. Default: 0s.
57+
GracePeriod caddy.Duration `json:"grace_period,omitempty"`
58+
5259
// Configures the DNS resolver used to resolve the
5360
// SRV address to SRV records.
5461
Resolver *UpstreamResolver `json:"resolver,omitempty"`
@@ -140,6 +147,12 @@ func (su SRVUpstreams) GetUpstreams(r *http.Request) ([]*Upstream, error) {
140147
// out and an error will be returned alongside the remaining results, if any." Thus, we
141148
// only return an error if no records were also returned.
142149
if len(records) == 0 {
150+
if su.GracePeriod > 0 {
151+
su.logger.Error("SRV lookup failed; using previously cached", zap.Error(err))
152+
cached.freshness = time.Now().Add(time.Duration(su.GracePeriod) - time.Duration(su.Refresh))
153+
srvs[suAddr] = cached
154+
return allNew(cached.upstreams), nil
155+
}
143156
return nil, err
144157
}
145158
su.logger.Warn("SRV records filtered", zap.Error(err))

0 commit comments

Comments
 (0)