Commit bcf4e63e authored by Jacob Vosmaer's avatar Jacob Vosmaer

Remove last uses of sync.Once

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