middleware/proxy: implement Exchanger (#480)
By defining and using an proxy.Exchanger interface we make the proxy more generic and we can then fold back httproxy into proxy. This overrides #463 and #473 and should make futures extensions rather trivial * Add docs that talk about `protocol` and how to set it. * middleware/proxy: rename New to NewLookup It's used as a Lookup mechanism not as a completely new proxy, reflect that in the name. * Set maxfails to 3 by default when looking up names. Most of the changes have been copied from https://github.com/johnbelamaric/coredns/pull/1/files
This commit is contained in:
parent
a6d232a622
commit
52e01264e8
25 changed files with 140 additions and 61 deletions
|
@ -7,17 +7,20 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var errUnreachable = errors.New("unreachable backend")
|
||||
var (
|
||||
errUnreachable = errors.New("unreachable backend")
|
||||
errInvalidProtocol = errors.New("invalid protocol")
|
||||
)
|
||||
|
||||
// Proxy represents a middleware instance that can proxy requests to another DNS server.
|
||||
// Proxy represents a middleware instance that can proxy requests to another (DNS) server.
|
||||
type Proxy struct {
|
||||
Next middleware.Handler
|
||||
Client *client
|
||||
Upstreams []Upstream
|
||||
}
|
||||
|
||||
|
@ -46,6 +49,7 @@ type UpstreamHost struct {
|
|||
Unhealthy bool
|
||||
CheckDown UpstreamHostDownFunc
|
||||
WithoutPathPrefix string
|
||||
Exchanger
|
||||
}
|
||||
|
||||
// Down checks whether the upstream host is down or not.
|
||||
|
@ -66,6 +70,7 @@ var tryDuration = 60 * time.Second
|
|||
|
||||
// ServeDNS satisfies the middleware.Handler interface.
|
||||
func (p Proxy) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
state := request.Request{W: w, Req: r}
|
||||
for _, upstream := range p.Upstreams {
|
||||
start := time.Now()
|
||||
|
||||
|
@ -82,7 +87,7 @@ func (p Proxy) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
|
|||
|
||||
atomic.AddInt64(&host.Conns, 1)
|
||||
|
||||
reply, backendErr := p.Client.ServeDNS(w, r, host)
|
||||
reply, backendErr := host.Exchange(state)
|
||||
|
||||
atomic.AddInt64(&host.Conns, -1)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue