From 02c7fd8d70e09c91f9c07b737fc68fcdd6bf68e2 Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 17 Jul 2020 17:34:40 +0800 Subject: [PATCH] Fix: write msg cache multiple times (#812) Co-authored-by: john.xu --- dns/resolver.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/dns/resolver.go b/dns/resolver.go index 52a08c0d..0a105d6c 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -115,11 +115,16 @@ func (r *Resolver) Exchange(m *D.Msg) (msg *D.Msg, err error) { func (r *Resolver) exchangeWithoutCache(m *D.Msg) (msg *D.Msg, err error) { q := m.Question[0] - defer func() { - if msg == nil { - return + ret, err, shared := r.group.Do(q.String(), func() (interface{}, error) { + isIPReq := isIPRequest(q) + if isIPReq { + return r.fallbackExchange(m) } + msg, err := r.batchExchange(r.main, m) + if err != nil { + return nil, err + } putMsgToCache(r.lruCache, q.String(), msg) if r.mapping || r.fakeip { ips := r.msgToIP(msg) @@ -127,15 +132,7 @@ func (r *Resolver) exchangeWithoutCache(m *D.Msg) (msg *D.Msg, err error) { putMsgToCache(r.lruCache, ip.String(), msg) } } - }() - - ret, err, shared := r.group.Do(q.String(), func() (interface{}, error) { - isIPReq := isIPRequest(q) - if isIPReq { - return r.fallbackExchange(m) - } - - return r.batchExchange(r.main, m) + return msg, nil }) if err == nil {