diff options
author | 2019-07-16 15:00:22 -0400 | |
---|---|---|
committer | 2019-07-16 19:00:22 +0000 | |
commit | 21e9c6047b5bb4af4b798229828630fdfe633a7e (patch) | |
tree | 82e7b2e1cdca7d2a159a3fd8a8d94e82923185ac | |
parent | 767d3998770a9bb7dac3ec7401c63d59b12bc215 (diff) | |
download | coredns-21e9c6047b5bb4af4b798229828630fdfe633a7e.tar.gz coredns-21e9c6047b5bb4af4b798229828630fdfe633a7e.tar.zst coredns-21e9c6047b5bb4af4b798229828630fdfe633a7e.zip |
Fix log plugin benchmark and slightly improve performance (#3004)
* log: use ioutil.Discard as write buffer in benchmark
Using a buffer gives unrealistic stats and consumes a large amount of
memory.
* log: lazily check if a msg should be logged
* log: improve variable name
Change 'ok' to the more descriptive 'shouldLog'.
* log: code comments: don't reuse variable
-rw-r--r-- | plugin/log/log.go | 12 | ||||
-rw-r--r-- | plugin/log/log_test.go | 4 |
2 files changed, 10 insertions, 6 deletions
diff --git a/plugin/log/log.go b/plugin/log/log.go index 49581dfc4..7e6201138 100644 --- a/plugin/log/log.go +++ b/plugin/log/log.go @@ -26,20 +26,24 @@ type Logger struct { // ServeDNS implements the plugin.Handler interface. func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { state := request.Request{W: w, Req: r} + name := state.Name() for _, rule := range l.Rules { - if !plugin.Name(rule.NameScope).Matches(state.Name()) { + if !plugin.Name(rule.NameScope).Matches(name) { continue } rrw := dnstest.NewRecorder(w) rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r) - tpe, _ := response.Typify(rrw.Msg, time.Now().UTC()) - class := response.Classify(tpe) // If we don't set up a class in config, the default "all" will be added // and we shouldn't have an empty rule.Class. _, ok := rule.Class[response.All] - _, ok1 := rule.Class[class] + var ok1 bool + if !ok { + tpe, _ := response.Typify(rrw.Msg, time.Now().UTC()) + class := response.Classify(tpe) + _, ok1 = rule.Class[class] + } if ok || ok1 { logstr := l.repl.Replace(ctx, state, rrw, rule.Format) clog.Infof(logstr) diff --git a/plugin/log/log_test.go b/plugin/log/log_test.go index e7f29fff1..16efb2026 100644 --- a/plugin/log/log_test.go +++ b/plugin/log/log_test.go @@ -3,6 +3,7 @@ package log import ( "bytes" "context" + "io/ioutil" "log" "strings" "testing" @@ -239,8 +240,7 @@ func TestLogged(t *testing.T) { } func BenchmarkLogged(b *testing.B) { - var f bytes.Buffer - log.SetOutput(&f) + log.SetOutput(ioutil.Discard) rule := Rule{ NameScope: ".", |