plugin/forward: Fix incorrect failover counter reset (#8277)

* plugin/forward: Fix incorrect failover counter reset

This PR fixes the isseue where resetting the failover counter caused
retry the same upstreams until timeout instead of stopping after one pass.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

* golint fix

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

---------

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang
2026-07-13 17:34:06 -07:00
committed by GitHub
parent e5053d50ad
commit 4ebf66a74e
2 changed files with 71 additions and 8 deletions

View File

@@ -10,6 +10,7 @@ import (
"errors"
"net"
"net/http"
"slices"
"sync/atomic"
"time"
@@ -126,6 +127,7 @@ func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
}
fails := 0
failoverAttempts := 0
var span, child ot.Span
var upstreamErr error
span = ot.SpanFromContext(ctx)
@@ -236,16 +238,11 @@ func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
// Check if we have a failover Rcode defined, check if we match on the code
tryNext := false
for _, failoverRcode := range f.failoverRcodes {
// if we match, we continue to the next upstream in the list
if failoverRcode == ret.Rcode {
if fails < len(f.proxies) {
tryNext = true
}
}
if slices.Contains(f.failoverRcodes, ret.Rcode) {
failoverAttempts++
tryNext = failoverAttempts < len(f.proxies)
}
if tryNext {
fails++
continue
}