~egtann/srp

b26f377adfec2a81fc5b9ba53358050b0ff19bf6 — Evan Tann 2 months ago 9734b13
add timeout control to transport
2 files changed, 8 insertions(+), 6 deletions(-)

M cmd/srp/main.go
M proxy.go
M cmd/srp/main.go => cmd/srp/main.go +1 -1
@@ 56,7 56,7 @@ func main() {
		os.Exit(1)
	}
	rand.Seed(time.Now().UnixNano())
	proxy := srp.NewProxy(&logger{}, reg)
	proxy := srp.NewProxy(&logger{}, reg, timeout)

	// Set up the API only if Subnet is configured and the internal
	// IP of the SRP server can be determined.

M proxy.go => proxy.go +7 -5
@@ 29,6 29,7 @@ import (
type ReverseProxy struct {
	rp       httputil.ReverseProxy
	reg      *Registry
	timeout  time.Duration
	jobCh    chan *healthCheck
	resultCh chan *healthCheck
	mu       sync.RWMutex


@@ 92,7 93,7 @@ type healthCheck struct {
}

// NewProxy from a given Registry.
func NewProxy(log Logger, reg *Registry) *ReverseProxy {
func NewProxy(log Logger, reg *Registry, timeout time.Duration) *ReverseProxy {
	director := func(req *http.Request) {
		req.URL.Scheme = "http"
		req.URL.Host = req.Host


@@ 105,7 106,7 @@ func NewProxy(log Logger, reg *Registry) *ReverseProxy {
		log.ReqPrintf(reqID, "%s requested %s %s", req.RemoteAddr,
			req.Method, req.Host)
	}
	transport := newTransport(reg)
	transport := newTransport(reg, timeout)
	errorHandler := func(w http.ResponseWriter, r *http.Request, err error) {
		w.WriteHeader(http.StatusBadGateway)
		msg := fmt.Sprintf("http: proxy error: %s %s: %v", r.Method, r.URL, err)


@@ 130,6 131,7 @@ func NewProxy(log Logger, reg *Registry) *ReverseProxy {
		rp:       rp,
		log:      log,
		reg:      reg,
		timeout:  timeout,
		jobCh:    jobCh,
		resultCh: resultCh,
	}


@@ 335,7 337,7 @@ func (r *ReverseProxy) UpdateRegistry(reg *Registry) {
	defer r.mu.Unlock()

	r.reg = reg
	r.rp.Transport = newTransport(reg)
	r.rp.Transport = newTransport(reg, r.timeout)
}

func ping(job *healthCheck) error {


@@ 361,9 363,9 @@ func ping(job *healthCheck) error {
	return nil
}

func newTransport(reg *Registry) http.RoundTripper {
func newTransport(reg *Registry, timeout time.Duration) http.RoundTripper {
	transport := cleanhttp.DefaultTransport()
	transport.ResponseHeaderTimeout = 30 * time.Second
	transport.ResponseHeaderTimeout = timeout
	transport.DialContext = func(
		ctx context.Context,
		network, addr string,