cache: move goroutine closure to separate function to save memory (#3353)
The goroutine closure was causing objects to be heap allocated. Moving it to a separate function fixes that. ```benchmark old ns/op new ns/op delta BenchmarkCacheResponse/NoPrefetch-12 773 713 -7.76% BenchmarkCacheResponse/Prefetch-12 878 837 -4.67% BenchmarkHash-12 9.17 9.18 +0.11% benchmark old allocs new allocs delta BenchmarkCacheResponse/NoPrefetch-12 9 8 -11.11% BenchmarkCacheResponse/Prefetch-12 9 8 -11.11% BenchmarkHash-12 0 0 +0.00% benchmark old bytes new bytes delta BenchmarkCacheResponse/NoPrefetch-12 471 327 -30.57% BenchmarkCacheResponse/Prefetch-12 471 327 -30.57% BenchmarkHash-12 0 0 +0.00% ``` Signed-off-by: Charlie Vieth <charlie.vieth@gmail.com> Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
parent
aa96d6b443
commit
f8551df272
1 changed files with 25 additions and 20 deletions
35
plugin/cache/handler.go
vendored
35
plugin/cache/handler.go
vendored
|
@ -29,19 +29,23 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
|
||||||
i, found := c.get(now, state, server)
|
i, found := c.get(now, state, server)
|
||||||
if i != nil && found {
|
if i != nil && found {
|
||||||
resp := i.toMsg(r, now)
|
resp := i.toMsg(r, now)
|
||||||
|
|
||||||
w.WriteMsg(resp)
|
w.WriteMsg(resp)
|
||||||
|
|
||||||
if c.prefetch > 0 {
|
if c.shouldPrefetch(i, now) {
|
||||||
ttl := i.ttl(now)
|
go c.doPrefetch(ctx, state, server, i, now)
|
||||||
i.Freq.Update(c.duration, now)
|
}
|
||||||
|
return dns.RcodeSuccess, nil
|
||||||
|
}
|
||||||
|
|
||||||
threshold := int(math.Ceil(float64(c.percentage) / 100 * float64(i.origTTL)))
|
crr := &ResponseWriter{ResponseWriter: w, Cache: c, state: state, server: server}
|
||||||
if i.Freq.Hits() >= c.prefetch && ttl <= threshold {
|
return plugin.NextOrFailure(c.Name(), c.Next, ctx, crr, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cache) doPrefetch(ctx context.Context, state request.Request, server string, i *item, now time.Time) {
|
||||||
cw := newPrefetchResponseWriter(server, state, c)
|
cw := newPrefetchResponseWriter(server, state, c)
|
||||||
go func(w dns.ResponseWriter) {
|
|
||||||
cachePrefetches.WithLabelValues(server).Inc()
|
cachePrefetches.WithLabelValues(server).Inc()
|
||||||
plugin.NextOrFailure(c.Name(), c.Next, ctx, w, r)
|
plugin.NextOrFailure(c.Name(), c.Next, ctx, cw, state.Req)
|
||||||
|
|
||||||
// When prefetching we loose the item i, and with it the frequency
|
// When prefetching we loose the item i, and with it the frequency
|
||||||
// that we've gathered sofar. See we copy the frequencies info back
|
// that we've gathered sofar. See we copy the frequencies info back
|
||||||
|
@ -49,14 +53,15 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
|
||||||
if i1 := c.exists(state); i1 != nil {
|
if i1 := c.exists(state); i1 != nil {
|
||||||
i1.Freq.Reset(now, i.Freq.Hits())
|
i1.Freq.Reset(now, i.Freq.Hits())
|
||||||
}
|
}
|
||||||
}(cw)
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return dns.RcodeSuccess, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
crr := &ResponseWriter{ResponseWriter: w, Cache: c, state: state, server: server}
|
func (c *Cache) shouldPrefetch(i *item, now time.Time) bool {
|
||||||
return plugin.NextOrFailure(c.Name(), c.Next, ctx, crr, r)
|
if c.prefetch <= 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
i.Freq.Update(c.duration, now)
|
||||||
|
threshold := int(math.Ceil(float64(c.percentage) / 100 * float64(i.origTTL)))
|
||||||
|
return i.Freq.Hits() >= c.prefetch && i.ttl(now) <= threshold
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name implements the Handler interface.
|
// Name implements the Handler interface.
|
||||||
|
|
Loading…
Add table
Reference in a new issue