Commit 69e801cf authored by Jacob Vosmaer's avatar Jacob Vosmaer

Eagerly initialize badgateway.RoundTripper

parent 9f0b3f71
...@@ -19,16 +19,15 @@ type API struct { ...@@ -19,16 +19,15 @@ type API struct {
Version string Version string
} }
func NewAPI(myURL *url.URL, version string, roundtripper *badgateway.RoundTripper) *API { func NewAPI(myURL *url.URL, version string, roundTripper *badgateway.RoundTripper) *API {
a := API{ if roundTripper == nil {
Client: &http.Client{Transport: &badgateway.RoundTripper{}}, roundTripper = badgateway.NewRoundTripper("", 0)
}
return &API{
Client: &http.Client{Transport: roundTripper},
URL: myURL, URL: myURL,
Version: version, Version: version,
} }
if roundtripper != nil {
a.Client.Transport = roundtripper
}
return &a
} }
type HandleFunc func(http.ResponseWriter, *http.Request, *Response) type HandleFunc func(http.ResponseWriter, *http.Request, *Response)
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"sync"
"time" "time"
) )
...@@ -24,15 +23,22 @@ var DefaultTransport = &http.Transport{ ...@@ -24,15 +23,22 @@ var DefaultTransport = &http.Transport{
} }
type RoundTripper struct { type RoundTripper struct {
Socket string Transport *http.Transport
ProxyHeadersTimeout time.Duration
Transport *http.Transport
configureRoundTripperOnce sync.Once
} }
func (t *RoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error) { func NewRoundTripper(socket string, proxyHeadersTimeout time.Duration) *RoundTripper {
t.configureRoundTripperOnce.Do(t.configureRoundTripper) tr := *DefaultTransport
tr.ResponseHeaderTimeout = proxyHeadersTimeout
if socket != "" {
tr.Dial = func(_, _ string) (net.Conn, error) {
return DefaultDialer.Dial("unix", socket)
}
}
return &RoundTripper{Transport: &tr}
}
func (t *RoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error) {
res, err = t.Transport.RoundTrip(r) res, err = t.Transport.RoundTrip(r)
// httputil.ReverseProxy translates all errors from this // httputil.ReverseProxy translates all errors from this
...@@ -61,20 +67,3 @@ func (t *RoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error ...@@ -61,20 +67,3 @@ func (t *RoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error
} }
return return
} }
func (t *RoundTripper) configureRoundTripper() {
if t.Transport != nil {
return
}
tr := *DefaultTransport
tr.ResponseHeaderTimeout = t.ProxyHeadersTimeout
if t.Socket != "" {
tr.Dial = func(_, _ string) (net.Conn, error) {
return DefaultDialer.Dial("unix", t.Socket)
}
}
t.Transport = &tr
}
...@@ -24,7 +24,7 @@ func (p *Proxy) reverseProxy() *httputil.ReverseProxy { ...@@ -24,7 +24,7 @@ func (p *Proxy) reverseProxy() *httputil.ReverseProxy {
if p.RoundTripper != nil { if p.RoundTripper != nil {
p._reverseProxy.Transport = p.RoundTripper p._reverseProxy.Transport = p.RoundTripper
} else { } else {
p._reverseProxy.Transport = &badgateway.RoundTripper{} p._reverseProxy.Transport = badgateway.NewRoundTripper("", 0)
} }
}) })
return p._reverseProxy return p._reverseProxy
......
...@@ -62,10 +62,7 @@ func (u *Upstream) configureURLPrefix() { ...@@ -62,10 +62,7 @@ func (u *Upstream) configureURLPrefix() {
func (u *Upstream) RoundTripper() *badgateway.RoundTripper { func (u *Upstream) RoundTripper() *badgateway.RoundTripper {
u.configureRoundTripperOnce.Do(func() { u.configureRoundTripperOnce.Do(func() {
u.roundtripper = &badgateway.RoundTripper{ u.roundtripper = badgateway.NewRoundTripper(u.Socket, u.ProxyHeadersTimeout)
Socket: u.Socket,
ProxyHeadersTimeout: u.ProxyHeadersTimeout,
}
}) })
return u.roundtripper return u.roundtripper
......
...@@ -511,7 +511,7 @@ func startWorkhorseServer(authBackend string) *httptest.Server { ...@@ -511,7 +511,7 @@ func startWorkhorseServer(authBackend string) *httptest.Server {
"123", "123",
testDocumentRoot, testDocumentRoot,
false, false,
time.Minute, 0,
) )
return httptest.NewServer(u) return httptest.NewServer(u)
} }
......
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