Skip to content

Commit

Permalink
Add ExchangeCache api
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Apr 26, 2023
1 parent 18b00d0 commit 25d948c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,39 @@ func (c *Client) exchange(ctx context.Context, transport Transport, message *dns
return response, err
}

func (c *Client) ExchangeCache(ctx context.Context, message *dns.Msg) (*dns.Msg, bool) {
response, cached := c.exchangeCache(ctx, message)
if !cached {
return nil, false
}
if rewriteTTL, loaded := RewriteTTLFromContext(ctx); loaded {
for _, recordList := range [][]dns.RR{response.Answer, response.Ns, response.Extra} {
for _, record := range recordList {
record.Header().Ttl = rewriteTTL
}
}
}
return response, true
}

func (c *Client) exchangeCache(ctx context.Context, message *dns.Msg) (*dns.Msg, bool) {
if c.independentCache || len(message.Question) != 1 {
return nil, false
}
question := message.Question[0]
disableCache := c.disableCache || DisableCacheFromContext(ctx)
if disableCache {
return nil, false
}
response, ttl := c.loadResponse(question, nil)
if response == nil {
return nil, false
}
logCachedResponse(c.logger, ctx, response, ttl)
response.Id = message.Id
return response, true
}

func (c *Client) Lookup(ctx context.Context, transport Transport, domain string, strategy DomainStrategy) ([]netip.Addr, error) {
if dns.IsFqdn(domain) {
domain = domain[:len(domain)-1]
Expand Down

0 comments on commit 25d948c

Please sign in to comment.