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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kirill Smelkov
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
Show 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 (
"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
reverseProxy
*
httputil
.
ReverseProxy
}
func
(
p
*
Proxy
)
reverseProxy
()
*
httputil
.
Reverse
Proxy
{
p
.
configureReverseProxyOnce
.
Do
(
func
()
{
u
:=
*
p
.
URL
// Make a copy of p.URL
func
NewProxy
(
myURL
*
url
.
URL
,
version
string
,
roundTripper
*
badgateway
.
RoundTripper
)
*
Proxy
{
p
:=
Proxy
{
Version
:
version
}
u
:=
*
my
URL
// Make a copy of p.URL
u
.
Path
=
""
p
.
_
reverseProxy
=
httputil
.
NewSingleHostReverseProxy
(
&
u
)
if
p
.
R
oundTripper
!=
nil
{
p
.
_reverseProxy
.
Transport
=
p
.
R
oundTripper
p
.
reverseProxy
=
httputil
.
NewSingleHostReverseProxy
(
&
u
)
if
r
oundTripper
!=
nil
{
p
.
reverseProxy
.
Transport
=
r
oundTripper
}
else
{
p
.
_
reverseProxy
.
Transport
=
badgateway
.
NewRoundTripper
(
""
,
0
)
p
.
reverseProxy
.
Transport
=
badgateway
.
NewRoundTripper
(
""
,
0
)
}
})
return
p
.
_reverseProxy
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
)
}
internal/upload/uploads_test.go
View file @
bcf4e63e
...
...
@@ -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
)
{
...
...
internal/upstream/routes.go
View file @
bcf4e63e
...
...
@@ -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
...
...
internal/upstream/upstream.go
View file @
bcf4e63e
...
...
@@ -14,7 +14,6 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"
)
...
...
@@ -23,26 +22,21 @@ 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
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
,
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
)
...
...
proxy_test.go
View file @
bcf4e63e
...
...
@@ -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"
)
}
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