| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | // Package forward implements a forwarding proxy. It caches an upstream net.Conn for some time, so if the same
 | 
					
						
							|  |  |  | // client returns the upstream's Conn will be precached. Depending on how you benchmark this looks to be
 | 
					
						
							|  |  |  | // 50% faster than just openening a new connection for every client. It works with UDP and TCP and uses
 | 
					
						
							|  |  |  | // inband healthchecking.
 | 
					
						
							|  |  |  | package forward
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 	"io"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | 	"sync/atomic"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | // limitTimeout is a utility function to auto-tune timeout values
 | 
					
						
							|  |  |  | // average observed time is moved towards the last observed delay moderated by a weight
 | 
					
						
							|  |  |  | // next timeout to use will be the double of the computed average, limited by min and max frame.
 | 
					
						
							|  |  |  | func limitTimeout(currentAvg *int64, minValue time.Duration, maxValue time.Duration) time.Duration {
 | 
					
						
							|  |  |  | 	rt := time.Duration(atomic.LoadInt64(currentAvg))
 | 
					
						
							|  |  |  | 	if rt < minValue {
 | 
					
						
							|  |  |  | 		return minValue
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if rt < maxValue/2 {
 | 
					
						
							|  |  |  | 		return 2 * rt
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return maxValue
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func averageTimeout(currentAvg *int64, observedDuration time.Duration, weight int64) {
 | 
					
						
							|  |  |  | 	dt := time.Duration(atomic.LoadInt64(currentAvg))
 | 
					
						
							|  |  |  | 	atomic.AddInt64(currentAvg, int64(observedDuration-dt)/weight)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *transport) dialTimeout() time.Duration {
 | 
					
						
							|  |  |  | 	return limitTimeout(&t.avgDialTime, minDialTimeout, maxDialTimeout)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (t *transport) updateDialTimeout(newDialTime time.Duration) {
 | 
					
						
							|  |  |  | 	averageTimeout(&t.avgDialTime, newDialTime, cumulativeAvgWeight)
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-04-16 19:51:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | // Dial dials the address configured in transport, potentially reusing a connection or creating a new one.
 | 
					
						
							|  |  |  | func (t *transport) Dial(proto string) (*dns.Conn, bool, error) {
 | 
					
						
							|  |  |  | 	// If tls has been configured; use it.
 | 
					
						
							|  |  |  | 	if t.tlsConfig != nil {
 | 
					
						
							|  |  |  | 		proto = "tcp-tls"
 | 
					
						
							| 
									
										
										
										
											2018-04-16 19:51:49 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	t.dial <- proto
 | 
					
						
							|  |  |  | 	c := <-t.ret
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if c != nil {
 | 
					
						
							|  |  |  | 		return c, true, nil
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	reqTime := time.Now()
 | 
					
						
							|  |  |  | 	timeout := t.dialTimeout()
 | 
					
						
							|  |  |  | 	if proto == "tcp-tls" {
 | 
					
						
							|  |  |  | 		conn, err := dns.DialTimeoutWithTLS("tcp", t.addr, t.tlsConfig, timeout)
 | 
					
						
							|  |  |  | 		t.updateDialTimeout(time.Since(reqTime))
 | 
					
						
							|  |  |  | 		return conn, false, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	conn, err := dns.DialTimeout(proto, t.addr, timeout)
 | 
					
						
							|  |  |  | 	t.updateDialTimeout(time.Since(reqTime))
 | 
					
						
							|  |  |  | 	return conn, false, err
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *Proxy) readTimeout() time.Duration {
 | 
					
						
							|  |  |  | 	return limitTimeout(&p.avgRtt, minTimeout, maxTimeout)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p *Proxy) updateRtt(newRtt time.Duration) {
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | 	averageTimeout(&p.avgRtt, newRtt, cumulativeAvgWeight)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 08:47:26 +03:00
										 |  |  | // 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) {
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	start := time.Now()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	proto := state.Proto()
 | 
					
						
							|  |  |  | 	if forceTCP {
 | 
					
						
							|  |  |  | 		proto = "tcp"
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 	conn, cached, err := p.Dial(proto)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-04-26 09:34:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	// Set buffer size correctly for this client.
 | 
					
						
							|  |  |  | 	conn.UDPSize = uint16(state.Size())
 | 
					
						
							|  |  |  | 	if conn.UDPSize < 512 {
 | 
					
						
							|  |  |  | 		conn.UDPSize = 512
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	conn.SetWriteDeadline(time.Now().Add(timeout))
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | 	reqTime := time.Now()
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	if err := conn.WriteMsg(state.Req); err != nil {
 | 
					
						
							|  |  |  | 		conn.Close() // not giving it back
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 		if err == io.EOF && cached {
 | 
					
						
							| 
									
										
										
										
											2018-05-09 14:41:14 +03:00
										 |  |  | 			return nil, ErrCachedClosed
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | 	conn.SetReadDeadline(time.Now().Add(p.readTimeout()))
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	ret, err := conn.ReadMsg()
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | 		p.updateRtt(timeout)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		conn.Close() // not giving it back
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 		if err == io.EOF && cached {
 | 
					
						
							| 
									
										
										
										
											2018-05-09 14:41:14 +03:00
										 |  |  | 			return nil, ErrCachedClosed
 | 
					
						
							| 
									
										
										
										
											2018-04-06 15:41:48 +03:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-04-12 21:17:05 +02:00
										 |  |  | 		return ret, err
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | 	p.updateRtt(time.Since(reqTime))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	p.Yield(conn)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if metric {
 | 
					
						
							|  |  |  | 		rc, ok := dns.RcodeToString[ret.Rcode]
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			rc = strconv.Itoa(ret.Rcode)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 		RequestCount.WithLabelValues(p.addr).Add(1)
 | 
					
						
							|  |  |  | 		RcodeCount.WithLabelValues(rc, p.addr).Add(1)
 | 
					
						
							|  |  |  | 		RequestDuration.WithLabelValues(p.addr).Observe(time.Since(start).Seconds())
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | const cumulativeAvgWeight = 4
 |