From 20eff200b100a6e8b59ffc36b63b93c6e9fbe2a7 Mon Sep 17 00:00:00 2001 From: Kr328 Date: Mon, 20 Jul 2020 21:16:36 +0800 Subject: [PATCH] Fix: dns should put msg to cache while exchangeWithoutCache (#820) --- dns/resolver.go | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/dns/resolver.go b/dns/resolver.go index 0a105d6c..da7f5db8 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -115,24 +115,29 @@ 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] - ret, err, shared := r.group.Do(q.String(), func() (interface{}, error) { + ret, err, shared := r.group.Do(q.String(), func() (result interface{}, err error) { + defer func() { + if err != nil { + return + } + + msg := result.(*D.Msg) + + putMsgToCache(r.lruCache, q.String(), msg) + if r.mapping || r.fakeip { + ips := r.msgToIP(msg) + for _, ip := range ips { + putMsgToCache(r.lruCache, ip.String(), msg) + } + } + }() + 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) - for _, ip := range ips { - putMsgToCache(r.lruCache, ip.String(), msg) - } - } - return msg, nil + return r.batchExchange(r.main, m) }) if err == nil {