Correct cache behavior

This commit is contained in:
风扇滑翔翼 2024-09-15 07:37:56 +00:00 committed by GitHub
parent d2e79c281d
commit 83cffab45f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -52,18 +52,19 @@ var (
) )
func QueryRecord(domain string, server string) (string, error) { func QueryRecord(domain string, server string) (string, error) {
mutex.RLock()
defer mutex.RUnlock()
rec, found := dnsCache[domain] rec, found := dnsCache[domain]
if found && rec.expire.After(time.Now()) { if found && rec.expire.After(time.Now()) {
return "", nil return rec.record, nil
} }
mutex.Lock()
defer mutex.Unlock()
record, err := dohQuery(server, domain) record, err := dohQuery(server, domain)
if err != nil { if err != nil {
return "", err return "", err
} }
rec.record = record rec.record = record
rec.expire = time.Now().Add(time.Second * 600) rec.expire = time.Now().Add(time.Second * 600)
dnsCache[domain] = rec
return record, nil return record, nil
} }