| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | package forward
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	"crypto/tls"
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 	"sort"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // a persistConn hold the dns.Conn and the last used time.
 | 
					
						
							|  |  |  | type persistConn struct {
 | 
					
						
							|  |  |  | 	c    *dns.Conn
 | 
					
						
							|  |  |  | 	used time.Time
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | // Transport hold the persistent cache.
 | 
					
						
							|  |  |  | type Transport struct {
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 	avgDialTime int64                          // kind of average time of dial time
 | 
					
						
							|  |  |  | 	conns       [typeTotalCount][]*persistConn // Buckets for udp, tcp and tcp-tls.
 | 
					
						
							|  |  |  | 	expire      time.Duration                  // After this duration a connection is expired.
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | 	addr        string
 | 
					
						
							|  |  |  | 	tlsConfig   *tls.Config
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	dial  chan string
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 	yield chan *persistConn
 | 
					
						
							|  |  |  | 	ret   chan *persistConn
 | 
					
						
							| 
									
										
										
										
											2018-04-24 16:10:31 +01:00
										 |  |  | 	stop  chan bool
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | func newTransport(addr string) *Transport {
 | 
					
						
							|  |  |  | 	t := &Transport{
 | 
					
						
							| 
									
										
										
										
											2018-11-20 08:48:56 +01:00
										 |  |  | 		avgDialTime: int64(maxDialTimeout / 2),
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 		conns:       [typeTotalCount][]*persistConn{},
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | 		expire:      defaultExpire,
 | 
					
						
							|  |  |  | 		addr:        addr,
 | 
					
						
							|  |  |  | 		dial:        make(chan string),
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 		yield:       make(chan *persistConn),
 | 
					
						
							|  |  |  | 		ret:         make(chan *persistConn),
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | 		stop:        make(chan bool),
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return t
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // connManagers manages the persistent connection cache for UDP and TCP.
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | func (t *Transport) connManager() {
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 	ticker := time.NewTicker(t.expire)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | Wait:
 | 
					
						
							|  |  |  | 	for {
 | 
					
						
							|  |  |  | 		select {
 | 
					
						
							|  |  |  | 		case proto := <-t.dial:
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 			transtype := stringToTransportType(proto)
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 			// take the last used conn - complexity O(1)
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 			if stack := t.conns[transtype]; len(stack) > 0 {
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 				pc := stack[len(stack)-1]
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 				if time.Since(pc.used) < t.expire {
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 					// Found one, remove from pool and return this conn.
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 					t.conns[transtype] = stack[:len(stack)-1]
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 					t.ret <- pc
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 					continue Wait
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 				// clear entire cache if the last conn is expired
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 				t.conns[transtype] = nil
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 				// now, the connections being passed to closeConns() are not reachable from
 | 
					
						
							|  |  |  | 				// transport methods anymore. So, it's safe to close them in a separate goroutine
 | 
					
						
							|  |  |  | 				go closeConns(stack)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-04-26 09:34:58 +01:00
										 |  |  | 			t.ret <- nil
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 		case pc := <-t.yield:
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 			transtype := t.transportTypeFromConn(pc)
 | 
					
						
							|  |  |  | 			t.conns[transtype] = append(t.conns[transtype], pc)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 		case <-ticker.C:
 | 
					
						
							|  |  |  | 			t.cleanup(false)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		case <-t.stop:
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 			t.cleanup(true)
 | 
					
						
							| 
									
										
										
										
											2018-04-26 09:34:58 +01:00
										 |  |  | 			close(t.ret)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | // closeConns closes connections.
 | 
					
						
							|  |  |  | func closeConns(conns []*persistConn) {
 | 
					
						
							|  |  |  | 	for _, pc := range conns {
 | 
					
						
							|  |  |  | 		pc.c.Close()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // cleanup removes connections from cache.
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | func (t *Transport) cleanup(all bool) {
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 	staleTime := time.Now().Add(-t.expire)
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 	for transtype, stack := range t.conns {
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 		if len(stack) == 0 {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if all {
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 			t.conns[transtype] = nil
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 			// now, the connections being passed to closeConns() are not reachable from
 | 
					
						
							|  |  |  | 			// transport methods anymore. So, it's safe to close them in a separate goroutine
 | 
					
						
							|  |  |  | 			go closeConns(stack)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if stack[0].used.After(staleTime) {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// connections in stack are sorted by "used"
 | 
					
						
							|  |  |  | 		good := sort.Search(len(stack), func(i int) bool {
 | 
					
						
							|  |  |  | 			return stack[i].used.After(staleTime)
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							| 
									
										
										
										
											2019-10-01 20:45:52 +01:00
										 |  |  | 		t.conns[transtype] = stack[good:]
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 		// now, the connections being passed to closeConns() are not reachable from
 | 
					
						
							|  |  |  | 		// transport methods anymore. So, it's safe to close them in a separate goroutine
 | 
					
						
							|  |  |  | 		go closeConns(stack[:good])
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-09 15:24:18 +08:00
										 |  |  | // It is hard to pin a value to this, the import thing is to no block forever, losing at cached connection is not terrible.
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | const yieldTimeout = 25 * time.Millisecond
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | // Yield return the connection to transport for reuse.
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | func (t *Transport) Yield(pc *persistConn) {
 | 
					
						
							|  |  |  | 	pc.used = time.Now() // update used time
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-09 15:24:18 +08:00
										 |  |  | 	// Make this non-blocking, because in the case of a very busy forwarder we will *block* on this yield. This
 | 
					
						
							| 
									
										
										
										
											2019-10-01 16:39:42 +01:00
										 |  |  | 	// blocks the outer go-routine and stuff will just pile up.  We timeout when the send fails to as returning
 | 
					
						
							|  |  |  | 	// these connection is an optimization anyway.
 | 
					
						
							|  |  |  | 	select {
 | 
					
						
							|  |  |  | 	case t.yield <- pc:
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	case <-time.After(yieldTimeout):
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | // Start starts the transport's connection manager.
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | func (t *Transport) Start() { go t.connManager() }
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | // Stop stops the transport's connection manager.
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | func (t *Transport) Stop() { close(t.stop) }
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // SetExpire sets the connection expire time in transport.
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | func (t *Transport) SetExpire(expire time.Duration) { t.expire = expire }
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // SetTLSConfig sets the TLS config in transport.
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | func (t *Transport) SetTLSConfig(cfg *tls.Config) { t.tlsConfig = cfg }
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | const (
 | 
					
						
							| 
									
										
										
										
											2018-11-20 08:48:56 +01:00
										 |  |  | 	defaultExpire  = 10 * time.Second
 | 
					
						
							|  |  |  | 	minDialTimeout = 1 * time.Second
 | 
					
						
							|  |  |  | 	maxDialTimeout = 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
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | )
 |