| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | package proxy
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"crypto/tls"
 | 
					
						
							| 
									
										
										
										
											2018-05-18 09:46:14 +03:00
										 |  |  | 	"runtime"
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	"sync/atomic"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/up"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Proxy defines an upstream host.
 | 
					
						
							|  |  |  | type Proxy struct {
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 	fails     uint32
 | 
					
						
							|  |  |  | 	addr      string
 | 
					
						
							|  |  |  | 	proxyName string
 | 
					
						
							| 
									
										
										
										
											2018-06-05 17:21:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	transport *Transport
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	readTimeout time.Duration
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	// health checking
 | 
					
						
							| 
									
										
										
										
											2018-07-09 15:14:55 +01:00
										 |  |  | 	probe  *up.Probe
 | 
					
						
							|  |  |  | 	health HealthChecker
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewProxy returns a new proxy.
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | func NewProxy(proxyName, addr, trans string) *Proxy {
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	p := &Proxy{
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 		addr:        addr,
 | 
					
						
							|  |  |  | 		fails:       0,
 | 
					
						
							|  |  |  | 		probe:       up.New(),
 | 
					
						
							|  |  |  | 		readTimeout: 2 * time.Second,
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 		transport:   newTransport(proxyName, addr),
 | 
					
						
							|  |  |  | 		health:      NewHealthChecker(proxyName, trans, true, "."),
 | 
					
						
							|  |  |  | 		proxyName:   proxyName,
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 09:46:14 +03:00
										 |  |  | 	runtime.SetFinalizer(p, (*Proxy).finalizer)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	return p
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | func (p *Proxy) Addr() string { return p.addr }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-24 18:18:26 +01:00
										 |  |  | // SetTLSConfig sets the TLS config in the lower p.transport and in the healthchecking client.
 | 
					
						
							|  |  |  | func (p *Proxy) SetTLSConfig(cfg *tls.Config) {
 | 
					
						
							|  |  |  | 	p.transport.SetTLSConfig(cfg)
 | 
					
						
							| 
									
										
										
										
											2018-07-09 15:14:55 +01:00
										 |  |  | 	p.health.SetTLSConfig(cfg)
 | 
					
						
							| 
									
										
										
										
											2018-04-24 18:18:26 +01:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | // SetExpire sets the expire duration in the lower p.transport.
 | 
					
						
							|  |  |  | func (p *Proxy) SetExpire(expire time.Duration) { p.transport.SetExpire(expire) }
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | func (p *Proxy) GetHealthchecker() HealthChecker {
 | 
					
						
							|  |  |  | 	return p.health
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-27 16:43:30 +01:00
										 |  |  | func (p *Proxy) GetTransport() *Transport {
 | 
					
						
							|  |  |  | 	return p.transport
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | func (p *Proxy) Fails() uint32 {
 | 
					
						
							|  |  |  | 	return atomic.LoadUint32(&p.fails)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | // Healthcheck kicks of a round of health checks for this proxy.
 | 
					
						
							| 
									
										
										
										
											2018-07-09 15:14:55 +01:00
										 |  |  | func (p *Proxy) Healthcheck() {
 | 
					
						
							| 
									
										
										
										
											2018-10-09 22:50:30 +03:00
										 |  |  | 	if p.health == nil {
 | 
					
						
							|  |  |  | 		log.Warning("No healthchecker")
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 15:14:55 +01:00
										 |  |  | 	p.probe.Do(func() error {
 | 
					
						
							|  |  |  | 		return p.health.Check(p)
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | // Down returns true if this proxy is down, i.e. has *more* fails than maxfails.
 | 
					
						
							|  |  |  | func (p *Proxy) Down(maxfails uint32) bool {
 | 
					
						
							|  |  |  | 	if maxfails == 0 {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	fails := atomic.LoadUint32(&p.fails)
 | 
					
						
							|  |  |  | 	return fails > maxfails
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | // Stop close stops the health checking goroutine.
 | 
					
						
							|  |  |  | func (p *Proxy) Stop()      { p.probe.Stop() }
 | 
					
						
							| 
									
										
										
										
											2018-07-09 15:14:55 +01:00
										 |  |  | func (p *Proxy) finalizer() { p.transport.Stop() }
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | // Start starts the proxy's healthchecking.
 | 
					
						
							|  |  |  | func (p *Proxy) Start(duration time.Duration) {
 | 
					
						
							| 
									
										
										
										
											2018-05-26 01:00:11 +03:00
										 |  |  | 	p.probe.Start(duration)
 | 
					
						
							|  |  |  | 	p.transport.Start()
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | func (p *Proxy) SetReadTimeout(duration time.Duration) {
 | 
					
						
							|  |  |  | 	p.readTimeout = duration
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-16 22:08:56 +08:00
										 |  |  | // incrementFails increments the number of fails safely.
 | 
					
						
							|  |  |  | func (p *Proxy) incrementFails() {
 | 
					
						
							|  |  |  | 	curVal := atomic.LoadUint32(&p.fails)
 | 
					
						
							|  |  |  | 	if curVal > curVal+1 {
 | 
					
						
							|  |  |  | 		// overflow occurred, do not update the counter again
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	atomic.AddUint32(&p.fails, 1)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | const (
 | 
					
						
							| 
									
										
										
										
											2018-06-15 02:37:22 -04:00
										 |  |  | 	maxTimeout = 2 * time.Second
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | )
 |