use tickers instead of time.After to avoid memory leak (#5220)

Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
This commit is contained in:
Chris O'Haver
2022-03-04 02:36:02 -05:00
committed by GitHub
parent d40d224271
commit 967814161a
5 changed files with 31 additions and 13 deletions

View File

@@ -92,8 +92,10 @@ func (d *dio) write(payload *tap.Dnstap) error {
}
func (d *dio) serve() {
timeout := time.After(d.flushTimeout)
timeout := time.NewTimer(d.flushTimeout)
defer timeout.Stop()
for {
timeout.Reset(d.flushTimeout)
select {
case <-d.quit:
if d.enc == nil {
@@ -106,7 +108,7 @@ func (d *dio) serve() {
if err := d.write(&payload); err != nil {
d.dial()
}
case <-timeout:
case <-timeout.C:
if dropped := atomic.SwapUint32(&d.dropped, 0); dropped > 0 {
log.Warningf("Dropped dnstap messages: %d", dropped)
}
@@ -115,7 +117,6 @@ func (d *dio) serve() {
} else {
d.enc.flush()
}
timeout = time.After(d.flushTimeout)
}
}
}