feature/conn25: extend assignment expiry on use
When we use assigned addresses in response to a DNS request, extend the expiry on the assignment. Updates tailscale/corp#39975 Signed-off-by: Fran Bull <fran@tailscale.com>
This commit is contained in:
@@ -34,16 +34,23 @@ type addrAssignments struct {
|
||||
|
||||
const defaultExpiry = 48 * time.Hour
|
||||
|
||||
func clampExpiryFromTTL(ttl time.Duration) time.Duration {
|
||||
const minTTL = time.Minute * 1
|
||||
const maxTTL = time.Hour * 72
|
||||
expiry := max(minTTL, ttl)
|
||||
return min(maxTTL, expiry)
|
||||
}
|
||||
|
||||
func (a *addrAssignments) insert(as *addrs) error {
|
||||
return a.insertWithExpiry(as, defaultExpiry)
|
||||
}
|
||||
|
||||
func (a *addrAssignments) insertFromTTL(as *addrs, ttl time.Duration) error {
|
||||
const minTTL = time.Minute * 1
|
||||
const maxTTL = time.Hour * 72
|
||||
expiry := max(minTTL, ttl)
|
||||
expiry = min(maxTTL, expiry)
|
||||
return a.insertWithExpiry(as, expiry)
|
||||
return a.insertWithExpiry(as, clampExpiryFromTTL(ttl))
|
||||
}
|
||||
|
||||
func (a *addrAssignments) updateFromTTL(as *addrs, ttl time.Duration) {
|
||||
a.updateExpiry(as, clampExpiryFromTTL(ttl))
|
||||
}
|
||||
|
||||
func (a *addrAssignments) insertWithExpiry(as *addrs, d time.Duration) error {
|
||||
@@ -104,10 +111,10 @@ func (a *addrAssignments) lookupByTransitIP(tip netip.Addr) (*addrs, bool) {
|
||||
// or an invalid addrs if there are no expired members of addrAssignments.
|
||||
func (a *addrAssignments) popExpired(now time.Time) *addrs {
|
||||
if a.byExpiresAt.Len() == 0 {
|
||||
return &addrs{}
|
||||
return nil
|
||||
}
|
||||
if !a.byExpiresAt.peek().expiresAt.Before(now) {
|
||||
return &addrs{}
|
||||
return nil
|
||||
}
|
||||
v := heap.Pop(&a.byExpiresAt).(*addrs)
|
||||
delete(a.byMagicIP, v.magic)
|
||||
@@ -117,6 +124,18 @@ func (a *addrAssignments) popExpired(now time.Time) *addrs {
|
||||
return v
|
||||
}
|
||||
|
||||
func (a *addrAssignments) updateExpiry(as *addrs, expiresIn time.Duration) {
|
||||
now := a.clock.Now()
|
||||
as.expiresAt = now.Add(expiresIn)
|
||||
// TODO(fran) 2026-05-26 We can make this perform better.
|
||||
// * With a bit of extra effort, we can track the index so that heap.Fix can
|
||||
// be used.
|
||||
// * Alternatively, marking the heap dirty and waiting until the next
|
||||
// operation that requires it to be in the correct order would mean a
|
||||
// whole slew of updates can accumulate before paying for a heap.Init.
|
||||
heap.Init(&a.byExpiresAt)
|
||||
}
|
||||
|
||||
type addrsHeap []*addrs
|
||||
|
||||
func (h addrsHeap) Len() int { return len(h) }
|
||||
|
||||
Reference in New Issue
Block a user