added ToTTLDuration

This commit is contained in:
Kwitsch 2024-04-17 17:49:08 +00:00
parent 7f39983ffe
commit 66a7155650
2 changed files with 12 additions and 2 deletions

View File

@ -137,7 +137,7 @@ func (r *CachingResolver) reloadCacheEntry(ctx context.Context, cacheKey string)
r.redisClient.PublishCache(cacheKey, cacheCopy)
}
return &packed, time.Duration(ttl) * time.Second
return &packed, util.ToTTLDuration(ttl)
}
func (r *CachingResolver) redisSubscriber(ctx context.Context) {
@ -263,7 +263,7 @@ func (r *CachingResolver) putInCache(logger *logrus.Entry, cacheKey string, resp
return nil
}
r.resultCache.Put(cacheKey, &packed, time.Duration(ttl)*time.Second)
r.resultCache.Put(cacheKey, &packed, util.ToTTLDuration(ttl))
return cacheCopy
}

View File

@ -3,6 +3,7 @@ package util
import (
"math"
"sync/atomic"
"time"
"github.com/miekg/dns"
)
@ -42,6 +43,15 @@ func ToTTL[T TTLInput](input T) uint32 {
return uint32(res)
}
// ToTTLDuration converts the input to a time.Duration.
//
// If the input is of underlying type time.Duration, the value is returned as is.
//
// Otherwise the value is converted to seconds and returned as time.Duration.
func ToTTLDuration[T TTLInput](input T) time.Duration {
return time.Duration(ToTTL(input)) * time.Second
}
// SetAnswerMinTTL sets the TTL of all answers in the message that are less than the specified minimum TTL to
// the minimum TTL.
func SetAnswerMinTTL[T TTLInput](msg *dns.Msg, min T) {