From 7e84fc502a7099068e92f931e295446e2ac7bb53 Mon Sep 17 00:00:00 2001 From: dingjun Date: Tue, 30 Jun 2020 19:51:09 +0800 Subject: [PATCH] filter some upstream errors --- upstream.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/upstream.go b/upstream.go index 3c716dc..be63bdf 100644 --- a/upstream.go +++ b/upstream.go @@ -36,12 +36,23 @@ func getResponseFromUpstream(msg *dns.Msg, upstreams []*url.URL) (*dns.Msg, erro }(up) } - select { - case <-ctx.Done(): - return nil, errors.New("time out") - case m := <-resch: - return m, nil + var errmsg *dns.Msg + + for i := 0; i < len(upstreams); i++ { + select { + case <-ctx.Done(): + return nil, errors.New("time out") + case m := <-resch: + if m.MsgHdr.Rcode == dns.RcodeSuccess { + return m, nil + } + errmsg = m + } + } + if errmsg != nil { + return errmsg, nil } + return nil, errors.New("empty result") } func queryUpstream(msg *dns.Msg, upstream *url.URL) (*dns.Msg, error) {