Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kazuhiko Shiozaki
gitlab-workhorse
Commits
bcf4e63e
Commit
bcf4e63e
authored
Jan 13, 2016
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove last uses of sync.Once
parent
69e801cf
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
41 additions
and
61 deletions
+41
-61
internal/proxy/proxy.go
internal/proxy/proxy.go
+14
-19
internal/upload/uploads_test.go
internal/upload/uploads_test.go
+2
-2
internal/upstream/routes.go
internal/upstream/routes.go
+6
-6
internal/upstream/upstream.go
internal/upstream/upstream.go
+13
-27
proxy_test.go
proxy_test.go
+6
-7
No files found.
internal/proxy/proxy.go
View file @
bcf4e63e
...
@@ -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
reverseProxy
*
httputil
.
ReverseProxy
RoundTripper
*
badgateway
.
RoundTripper
_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
)
}
}
internal/upload/uploads_test.go
View file @
bcf4e63e
...
@@ -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
)
{
...
...
internal/upstream/routes.go
View file @
bcf4e63e
...
@@ -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
...
...
internal/upstream/upstream.go
View file @
bcf4e63e
...
@@ -14,35 +14,29 @@ import (
...
@@ -14,35 +14,29 @@ import (
"net/http"
"net/http"
"net/url"
"net/url"
"strings"
"strings"
"sync"
"time"
"time"
)
)
var
DefaultBackend
=
helper
.
URLMustParse
(
"http://localhost:8080"
)
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
Routes
[]
route
URLPrefix
urlprefix
.
Prefix
RoundTripper
*
badgateway
.
RoundTripper
Routes
[]
route
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
,
RoundTripper
:
badgateway
.
NewRoundTripper
(
socket
,
proxyHeadersTimeout
),
ProxyHeadersTimeout
:
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
)
...
...
proxy_test.go
View file @
bcf4e63e
...
@@ -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"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment