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

Eagerly initialize badgateway.RoundTripper

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