Skip to content

Commit

Permalink
Check for a successful answer before rewriting TTL.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikispag committed Oct 4, 2023
1 parent ed7a181 commit 365aea3
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,6 @@ func (s *Server) timer(ctx context.Context) {

func (s *Server) forwardMessageAndCacheResponse(q *dns.Msg) (m *dns.Msg) {
m = s.forwardMessageAndGetResponse(q)
// Rewrite the TTL.
for _, a := range m.Answer {
// If the TTL provided upstream is smaller than `minTTL`, rewrite it.
a.Header().Ttl = uint32(max(a.Header().Ttl, uint32(s.minTTL)))
}
// Let's retry a few times if we can't resolve it at the first try.
for c := 0; m == nil && c < connectionsPerUpstream; c++ {
s.Log.Debugf("Retrying %q [%d/%d]...", q.Question, c+1, connectionsPerUpstream)
Expand All @@ -237,6 +232,13 @@ func (s *Server) forwardMessageAndCacheResponse(q *dns.Msg) (m *dns.Msg) {
s.Log.Infof("Giving up on %q after %d connection retries.", q.Question, connectionsPerUpstream)
return nil
}
if m.Answer != nil {
// Rewrite the TTL.
for _, a := range m.Answer {
// If the TTL provided upstream is smaller than `minTTL`, rewrite it.
a.Header().Ttl = uint32(max(a.Header().Ttl, uint32(s.minTTL)))
}
}
s.cache.put(q, m)
return m
}
Expand Down

0 comments on commit 365aea3

Please sign in to comment.