Commit a1d92c51 authored by Miek Gieben's avatar Miek Gieben Committed by GitHub

plugin/forward: remove dynamic read timeout (#2319)

* plugin/forward: remove dynamic read timeout

We care about an upstream being there, so we still have a dynamic dial
time out (by way higher then 200ms) of 1s; this should be fairly stable
for an upstream. The read timeout if more variable because of cached and
non cached responses. As such remove his logic entirely.

Drop to 2s read timeout.

Fixes #2306
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>
parent e94ce7a1
...@@ -69,14 +69,6 @@ func (t *Transport) Dial(proto string) (*dns.Conn, bool, error) { ...@@ -69,14 +69,6 @@ func (t *Transport) Dial(proto string) (*dns.Conn, bool, error) {
return conn, false, err return conn, false, err
} }
func (p *Proxy) readTimeout() time.Duration {
return limitTimeout(&p.avgRtt, minTimeout, maxTimeout)
}
func (p *Proxy) updateRtt(newRtt time.Duration) {
averageTimeout(&p.avgRtt, newRtt, cumulativeAvgWeight)
}
// Connect selects an upstream, sends the request and waits for a response. // Connect selects an upstream, sends the request and waits for a response.
func (p *Proxy) Connect(ctx context.Context, state request.Request, opts options) (*dns.Msg, error) { func (p *Proxy) Connect(ctx context.Context, state request.Request, opts options) (*dns.Msg, error) {
start := time.Now() start := time.Now()
...@@ -103,7 +95,6 @@ func (p *Proxy) Connect(ctx context.Context, state request.Request, opts options ...@@ -103,7 +95,6 @@ func (p *Proxy) Connect(ctx context.Context, state request.Request, opts options
} }
conn.SetWriteDeadline(time.Now().Add(maxTimeout)) conn.SetWriteDeadline(time.Now().Add(maxTimeout))
reqTime := time.Now()
if err := conn.WriteMsg(state.Req); err != nil { if err := conn.WriteMsg(state.Req); err != nil {
conn.Close() // not giving it back conn.Close() // not giving it back
if err == io.EOF && cached { if err == io.EOF && cached {
...@@ -112,10 +103,9 @@ func (p *Proxy) Connect(ctx context.Context, state request.Request, opts options ...@@ -112,10 +103,9 @@ func (p *Proxy) Connect(ctx context.Context, state request.Request, opts options
return nil, err return nil, err
} }
conn.SetReadDeadline(time.Now().Add(p.readTimeout())) conn.SetReadDeadline(time.Now().Add(readTimeout))
ret, err := conn.ReadMsg() ret, err := conn.ReadMsg()
if err != nil { if err != nil {
p.updateRtt(maxTimeout)
conn.Close() // not giving it back conn.Close() // not giving it back
if err == io.EOF && cached { if err == io.EOF && cached {
return nil, ErrCachedClosed return nil, ErrCachedClosed
...@@ -123,8 +113,6 @@ func (p *Proxy) Connect(ctx context.Context, state request.Request, opts options ...@@ -123,8 +113,6 @@ func (p *Proxy) Connect(ctx context.Context, state request.Request, opts options
return ret, err return ret, err
} }
p.updateRtt(time.Since(reqTime))
p.transport.Yield(conn) p.transport.Yield(conn)
rc, ok := dns.RcodeToString[ret.Rcode] rc, ok := dns.RcodeToString[ret.Rcode]
......
...@@ -144,9 +144,10 @@ func TestHealthMaxFails(t *testing.T) { ...@@ -144,9 +144,10 @@ func TestHealthMaxFails(t *testing.T) {
f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req) f.ServeDNS(context.TODO(), &test.ResponseWriter{}, req)
time.Sleep(1 * time.Second) time.Sleep(readTimeout + 1*time.Second)
fails := atomic.LoadUint32(&p.fails)
if !p.Down(f.maxfails) { if !p.Down(f.maxfails) {
t.Errorf("Expected Proxy fails to be greater than %d, got %d", f.maxfails, p.fails) t.Errorf("Expected Proxy fails to be greater than %d, got %d", f.maxfails, fails)
} }
} }
......
...@@ -31,7 +31,7 @@ type Transport struct { ...@@ -31,7 +31,7 @@ type Transport struct {
func newTransport(addr string) *Transport { func newTransport(addr string) *Transport {
t := &Transport{ t := &Transport{
avgDialTime: int64(defaultDialTimeout / 2), avgDialTime: int64(maxDialTimeout / 2),
conns: make(map[string][]*persistConn), conns: make(map[string][]*persistConn),
expire: defaultExpire, expire: defaultExpire,
addr: addr, addr: addr,
...@@ -160,7 +160,9 @@ func (t *Transport) SetTLSConfig(cfg *tls.Config) { t.tlsConfig = cfg } ...@@ -160,7 +160,9 @@ func (t *Transport) SetTLSConfig(cfg *tls.Config) { t.tlsConfig = cfg }
const ( const (
defaultExpire = 10 * time.Second defaultExpire = 10 * time.Second
minDialTimeout = 100 * time.Millisecond minDialTimeout = 1 * time.Second
maxDialTimeout = 30 * time.Second maxDialTimeout = 30 * time.Second
defaultDialTimeout = 30 * time.Second
// Some resolves might take quite a while, usually (cached) responses are fast. Set to 2s to give us some time to retry a different upstream.
readTimeout = 2 * time.Second
) )
...@@ -140,9 +140,9 @@ func TestCleanupAll(t *testing.T) { ...@@ -140,9 +140,9 @@ func TestCleanupAll(t *testing.T) {
tr := newTransport(s.Addr) tr := newTransport(s.Addr)
c1, _ := dns.DialTimeout("udp", tr.addr, defaultDialTimeout) c1, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout)
c2, _ := dns.DialTimeout("udp", tr.addr, defaultDialTimeout) c2, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout)
c3, _ := dns.DialTimeout("udp", tr.addr, defaultDialTimeout) c3, _ := dns.DialTimeout("udp", tr.addr, maxDialTimeout)
tr.conns["udp"] = []*persistConn{ tr.conns["udp"] = []*persistConn{
{c1, time.Now()}, {c1, time.Now()},
......
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
// Proxy defines an upstream host. // Proxy defines an upstream host.
type Proxy struct { type Proxy struct {
avgRtt int64
fails uint32 fails uint32
addr string addr string
...@@ -32,7 +31,6 @@ func NewProxy(addr, trans string) *Proxy { ...@@ -32,7 +31,6 @@ func NewProxy(addr, trans string) *Proxy {
fails: 0, fails: 0,
probe: up.New(), probe: up.New(),
transport: newTransport(addr), transport: newTransport(addr),
avgRtt: int64(maxTimeout / 2),
} }
p.health = NewHealthChecker(trans) p.health = NewHealthChecker(trans)
runtime.SetFinalizer(p, (*Proxy).finalizer) runtime.SetFinalizer(p, (*Proxy).finalizer)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment