mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
Remove context.Context from request.Request (#2726)
* Remove context.Context from request.Request This removes the context from request.Request and makes all the changes in the code to make it compile again. It's all mechanical. It did unearth some weirdness in that the context was kept in handler structs which may cause havoc with concurrently handling of requests. Fixes #2721 Signed-off-by: Miek Gieben <miek@miek.nl> * Make test compile Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
@@ -36,14 +36,13 @@ type Etcd struct {
|
||||
PathPrefix string
|
||||
Upstream *upstream.Upstream
|
||||
Client *etcdcv3.Client
|
||||
Ctx context.Context
|
||||
|
||||
endpoints []string // Stored here as well, to aid in testing.
|
||||
}
|
||||
|
||||
// Services implements the ServiceBackend interface.
|
||||
func (e *Etcd) Services(state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
|
||||
services, err = e.Records(state, exact)
|
||||
func (e *Etcd) Services(ctx context.Context, state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
|
||||
services, err = e.Records(ctx, state, exact)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -53,13 +52,13 @@ func (e *Etcd) Services(state request.Request, exact bool, opt plugin.Options) (
|
||||
}
|
||||
|
||||
// Reverse implements the ServiceBackend interface.
|
||||
func (e *Etcd) Reverse(state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
|
||||
return e.Services(state, exact, opt)
|
||||
func (e *Etcd) Reverse(ctx context.Context, state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
|
||||
return e.Services(ctx, state, exact, opt)
|
||||
}
|
||||
|
||||
// Lookup implements the ServiceBackend interface.
|
||||
func (e *Etcd) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
|
||||
return e.Upstream.Lookup(state, name, typ)
|
||||
func (e *Etcd) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
|
||||
return e.Upstream.Lookup(ctx, state, name, typ)
|
||||
}
|
||||
|
||||
// IsNameError implements the ServiceBackend interface.
|
||||
@@ -69,11 +68,11 @@ func (e *Etcd) IsNameError(err error) bool {
|
||||
|
||||
// Records looks up records in etcd. If exact is true, it will lookup just this
|
||||
// name. This is used when find matches when completing SRV lookups for instance.
|
||||
func (e *Etcd) Records(state request.Request, exact bool) ([]msg.Service, error) {
|
||||
func (e *Etcd) Records(ctx context.Context, state request.Request, exact bool) ([]msg.Service, error) {
|
||||
name := state.Name()
|
||||
|
||||
path, star := msg.PathWithWildcard(name, e.PathPrefix)
|
||||
r, err := e.get(path, !exact)
|
||||
r, err := e.get(ctx, path, !exact)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -81,8 +80,8 @@ func (e *Etcd) Records(state request.Request, exact bool) ([]msg.Service, error)
|
||||
return e.loopNodes(r.Kvs, segments, star, state.QType())
|
||||
}
|
||||
|
||||
func (e *Etcd) get(path string, recursive bool) (*etcdcv3.GetResponse, error) {
|
||||
ctx, cancel := context.WithTimeout(e.Ctx, etcdTimeout)
|
||||
func (e *Etcd) get(ctx context.Context, path string, recursive bool) (*etcdcv3.GetResponse, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, etcdTimeout)
|
||||
defer cancel()
|
||||
if recursive == true {
|
||||
if !strings.HasSuffix(path, "/") {
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
// ServeDNS implements the plugin.Handler interface.
|
||||
func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
opt := plugin.Options{}
|
||||
state := request.Request{W: w, Req: r, Context: ctx}
|
||||
state := request.Request{W: w, Req: r}
|
||||
|
||||
zone := plugin.Zones(e.Zones).Matches(state.Name())
|
||||
if zone == "" {
|
||||
@@ -26,44 +26,44 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
|
||||
|
||||
switch state.QType() {
|
||||
case dns.TypeA:
|
||||
records, err = plugin.A(e, zone, state, nil, opt)
|
||||
records, err = plugin.A(ctx, e, zone, state, nil, opt)
|
||||
case dns.TypeAAAA:
|
||||
records, err = plugin.AAAA(e, zone, state, nil, opt)
|
||||
records, err = plugin.AAAA(ctx, e, zone, state, nil, opt)
|
||||
case dns.TypeTXT:
|
||||
records, err = plugin.TXT(e, zone, state, opt)
|
||||
records, err = plugin.TXT(ctx, e, zone, state, opt)
|
||||
case dns.TypeCNAME:
|
||||
records, err = plugin.CNAME(e, zone, state, opt)
|
||||
records, err = plugin.CNAME(ctx, e, zone, state, opt)
|
||||
case dns.TypePTR:
|
||||
records, err = plugin.PTR(e, zone, state, opt)
|
||||
records, err = plugin.PTR(ctx, e, zone, state, opt)
|
||||
case dns.TypeMX:
|
||||
records, extra, err = plugin.MX(e, zone, state, opt)
|
||||
records, extra, err = plugin.MX(ctx, e, zone, state, opt)
|
||||
case dns.TypeSRV:
|
||||
records, extra, err = plugin.SRV(e, zone, state, opt)
|
||||
records, extra, err = plugin.SRV(ctx, e, zone, state, opt)
|
||||
case dns.TypeSOA:
|
||||
records, err = plugin.SOA(e, zone, state, opt)
|
||||
records, err = plugin.SOA(ctx, e, zone, state, opt)
|
||||
case dns.TypeNS:
|
||||
if state.Name() == zone {
|
||||
records, extra, err = plugin.NS(e, zone, state, opt)
|
||||
records, extra, err = plugin.NS(ctx, e, zone, state, opt)
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
default:
|
||||
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
|
||||
_, err = plugin.A(e, zone, state, nil, opt)
|
||||
_, err = plugin.A(ctx, e, zone, state, nil, opt)
|
||||
}
|
||||
if err != nil && e.IsNameError(err) {
|
||||
if e.Fall.Through(state.Name()) {
|
||||
return plugin.NextOrFailure(e.Name(), e.Next, ctx, w, r)
|
||||
}
|
||||
// Make err nil when returning here, so we don't log spam for NXDOMAIN.
|
||||
return plugin.BackendError(e, zone, dns.RcodeNameError, state, nil /* err */, opt)
|
||||
return plugin.BackendError(ctx, e, zone, dns.RcodeNameError, state, nil /* err */, opt)
|
||||
}
|
||||
if err != nil {
|
||||
return plugin.BackendError(e, zone, dns.RcodeServerFailure, state, err, opt)
|
||||
return plugin.BackendError(ctx, e, zone, dns.RcodeServerFailure, state, err, opt)
|
||||
}
|
||||
|
||||
if len(records) == 0 {
|
||||
return plugin.BackendError(e, zone, dns.RcodeSuccess, state, err, opt)
|
||||
return plugin.BackendError(ctx, e, zone, dns.RcodeSuccess, state, err, opt)
|
||||
}
|
||||
|
||||
m := new(dns.Msg)
|
||||
|
||||
@@ -294,7 +294,6 @@ func newEtcdPlugin() *Etcd {
|
||||
return &Etcd{
|
||||
Upstream: upstream.New(),
|
||||
PathPrefix: "skydns",
|
||||
Ctx: context.Background(),
|
||||
Zones: []string{"skydns.test.", "skydns_extra.test.", "skydns_zonea.test.", "skydns_zoneb.test.", "skydns_zonec.test.", "skydns_zoned.test.", "in-addr.arpa."},
|
||||
Client: client,
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
@@ -38,10 +37,7 @@ func setup(c *caddy.Controller) error {
|
||||
}
|
||||
|
||||
func etcdParse(c *caddy.Controller) (*Etcd, error) {
|
||||
etc := Etcd{
|
||||
PathPrefix: "skydns",
|
||||
Ctx: context.Background(),
|
||||
}
|
||||
etc := Etcd{PathPrefix: "skydns"}
|
||||
var (
|
||||
tlsConfig *tls.Config
|
||||
err error
|
||||
|
||||
Reference in New Issue
Block a user