feat(dns): Implement optimistic caching

This commit is contained in:
Meo597
2025-10-19 02:24:04 +08:00
parent b69a376aa1
commit 79787a0176
14 changed files with 352 additions and 194 deletions
+5 -9
View File
@@ -3,6 +3,7 @@ package dns
import (
"context"
"encoding/binary"
"math"
"strings"
"time"
@@ -38,19 +39,14 @@ type IPRecord struct {
RawHeader *dnsmessage.Header
}
func (r *IPRecord) getIPs() ([]net.IP, uint32, error) {
func (r *IPRecord) getIPs() ([]net.IP, int32, error) {
if r == nil {
return nil, 0, errRecordNotFound
}
untilExpire := time.Until(r.Expire).Seconds()
if untilExpire <= 0 {
return nil, 0, errRecordNotFound
}
ttl := uint32(untilExpire) + 1
if ttl == 1 {
r.Expire = time.Now().Add(time.Second) // To ensure that two consecutive requests get the same result
}
untilExpire := time.Until(r.Expire).Seconds()
ttl := int32(math.Ceil(untilExpire))
if r.RCode != dnsmessage.RCodeSuccess {
return nil, ttl, dns_feature.RCodeError(r.RCode)
}