Commit 3ebb4632 authored by Ricardo Katz's avatar Ricardo Katz Committed by GitHub

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

* Improve gRPC Plugin when backend is not available
Signed-off-by: default avatarRicardo Pchevuzinske Katz <ricardo.katz@serpro.gov.br>

* Improve gRPC Plugin when backend is not available
Signed-off-by: default avatarRicardo Pchevuzinske Katz <ricardo.katz@serpro.gov.br>
parent d3717381
...@@ -3,6 +3,7 @@ package grpc ...@@ -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"
...@@ -36,10 +37,10 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ( ...@@ -36,10 +37,10 @@ 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)
list := g.list() list := g.list()
...@@ -73,6 +74,8 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ( ...@@ -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) ( ...@@ -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 { ...@@ -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")
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment