plugin/forward: expose few methods and attributes to public (#1766)

* plugin/forward: expose few methods and attributes to public

* Update comments
This commit is contained in:
Eugen Kleiner 2018-05-04 08:47:26 +03:00 committed by Miek Gieben
parent 59dbcd32ea
commit be8fcc484a
5 changed files with 21 additions and 11 deletions

View file

@ -33,7 +33,8 @@ func (p *Proxy) updateRtt(newRtt time.Duration) {
atomic.AddInt64(&p.avgRtt, int64((newRtt-rtt)/rttCount)) atomic.AddInt64(&p.avgRtt, int64((newRtt-rtt)/rttCount))
} }
func (p *Proxy) connect(ctx context.Context, state request.Request, forceTCP, metric bool) (*dns.Msg, error) { // Connect selects an upstream, sends the request and waits for a response.
func (p *Proxy) Connect(ctx context.Context, state request.Request, forceTCP, metric bool) (*dns.Msg, error) {
start := time.Now() start := time.Now()
proto := state.Proto() proto := state.Proto()

View file

@ -68,7 +68,7 @@ func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
var upstreamErr error var upstreamErr error
span = ot.SpanFromContext(ctx) span = ot.SpanFromContext(ctx)
i := 0 i := 0
list := f.list() list := f.List()
deadline := time.Now().Add(defaultTimeout) deadline := time.Now().Add(defaultTimeout)
for time.Now().Before(deadline) { for time.Now().Before(deadline) {
@ -103,7 +103,7 @@ func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
err error err error
) )
for { for {
ret, err = proxy.connect(ctx, state, f.forceTCP, true) ret, err = proxy.Connect(ctx, state, f.forceTCP, true)
if err != nil && err == errCachedClosed { // Remote side closed conn, can only happen with TCP. if err != nil && err == errCachedClosed { // Remote side closed conn, can only happen with TCP.
continue continue
} }
@ -176,8 +176,14 @@ func (f *Forward) isAllowedDomain(name string) bool {
return true return true
} }
// From returns the base domain to match for the request to be forwarded.
func (f *Forward) From() string { return f.from }
// ForceTCP returns if TCP is forced to be used even when the request comes in over UDP.
func (f *Forward) ForceTCP() bool { return f.forceTCP }
// List returns a set of proxies to be used for this client depending on the policy in f. // List returns a set of proxies to be used for this client depending on the policy in f.
func (f *Forward) list() []*Proxy { return f.p.List(f.proxies) } func (f *Forward) List() []*Proxy { return f.p.List(f.proxies) }
var ( var (
errInvalidDomain = errors.New("invalid domain for forward") errInvalidDomain = errors.New("invalid domain for forward")

View file

@ -21,7 +21,7 @@ func (f *Forward) Forward(state request.Request) (*dns.Msg, error) {
fails := 0 fails := 0
var upstreamErr error var upstreamErr error
for _, proxy := range f.list() { for _, proxy := range f.List() {
if proxy.Down(f.maxfails) { if proxy.Down(f.maxfails) {
fails++ fails++
if fails < len(f.proxies) { if fails < len(f.proxies) {
@ -29,10 +29,10 @@ func (f *Forward) Forward(state request.Request) (*dns.Msg, error) {
} }
// All upstream proxies are dead, assume healtcheck is complete broken and randomly // All upstream proxies are dead, assume healtcheck is complete broken and randomly
// select an upstream to connect to. // select an upstream to connect to.
proxy = f.list()[0] proxy = f.List()[0]
} }
ret, err := proxy.connect(context.Background(), state, f.forceTCP, true) ret, err := proxy.Connect(context.Background(), state, f.forceTCP, true)
ret, err = truncated(state, ret, err) ret, err = truncated(state, ret, err)
upstreamErr = err upstreamErr = err

View file

@ -39,6 +39,9 @@ func NewProxy(addr string, tlsConfig *tls.Config) *Proxy {
return p return p
} }
// Addr returns the address to forward to.
func (p *Proxy) Addr() (addr string) { return p.addr }
// dnsClient returns a client used for health checking. // dnsClient returns a client used for health checking.
func dnsClient(tlsConfig *tls.Config) *dns.Client { func dnsClient(tlsConfig *tls.Config) *dns.Client {
c := new(dns.Client) c := new(dns.Client)

View file

@ -29,10 +29,10 @@ func TestProxyClose(t *testing.T) {
p := NewProxy(s.Addr, nil) p := NewProxy(s.Addr, nil)
p.start(hcDuration) p.start(hcDuration)
go func() { p.connect(ctx, state, false, false) }() go func() { p.Connect(ctx, state, false, false) }()
go func() { p.connect(ctx, state, true, false) }() go func() { p.Connect(ctx, state, true, false) }()
go func() { p.connect(ctx, state, false, false) }() go func() { p.Connect(ctx, state, false, false) }()
go func() { p.connect(ctx, state, true, false) }() go func() { p.Connect(ctx, state, true, false) }()
p.close() p.close()
} }