git » dnss » commit 935ca35

dnsserver: Sort entries by TTL on /cache/dump

author Alberto Bertogli
2023-08-04 00:57:00 UTC
committer Alberto Bertogli
2023-08-06 09:45:10 UTC
parent 4e0ca8060667ff354e99d97cf969f45e98e8a035

dnsserver: Sort entries by TTL on /cache/dump

When doing a cache dump, sort entries by TTL to have a more consistent,
easy to read output.

internal/dnsserver/resolver.go +16 -1

diff --git a/internal/dnsserver/resolver.go b/internal/dnsserver/resolver.go
index db3529e..12ac6bc 100644
--- a/internal/dnsserver/resolver.go
+++ b/internal/dnsserver/resolver.go
@@ -5,6 +5,7 @@ import (
 	"expvar"
 	"fmt"
 	"net/http"
+	"sort"
 	"sync"
 	"time"
 
@@ -118,7 +119,21 @@ func (c *cachingResolver) DumpCache(w http.ResponseWriter, r *http.Request) {
 	buf := bytes.NewBuffer(nil)
 
 	c.mu.RLock()
-	for q, ans := range c.answer {
+
+	// Sort output by expiration, so it is somewhat consistent and practical
+	// to read.
+	qs := []dns.Question{}
+	for q := range c.answer {
+		qs = append(qs, q)
+	}
+	sort.Slice(qs, func(i, j int) bool {
+		return getTTL(c.answer[qs[i]]) < getTTL(c.answer[qs[j]])
+	})
+
+	// Go through the sorted list and dump the entries.
+	for _, q := range qs {
+		ans := c.answer[q]
+
 		// Only include names and records if we are running verbosily.
 		name := "<hidden>"
 		if log.V(1) {