Improve gRPC Plugin when backend is not available (#3966)

* Improve gRPC Plugin when backend is not available

Signed-off-by: Ricardo Pchevuzinske Katz <ricardo.katz@serpro.gov.br>

* Improve gRPC Plugin when backend is not available

Signed-off-by: Ricardo Pchevuzinske Katz <ricardo.katz@serpro.gov.br>
This commit is contained in:
Ricardo Katz 2020-07-15 14:59:45 -03:00 committed by GitHub
parent d371738149
commit 3ebb46320a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ package grpc
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"time" "time"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
@ -38,7 +39,7 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
var ( var (
span, child ot.Span span, child ot.Span
ret *dns.Msg ret *dns.Msg
err error upstreamErr, err error
i int i int
) )
span = ot.SpanFromContext(ctx) span = ot.SpanFromContext(ctx)
@ -73,6 +74,8 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
child.Finish() child.Finish()
} }
upstreamErr = err
// Check if the reply is correct; if not return FormErr. // Check if the reply is correct; if not return FormErr.
if !state.Match(ret) { if !state.Match(ret) {
debug.Hexdumpf(ret, "Wrong reply for id: %d, %s %d", ret.Id, state.QName(), state.QType()) debug.Hexdumpf(ret, "Wrong reply for id: %d, %s %d", ret.Id, state.QName(), state.QType())
@ -87,7 +90,11 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
return 0, nil return 0, nil
} }
return 0, nil if upstreamErr != nil {
return dns.RcodeServerFailure, upstreamErr
}
return dns.RcodeServerFailure, ErrNoHealthy
} }
// NewGRPC returns a new GRPC. // NewGRPC returns a new GRPC.
@ -129,3 +136,8 @@ func (g *GRPC) isAllowedDomain(name string) bool {
func (g *GRPC) list() []*Proxy { return g.p.List(g.proxies) } func (g *GRPC) list() []*Proxy { return g.p.List(g.proxies) }
const defaultTimeout = 5 * time.Second const defaultTimeout = 5 * time.Second
var (
// ErrNoHealthy means no healthy proxies left.
ErrNoHealthy = errors.New("no healthy gRPC proxies")
)