Avoids allocations and function calls if no extra data is present.
This commit is contained in:
parent
e05c77b9bf
commit
45e2879eb5
|
@ -545,14 +545,23 @@ func syncExtra(index map[string]dns.RR, resp *dns.Msg) {
|
|||
// records will be trimmed along with answers.
|
||||
func trimUDPResponse(config *DNSConfig, resp *dns.Msg) (trimmed bool) {
|
||||
numAnswers := len(resp.Answer)
|
||||
index := indexRRs(resp.Extra)
|
||||
hasExtra := len(resp.Extra) > 0
|
||||
|
||||
// We avoid some function calls and allocations by only handling the
|
||||
// extra data when necessary.
|
||||
var index map[string]dns.RR
|
||||
if hasExtra {
|
||||
index = indexRRs(resp.Extra)
|
||||
}
|
||||
|
||||
// This cuts UDP responses to a useful but limited number of responses.
|
||||
maxAnswers := lib.MinInt(maxUDPAnswerLimit, config.UDPAnswerLimit)
|
||||
if numAnswers > maxAnswers {
|
||||
resp.Answer = resp.Answer[:maxAnswers]
|
||||
if hasExtra {
|
||||
syncExtra(index, resp)
|
||||
}
|
||||
}
|
||||
|
||||
// This enforces the hard limit of 512 bytes per the RFC. Note that we
|
||||
// temporarily switch to uncompressed so that we limit to a response
|
||||
|
@ -563,8 +572,10 @@ func trimUDPResponse(config *DNSConfig, resp *dns.Msg) (trimmed bool) {
|
|||
resp.Compress = false
|
||||
for len(resp.Answer) > 0 && resp.Len() > 512 {
|
||||
resp.Answer = resp.Answer[:len(resp.Answer)-1]
|
||||
if hasExtra {
|
||||
syncExtra(index, resp)
|
||||
}
|
||||
}
|
||||
resp.Compress = compress
|
||||
|
||||
return len(resp.Answer) < numAnswers
|
||||
|
|
Loading…
Reference in New Issue