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
1
Merge Requests
1
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
nexedi
gitlab-workhorse
Commits
e837d788
Commit
e837d788
authored
Mar 01, 2017
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Read json instead of application/form-encoded
parent
155d1b6f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
25 deletions
+34
-25
internal/builds/register.go
internal/builds/register.go
+34
-25
No files found.
internal/builds/register.go
View file @
e837d788
package
builds
package
builds
import
(
import
(
"encoding/json"
"errors"
"net/http"
"net/http"
"time"
"time"
...
@@ -12,8 +14,6 @@ import (
...
@@ -12,8 +14,6 @@ import (
const
(
const
(
maxRegisterBodySize
=
4
*
1024
maxRegisterBodySize
=
4
*
1024
runnerBuildQueue
=
"runner:build_queue:"
runnerBuildQueue
=
"runner:build_queue:"
runnerBuildQueueKey
=
"token"
runnerBuildQueueValue
=
"X-GitLab-Last-Update"
)
)
var
(
var
(
...
@@ -43,14 +43,9 @@ func init() {
...
@@ -43,14 +43,9 @@ func init() {
)
)
}
}
func
readRunnerToken
(
r
*
http
.
Request
)
(
string
,
error
)
{
type
runnerRequest
struct
{
err
:=
r
.
ParseForm
()
Token
string
`json:"token,omitempty"`
if
err
!=
nil
{
LastUpdate
string
`json:"last_update,omitempty"`
return
""
,
err
}
token
:=
r
.
FormValue
(
runnerBuildQueueKey
)
return
token
,
nil
}
}
func
readRunnerBody
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
([]
byte
,
error
)
{
func
readRunnerBody
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
([]
byte
,
error
)
{
...
@@ -60,6 +55,21 @@ func readRunnerBody(w http.ResponseWriter, r *http.Request) ([]byte, error) {
...
@@ -60,6 +55,21 @@ func readRunnerBody(w http.ResponseWriter, r *http.Request) ([]byte, error) {
return
helper
.
ReadRequestBody
(
w
,
r
,
maxRegisterBodySize
)
return
helper
.
ReadRequestBody
(
w
,
r
,
maxRegisterBodySize
)
}
}
func
readRunnerRequest
(
r
*
http
.
Request
,
body
[]
byte
)
(
runnerRequest
,
error
)
{
var
runnerRequest
runnerRequest
if
r
.
Header
.
Get
(
"Content-Type"
)
!=
"application/json"
{
return
runnerRequest
,
errors
.
New
(
"invalid content-type received"
)
}
err
:=
json
.
Unmarshal
(
body
,
&
runnerRequest
)
if
err
!=
nil
{
return
runnerRequest
,
err
}
return
runnerRequest
,
nil
}
func
proxyRegisterRequest
(
h
http
.
Handler
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
proxyRegisterRequest
(
h
http
.
Handler
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
registerHandlerOpen
.
WithLabelValues
(
"proxying"
)
.
Inc
()
registerHandlerOpen
.
WithLabelValues
(
"proxying"
)
.
Inc
()
defer
registerHandlerOpen
.
WithLabelValues
(
"proxying"
)
.
Dec
()
defer
registerHandlerOpen
.
WithLabelValues
(
"proxying"
)
.
Dec
()
...
@@ -80,13 +90,6 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
...
@@ -80,13 +90,6 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
}
}
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
lastUpdate
:=
r
.
Header
.
Get
(
runnerBuildQueueValue
)
if
lastUpdate
==
""
{
registerHandlerHits
.
WithLabelValues
(
"missing-value"
)
.
Inc
()
proxyRegisterRequest
(
h
,
w
,
r
)
return
}
requestBody
,
err
:=
readRunnerBody
(
w
,
r
)
requestBody
,
err
:=
readRunnerBody
(
w
,
r
)
if
err
!=
nil
{
if
err
!=
nil
{
registerHandlerHits
.
WithLabelValues
(
"body-read-error"
)
.
Inc
()
registerHandlerHits
.
WithLabelValues
(
"body-read-error"
)
.
Inc
()
...
@@ -94,14 +97,20 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
...
@@ -94,14 +97,20 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
return
return
}
}
runner
Token
,
err
:=
readRunnerToken
(
helper
.
CloneRequestWithNewBody
(
r
,
requestBody
)
)
runner
Request
,
err
:=
readRunnerRequest
(
r
,
requestBody
)
if
runnerToken
==
""
||
err
!=
nil
{
if
err
!=
nil
{
registerHandlerHits
.
WithLabelValues
(
"body-parse-error"
)
.
Inc
()
registerHandlerHits
.
WithLabelValues
(
"body-parse-error"
)
.
Inc
()
proxyRegisterRequest
(
h
,
w
,
r
)
proxyRegisterRequest
(
h
,
w
,
r
)
return
return
}
}
result
,
err
:=
watchForRunnerChange
(
runnerToken
,
lastUpdate
,
pollingDuration
)
if
runnerRequest
.
Token
==
""
||
runnerRequest
.
LastUpdate
==
""
{
registerHandlerHits
.
WithLabelValues
(
"missing-values"
)
.
Inc
()
proxyRegisterRequest
(
h
,
w
,
r
)
return
}
result
,
err
:=
watchForRunnerChange
(
runnerRequest
.
Token
,
runnerRequest
.
LastUpdate
,
pollingDuration
)
if
err
!=
nil
{
if
err
!=
nil
{
registerHandlerHits
.
WithLabelValues
(
"watch-error"
)
.
Inc
()
registerHandlerHits
.
WithLabelValues
(
"watch-error"
)
.
Inc
()
helper
.
Fail500
(
w
,
r
,
&
watchError
{
err
})
helper
.
Fail500
(
w
,
r
,
&
watchError
{
err
})
...
@@ -122,18 +131,18 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
...
@@ -122,18 +131,18 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
// whether the connection is not dead
// whether the connection is not dead
case
redis
.
WatchKeyStatusSeenChange
:
case
redis
.
WatchKeyStatusSeenChange
:
registerHandlerHits
.
WithLabelValues
(
"seen-change"
)
.
Inc
()
registerHandlerHits
.
WithLabelValues
(
"seen-change"
)
.
Inc
()
w
.
WriteHeader
(
204
)
w
.
WriteHeader
(
http
.
StatusNoContent
)
// When we receive one of these statuses, it means that we detected no change,
// When we receive one of these statuses, it means that we detected no change,
// so we return to runner 204, which means nothing got changed,
// so we return to runner 204, which means nothing got changed,
// and there's no new builds to process
// and there's no new builds to process
case
redis
.
WatchKeyStatusTimeout
:
case
redis
.
WatchKeyStatusTimeout
:
registerHandlerHits
.
WithLabelValues
(
"timeout"
)
.
Inc
()
registerHandlerHits
.
WithLabelValues
(
"timeout"
)
.
Inc
()
w
.
WriteHeader
(
204
)
w
.
WriteHeader
(
http
.
StatusNoContent
)
case
redis
.
WatchKeyStatusNoChange
:
case
redis
.
WatchKeyStatusNoChange
:
registerHandlerHits
.
WithLabelValues
(
"no-change"
)
.
Inc
()
registerHandlerHits
.
WithLabelValues
(
"no-change"
)
.
Inc
()
w
.
WriteHeader
(
204
)
w
.
WriteHeader
(
http
.
StatusNoContent
)
}
}
})
})
}
}
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