chore: Disable cache for RCode client

This commit is contained in:
gVisor bot 2023-06-11 20:58:51 +08:00
parent 1f49fa0c5d
commit 07d75d52e6
3 changed files with 19 additions and 10 deletions

View file

@ -59,7 +59,8 @@ func (d *dhcpClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg,
return nil, err return nil, err
} }
return batchExchange(ctx, clients, m) msg, _, err = batchExchange(ctx, clients, m)
return
} }
func (d *dhcpClient) resolve(ctx context.Context) ([]dnsClient, error) { func (d *dhcpClient) resolve(ctx context.Context) ([]dnsClient, error) {

View file

@ -182,6 +182,7 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M
fn := func() (result any, err error) { fn := func() (result any, err error) {
ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout) // reset timeout in singleflight ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout) // reset timeout in singleflight
defer cancel() defer cancel()
cache := false
defer func() { defer func() {
if err != nil { if err != nil {
@ -192,7 +193,9 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M
msg := result.(*D.Msg) msg := result.(*D.Msg)
if cache {
putMsgToCache(r.lruCache, q.String(), msg) putMsgToCache(r.lruCache, q.String(), msg)
}
}() }()
isIPReq := isIPRequest(q) isIPReq := isIPRequest(q)
@ -201,9 +204,11 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M
} }
if matched := r.matchPolicy(m); len(matched) != 0 { if matched := r.matchPolicy(m); len(matched) != 0 {
return r.batchExchange(ctx, matched, m) result, cache, err = r.batchExchange(ctx, matched, m)
return
} }
return r.batchExchange(ctx, r.main, m) result, cache, err = r.batchExchange(ctx, r.main, m)
return
} }
ch := r.group.DoChan(q.String(), fn) ch := r.group.DoChan(q.String(), fn)
@ -244,7 +249,7 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M
return return
} }
func (r *Resolver) batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, err error) { func (r *Resolver) batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, cache bool, err error) {
ctx, cancel := context.WithTimeout(ctx, resolver.DefaultDNSTimeout) ctx, cancel := context.WithTimeout(ctx, resolver.DefaultDNSTimeout)
defer cancel() defer cancel()
@ -371,7 +376,7 @@ func (r *Resolver) lookupIP(ctx context.Context, host string, dnsType uint16) (i
func (r *Resolver) asyncExchange(ctx context.Context, client []dnsClient, msg *D.Msg) <-chan *result { func (r *Resolver) asyncExchange(ctx context.Context, client []dnsClient, msg *D.Msg) <-chan *result {
ch := make(chan *result, 1) ch := make(chan *result, 1)
go func() { go func() {
res, err := r.batchExchange(ctx, client, msg) res, _, err := r.batchExchange(ctx, client, msg)
ch <- &result{Msg: res, Error: err} ch <- &result{Msg: res, Error: err}
}() }()
return ch return ch

View file

@ -287,17 +287,20 @@ func listenPacket(ctx context.Context, proxyAdapter C.ProxyAdapter, proxyName st
return proxyAdapter.ListenPacketContext(ctx, metadata, opts...) return proxyAdapter.ListenPacketContext(ctx, metadata, opts...)
} }
func batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, err error) { func batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, cache bool, err error) {
fast, ctx := picker.WithTimeout[*D.Msg](ctx, resolver.DefaultDNSTimeout) fast, ctx := picker.WithTimeout[*D.Msg](ctx, resolver.DefaultDNSTimeout)
domain := msgToDomain(m) domain := msgToDomain(m)
for _, client := range clients { for _, client := range clients {
_, ignoreRCodeError := client.(rcodeClient) _, cache = client.(rcodeClient)
cache = !cache
fast.Go(func() (*D.Msg, error) { fast.Go(func() (*D.Msg, error) {
log.Debugln("[DNS] resolve %s from %s", domain, client.Address()) log.Debugln("[DNS] resolve %s from %s", domain, client.Address())
m, err := client.ExchangeContext(ctx, m) m, err := client.ExchangeContext(ctx, m)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !ignoreRCodeError && (m.Rcode == D.RcodeServerFailure || m.Rcode == D.RcodeRefused) { } else if cache && (m.Rcode == D.RcodeServerFailure || m.Rcode == D.RcodeRefused) {
// currently, cache indicates whether this msg was from a RCode client,
// so we would ignore RCode errors from RCode clients.
return nil, errors.New("server failure") return nil, errors.New("server failure")
} }
log.Debugln("[DNS] %s --> %s, from %s", domain, msgToIP(m), client.Address()) log.Debugln("[DNS] %s --> %s, from %s", domain, msgToIP(m), client.Address())
@ -311,7 +314,7 @@ func batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.M
if fErr := fast.Error(); fErr != nil { if fErr := fast.Error(); fErr != nil {
err = fmt.Errorf("%w, first error: %s", err, fErr.Error()) err = fmt.Errorf("%w, first error: %s", err, fErr.Error())
} }
return nil, err return nil, true, err
} }
msg = elm msg = elm
return return