Commit bcf4e63e authored by Jacob Vosmaer's avatar Jacob Vosmaer

Remove last uses of sync.Once

parent 69e801cf
......@@ -5,29 +5,24 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"sync"
)
type Proxy struct {
URL *url.URL
Version string
RoundTripper *badgateway.RoundTripper
_reverseProxy *httputil.ReverseProxy
configureReverseProxyOnce sync.Once
Version string
reverseProxy *httputil.ReverseProxy
}
func (p *Proxy) reverseProxy() *httputil.ReverseProxy {
p.configureReverseProxyOnce.Do(func() {
u := *p.URL // Make a copy of p.URL
u.Path = ""
p._reverseProxy = httputil.NewSingleHostReverseProxy(&u)
if p.RoundTripper != nil {
p._reverseProxy.Transport = p.RoundTripper
} else {
p._reverseProxy.Transport = badgateway.NewRoundTripper("", 0)
}
})
return p._reverseProxy
func NewProxy(myURL *url.URL, version string, roundTripper *badgateway.RoundTripper) *Proxy {
p := Proxy{Version: version}
u := *myURL // Make a copy of p.URL
u.Path = ""
p.reverseProxy = httputil.NewSingleHostReverseProxy(&u)
if roundTripper != nil {
p.reverseProxy.Transport = roundTripper
} else {
p.reverseProxy.Transport = badgateway.NewRoundTripper("", 0)
}
return &p
}
func HeaderClone(h http.Header) http.Header {
......@@ -50,5 +45,5 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rw := newSendFileResponseWriter(w, &req)
defer rw.Flush()
p.reverseProxy().ServeHTTP(&rw, &req)
p.reverseProxy.ServeHTTP(&rw, &req)
}
......@@ -57,7 +57,7 @@ func TestUploadHandlerForwardingRawData(t *testing.T) {
httpRequest.Header.Set(tempPathHeader, tempPath)
handleFileUploads(&proxy.Proxy{URL: helper.URLMustParse(ts.URL), Version: "123"}).ServeHTTP(response, httpRequest)
handleFileUploads(proxy.NewProxy(helper.URLMustParse(ts.URL), "123", nil)).ServeHTTP(response, httpRequest)
helper.AssertResponseCode(t, response, 202)
if response.Body.String() != "RESPONSE" {
t.Fatal("Expected RESPONSE in response body")
......@@ -131,7 +131,7 @@ func TestUploadHandlerRewritingMultiPartData(t *testing.T) {
httpRequest.Header.Set(tempPathHeader, tempPath)
response := httptest.NewRecorder()
handleFileUploads(&proxy.Proxy{URL: helper.URLMustParse(ts.URL), Version: "123"}).ServeHTTP(response, httpRequest)
handleFileUploads(proxy.NewProxy(helper.URLMustParse(ts.URL), "123", nil)).ServeHTTP(response, httpRequest)
helper.AssertResponseCode(t, response, 202)
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
......
......@@ -34,14 +34,14 @@ func (u *Upstream) configureRoutes() {
api := apipkg.NewAPI(
u.Backend,
u.Version,
u.RoundTripper(),
u.RoundTripper,
)
static := &staticpages.Static{u.DocumentRoot}
proxy := &proxypkg.Proxy{
URL: u.Backend,
Version: u.Version,
RoundTripper: u.RoundTripper(),
}
proxy := proxypkg.NewProxy(
u.Backend,
u.Version,
u.RoundTripper,
)
u.Routes = []route{
// Git Clone
......
......@@ -14,35 +14,29 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"
)
var DefaultBackend = helper.URLMustParse("http://localhost:8080")
type Upstream struct {
Backend *url.URL
Version string
Socket string
DocumentRoot string
DevelopmentMode bool
ProxyHeadersTimeout time.Duration
URLPrefix urlprefix.Prefix
Routes []route
roundtripper *badgateway.RoundTripper
configureRoundTripperOnce sync.Once
Backend *url.URL
Version string
DocumentRoot string
DevelopmentMode bool
URLPrefix urlprefix.Prefix
Routes []route
RoundTripper *badgateway.RoundTripper
}
func NewUpstream(backend *url.URL, socket string, version string, documentRoot string, developmentMode bool, proxyHeadersTimeout time.Duration) *Upstream {
up := Upstream{
Backend: backend,
Socket: socket,
Version: version,
DocumentRoot: documentRoot,
DevelopmentMode: developmentMode,
ProxyHeadersTimeout: proxyHeadersTimeout,
Backend: backend,
Version: version,
DocumentRoot: documentRoot,
DevelopmentMode: developmentMode,
RoundTripper: badgateway.NewRoundTripper(socket, proxyHeadersTimeout),
}
if backend == nil {
up.Backend = DefaultBackend
......@@ -60,14 +54,6 @@ func (u *Upstream) configureURLPrefix() {
u.URLPrefix = urlprefix.Prefix(relativeURLRoot)
}
func (u *Upstream) RoundTripper() *badgateway.RoundTripper {
u.configureRoundTripperOnce.Do(func() {
u.roundtripper = badgateway.NewRoundTripper(u.Socket, u.ProxyHeadersTimeout)
})
return u.roundtripper
}
func (u *Upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
w := newLoggingResponseWriter(ow)
defer w.Log(r)
......
......@@ -15,8 +15,8 @@ import (
"time"
)
func newProxy(url string) *proxy.Proxy {
return &proxy.Proxy{URL: helper.URLMustParse(url), Version: "123"}
func newProxy(url string, rt *badgateway.RoundTripper) *proxy.Proxy {
return proxy.NewProxy(helper.URLMustParse(url), "123", rt)
}
func TestProxyRequest(t *testing.T) {
......@@ -47,7 +47,7 @@ func TestProxyRequest(t *testing.T) {
httpRequest.Header.Set("Custom-Header", "test")
w := httptest.NewRecorder()
newProxy(ts.URL).ServeHTTP(w, httpRequest)
newProxy(ts.URL, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 202)
helper.AssertResponseBody(t, w, "RESPONSE")
......@@ -64,7 +64,7 @@ func TestProxyError(t *testing.T) {
httpRequest.Header.Set("Custom-Header", "test")
w := httptest.NewRecorder()
newProxy("http://localhost:655575/").ServeHTTP(w, httpRequest)
newProxy("http://localhost:655575/", nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 502)
helper.AssertResponseBody(t, w, "dial tcp: invalid port 655575")
}
......@@ -91,8 +91,7 @@ func TestProxyReadTimeout(t *testing.T) {
},
}
p := newProxy(ts.URL)
p.RoundTripper = rt
p := newProxy(ts.URL, rt)
w := httptest.NewRecorder()
p.ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 502)
......@@ -112,7 +111,7 @@ func TestProxyHandlerTimeout(t *testing.T) {
}
w := httptest.NewRecorder()
newProxy(ts.URL).ServeHTTP(w, httpRequest)
newProxy(ts.URL, nil).ServeHTTP(w, httpRequest)
helper.AssertResponseCode(t, w, 503)
helper.AssertResponseBody(t, w, "Request took too long")
}
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