Skip to content

Commit

Permalink
plugin/log: remove ErrorFunc (coredns#2716)
Browse files Browse the repository at this point in the history
The server handles this case no need to also do it in the log plugin.

Means DefaultErrorFunc can be private to the dnsserver and is now
renamed to just errorFunc

Fixes: coredns#2715

Signed-off-by: Miek Gieben <miek@miek.nl>
  • Loading branch information
miekg authored and yongtang committed Mar 25, 2019
1 parent f08f7e2 commit 45624a0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 34 deletions.
20 changes: 10 additions & 10 deletions core/dnsserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// The default dns.Mux checks the question section size, but we have our
// own mux here. Check if we have a question section. If not drop them here.
if r == nil || len(r.Question) == 0 {
DefaultErrorFunc(ctx, w, r, dns.RcodeServerFailure)
errorFunc(ctx, w, r, dns.RcodeServerFailure)
return
}

Expand All @@ -215,13 +215,13 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// need to make sure that we stay alive up here
if rec := recover(); rec != nil {
vars.Panic.Inc()
DefaultErrorFunc(ctx, w, r, dns.RcodeServerFailure)
errorFunc(ctx, w, r, dns.RcodeServerFailure)
}
}()
}

if !s.classChaos && r.Question[0].Qclass != dns.ClassINET {
DefaultErrorFunc(ctx, w, r, dns.RcodeRefused)
errorFunc(ctx, w, r, dns.RcodeRefused)
return
}

Expand Down Expand Up @@ -260,7 +260,7 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
if h.FilterFunc == nil {
rcode, _ := h.pluginChain.ServeDNS(ctx, w, r)
if !plugin.ClientWrite(rcode) {
DefaultErrorFunc(ctx, w, r, rcode)
errorFunc(ctx, w, r, rcode)
}
return
}
Expand All @@ -269,7 +269,7 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
if h.FilterFunc(q) {
rcode, _ := h.pluginChain.ServeDNS(ctx, w, r)
if !plugin.ClientWrite(rcode) {
DefaultErrorFunc(ctx, w, r, rcode)
errorFunc(ctx, w, r, rcode)
}
return
}
Expand All @@ -291,7 +291,7 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
// DS request, and we found a zone, use the handler for the query.
rcode, _ := dshandler.pluginChain.ServeDNS(ctx, w, r)
if !plugin.ClientWrite(rcode) {
DefaultErrorFunc(ctx, w, r, rcode)
errorFunc(ctx, w, r, rcode)
}
return
}
Expand All @@ -304,13 +304,13 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)

rcode, _ := h.pluginChain.ServeDNS(ctx, w, r)
if !plugin.ClientWrite(rcode) {
DefaultErrorFunc(ctx, w, r, rcode)
errorFunc(ctx, w, r, rcode)
}
return
}

// Still here? Error out with REFUSED.
DefaultErrorFunc(ctx, w, r, dns.RcodeRefused)
errorFunc(ctx, w, r, dns.RcodeRefused)
}

// OnStartupComplete lists the sites served by this server
Expand All @@ -336,8 +336,8 @@ func (s *Server) Tracer() ot.Tracer {
return s.trace.Tracer()
}

// DefaultErrorFunc responds to an DNS request with an error.
func DefaultErrorFunc(ctx context.Context, w dns.ResponseWriter, r *dns.Msg, rc int) {
// errorFunc responds to an DNS request with an error.
func errorFunc(ctx context.Context, w dns.ResponseWriter, r *dns.Msg, rc int) {
state := request.Request{W: w, Req: r}

answer := new(dns.Msg)
Expand Down
23 changes: 2 additions & 21 deletions plugin/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"time"

"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics/vars"
"github.com/coredns/coredns/plugin/pkg/dnstest"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/rcode"
"github.com/coredns/coredns/plugin/pkg/replacer"
"github.com/coredns/coredns/plugin/pkg/response"
"github.com/coredns/coredns/request"
Expand All @@ -19,9 +17,8 @@ import (

// Logger is a basic request logging plugin.
type Logger struct {
Next plugin.Handler
Rules []Rule
ErrorFunc func(context.Context, dns.ResponseWriter, *dns.Msg, int) // failover error handler
Next plugin.Handler
Rules []Rule

repl replacer.Replacer
}
Expand All @@ -37,22 +34,6 @@ func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
rrw := dnstest.NewRecorder(w)
rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r)

if rc > 0 {
// There was an error up the chain, but no response has been written yet.
// The error must be handled here so the log entry will record the response size.
if l.ErrorFunc != nil {
l.ErrorFunc(ctx, rrw, r, rc)
} else {
answer := new(dns.Msg)
answer.SetRcode(r, rc)

vars.Report(ctx, state, vars.Dropped, rcode.ToString(rc), answer.Len(), time.Now())

w.WriteMsg(answer)
}
rc = 0
}

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
Expand Down
4 changes: 2 additions & 2 deletions plugin/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestLoggedStatus(t *testing.T) {
rec := dnstest.NewRecorder(&test.ResponseWriter{})

rcode, _ := logger.ServeDNS(ctx, rec, r)
if rcode != 0 {
t.Errorf("Expected rcode to be 0 - was: %d", rcode)
if rcode != 2 {
t.Errorf("Expected rcode to be 2 - was: %d", rcode)
}

logged := f.String()
Expand Down
2 changes: 1 addition & 1 deletion plugin/log/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func setup(c *caddy.Controller) error {
}

dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Logger{Next: next, Rules: rules, ErrorFunc: dnsserver.DefaultErrorFunc, repl: replacer.New()}
return Logger{Next: next, Rules: rules, repl: replacer.New()}
})

return nil
Expand Down

0 comments on commit 45624a0

Please sign in to comment.