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
Hide 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
import
(
"encoding/json"
"errors"
"net/http"
"time"
...
...
@@ -10,10 +12,8 @@ import (
)
const
(
maxRegisterBodySize
=
4
*
1024
runnerBuildQueue
=
"runner:build_queue:"
runnerBuildQueueKey
=
"token"
runnerBuildQueueValue
=
"X-GitLab-Last-Update"
maxRegisterBodySize
=
4
*
1024
runnerBuildQueue
=
"runner:build_queue:"
)
var
(
...
...
@@ -43,14 +43,9 @@ func init() {
)
}
func
readRunnerToken
(
r
*
http
.
Request
)
(
string
,
error
)
{
err
:=
r
.
ParseForm
()
if
err
!=
nil
{
return
""
,
err
}
token
:=
r
.
FormValue
(
runnerBuildQueueKey
)
return
token
,
nil
type
runnerRequest
struct
{
Token
string
`json:"token,omitempty"`
LastUpdate
string
`json:"last_update,omitempty"`
}
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
)
}
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
)
{
registerHandlerOpen
.
WithLabelValues
(
"proxying"
)
.
Inc
()
defer
registerHandlerOpen
.
WithLabelValues
(
"proxying"
)
.
Dec
()
...
...
@@ -80,13 +90,6 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
}
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
)
if
err
!=
nil
{
registerHandlerHits
.
WithLabelValues
(
"body-read-error"
)
.
Inc
()
...
...
@@ -94,14 +97,20 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
return
}
runner
Token
,
err
:=
readRunnerToken
(
helper
.
CloneRequestWithNewBody
(
r
,
requestBody
)
)
if
runnerToken
==
""
||
err
!=
nil
{
runner
Request
,
err
:=
readRunnerRequest
(
r
,
requestBody
)
if
err
!=
nil
{
registerHandlerHits
.
WithLabelValues
(
"body-parse-error"
)
.
Inc
()
proxyRegisterRequest
(
h
,
w
,
r
)
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
{
registerHandlerHits
.
WithLabelValues
(
"watch-error"
)
.
Inc
()
helper
.
Fail500
(
w
,
r
,
&
watchError
{
err
})
...
...
@@ -122,18 +131,18 @@ func RegisterHandler(h http.Handler, pollingDuration time.Duration) http.Handler
// whether the connection is not dead
case
redis
.
WatchKeyStatusSeenChange
:
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,
// so we return to runner 204, which means nothing got changed,
// and there's no new builds to process
case
redis
.
WatchKeyStatusTimeout
:
registerHandlerHits
.
WithLabelValues
(
"timeout"
)
.
Inc
()
w
.
WriteHeader
(
204
)
w
.
WriteHeader
(
http
.
StatusNoContent
)
case
redis
.
WatchKeyStatusNoChange
:
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