Skip to content

Commit 5f56440

Browse files
committed
dnsx/cacher: m refactor hangover
1 parent be04390 commit 5f56440

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

intra/core/hangover.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020 RethinkDNS and its authors.
1+
// Copyright (c) 2024 RethinkDNS and its authors.
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -41,14 +41,14 @@ func NewHangover() *Hangover {
4141
return &Hangover{start: s}
4242
}
4343

44-
func (h *Hangover) Start() {
44+
func (h *Hangover) Note() {
4545
s := h.start.Load()
4646
if s.IsZero() {
4747
h.start.Cas(s, time.Now())
4848
} // else: already started
4949
}
5050

51-
func (h *Hangover) Stop() {
51+
func (h *Hangover) Break() {
5252
s := h.start.Load()
5353
if !s.IsZero() {
5454
h.start.Cas(s, zerotime)

intra/dnsx/cacher.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type ctransport struct {
8080
bumps int // max bumps in lifetime of a cached response
8181
size int // max size of a cache bucket
8282
reqbarrier *core.Barrier[*cres] // coalesce requests for the same query
83-
hangover *core.Hangover // hangover for the transport
83+
hangover *core.Hangover // tracks send failure threshold
8484
est core.P2QuantileEstimator
8585
}
8686

@@ -306,13 +306,22 @@ func (t *ctransport) Type() string {
306306
return t.Transport.Type()
307307
}
308308

309+
func (t *ctransport) hangoverCheckpoint() {
310+
if t.Status() == SendFailed {
311+
t.hangover.Note()
312+
} else {
313+
t.hangover.Break()
314+
}
315+
}
316+
309317
func (t *ctransport) fetch(network string, q []byte, msg *dns.Msg, summary *x.DNSSummary, cb *cache, key string) (r []byte, err error) {
310318
sendRequest := func(fsmm *x.DNSSummary) ([]byte, error) {
311319
fsmm.ID = t.Transport.ID()
312320
fsmm.Type = t.Transport.Type()
313321

314322
v, _ := t.reqbarrier.Do(key, func() (*cres, error) {
315323
ans, qerr := Req(t.Transport, network, q, fsmm)
324+
t.hangoverCheckpoint()
316325
// cb.put no-ops when len(ans) is 0
317326
cb.put(key, ans, fsmm)
318327
// cres.ans may be nil
@@ -339,12 +348,7 @@ func (t *ctransport) fetch(network string, q []byte, msg *dns.Msg, summary *x.DN
339348
// no network connectivity but cache returns proper responses to queries,
340349
// which results in confused apps that think there's network connectivity,
341350
// that is, these confused apps go bezerk resulting in battery drain.
342-
if t.Status() == SendFailed {
343-
t.hangover.Start()
344-
} else {
345-
t.hangover.Stop()
346-
}
347-
// 10s hasn't elapsed since the first send failure
351+
// has 10s elapsed since the first send failure
348352
trok := t.hangover.Within(ttl10s)
349353

350354
if v, isfresh := cb.freshCopy(key); trok && v != nil {

0 commit comments

Comments
 (0)