middleware/cache: fix race (#757)

While adding a parallel performance benchmark I stumbled on a race
condition (another reason to add performance benchmarks!), so this
PR makes sure the msg is created in a race free manor and adds the
parallel benchmark.
This commit is contained in:
Miek Gieben 2017-06-26 07:44:25 -07:00 committed by GitHub
parent ea90702bfc
commit da5880a273
4 changed files with 67 additions and 27 deletions

View file

@ -6,6 +6,8 @@ import (
"testing"
"time"
"golang.org/x/net/context"
"github.com/coredns/coredns/middleware"
"github.com/coredns/coredns/middleware/pkg/cache"
"github.com/coredns/coredns/middleware/pkg/response"
@ -205,3 +207,45 @@ func TestCache(t *testing.T) {
}
}
}
func BenchmarkCacheResponse(b *testing.B) {
c := &Cache{Zones: []string{"."}, pcap: defaultCap, ncap: defaultCap, pttl: maxTTL, nttl: maxTTL}
c.pcache = cache.New(c.pcap)
c.ncache = cache.New(c.ncap)
c.prefetch = 1
c.duration = 1 * time.Second
c.Next = BackendHandler()
ctx := context.TODO()
reqs := make([]*dns.Msg, 5)
for i, q := range []string{"example1", "example2", "a", "b", "ddd"} {
reqs[i] = new(dns.Msg)
reqs[i].SetQuestion(q+".example.org.", dns.TypeA)
}
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
req := reqs[i]
c.ServeDNS(ctx, &test.ResponseWriter{}, req)
i++
i = i % 5
}
})
}
func BackendHandler() middleware.Handler {
return middleware.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
m := new(dns.Msg)
m.SetReply(r)
m.Response = true
m.RecursionAvailable = true
owner := m.Question[0].Name
m.Answer = []dns.RR{test.A(owner + " 303 IN A 127.0.0.53")}
w.WriteMsg(m)
return dns.RcodeSuccess, nil
})
}