Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
容器镜像库
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
2ff811ed
未验证
提交
2ff811ed
编辑于
8 years ago
作者:
Kamil Trzciński
浏览文件
操作
下载
补丁
差异文件
Update after review
上级
378dcc60
No related branches found
分支 包含提交
No related tags found
标签 包含提交
无相关合并请求
变更
3
隐藏空白变更内容
行内
左右并排
显示
3 个更改的文件
internal/builds/register.go
+7
-12
7 个添加, 12 个删除
internal/builds/register.go
internal/builds/register_test.go
+12
-14
12 个添加, 14 个删除
internal/builds/register_test.go
internal/helper/helpers.go
+1
-0
1 个添加, 0 个删除
internal/helper/helpers.go
有
20 个添加
和
26 个删除
internal/builds/register.go
+
7
−
12
浏览文件 @
2ff811ed
...
...
@@ -31,15 +31,11 @@ var (
},
[]
string
{
"state"
},
)
)
var
(
registerHandlerOpenAtReading
=
registerHandlerOpen
.
WithLabelValues
(
"reading"
)
registerHandlerOpenAtProxying
=
registerHandlerOpen
.
WithLabelValues
(
"proxying"
)
registerHandlerOpenAtWatching
=
registerHandlerOpen
.
WithLabelValues
(
"watching"
)
)
var
(
registerHandlerBodyReadErrors
=
registerHandlerHits
.
WithLabelValues
(
"body-read-error"
)
registerHandlerBodyParseErrors
=
registerHandlerHits
.
WithLabelValues
(
"body-parse-error"
)
registerHandlerMissingValues
=
registerHandlerHits
.
WithLabelValues
(
"missing-values"
)
...
...
@@ -74,19 +70,18 @@ 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
func
readRunnerRequest
(
r
*
http
.
Request
,
body
[]
byte
)
(
*
runnerRequest
,
error
)
{
if
!
helper
.
IsApplicationJson
(
r
)
{
return
runnerRequest
,
errors
.
New
(
"invalid content-type received"
)
return
nil
,
errors
.
New
(
"invalid content-type received"
)
}
var
runnerRequest
runnerRequest
err
:=
json
.
Unmarshal
(
body
,
&
runnerRequest
)
if
err
!=
nil
{
return
runnerRequest
,
err
return
nil
,
err
}
return
runnerRequest
,
nil
return
&
runnerRequest
,
nil
}
func
proxyRegisterRequest
(
h
http
.
Handler
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -141,7 +136,7 @@ func RegisterHandler(h http.Handler, watchHandler WatchKeyHandler, pollingDurati
switch
result
{
// It means that we detected a change before starting watching on change,
// We proxy request to Rails, to see whether we
can receive the build
// We proxy request to Rails, to see whether we
have a build to receive
case
redis
.
WatchKeyStatusAlreadyChanged
:
registerHandlerAlreadyChangedHits
.
Inc
()
proxyRegisterRequest
(
h
,
w
,
newRequest
)
...
...
@@ -150,7 +145,7 @@ func RegisterHandler(h http.Handler, watchHandler WatchKeyHandler, pollingDurati
// We could potentially proxy request to Rails, but...
// We can end-up with unreliable responses,
// as don't really know whether ResponseWriter is still in a sane state,
//
whether
the connection is
not
dead
//
for example
the connection is dead
case
redis
.
WatchKeyStatusSeenChange
:
registerHandlerSeenChangeHits
.
Inc
()
w
.
WriteHeader
(
http
.
StatusNoContent
)
...
...
此差异已折叠。
点击以展开。
internal/builds/register_test.go
+
12
−
14
浏览文件 @
2ff811ed
...
...
@@ -20,8 +20,6 @@ func echoRequest(rw http.ResponseWriter, req *http.Request) {
var
echoRequestFunc
=
http
.
HandlerFunc
(
echoRequest
)
const
applicationJson
=
"application/json"
func
expectHandlerWithWatcher
(
t
*
testing
.
T
,
watchHandler
WatchKeyHandler
,
data
string
,
contentType
string
,
expectedHttpStatus
int
,
msgAndArgs
...
interface
{})
{
h
:=
RegisterHandler
(
echoRequestFunc
,
watchHandler
,
time
.
Second
)
...
...
@@ -40,7 +38,7 @@ func expectHandler(t *testing.T, data string, contentType string, expectedHttpSt
func
TestRegisterHandlerLargeBody
(
t
*
testing
.
T
)
{
data
:=
strings
.
Repeat
(
"."
,
maxRegisterBodySize
+
5
)
expectHandler
(
t
,
data
,
application
J
son
,
http
.
StatusRequestEntityTooLarge
,
expectHandler
(
t
,
data
,
"
application
/j
son
"
,
http
.
StatusRequestEntityTooLarge
,
"rejects body with entity too large"
)
}
...
...
@@ -50,20 +48,20 @@ func TestRegisterHandlerInvalidRunnerRequest(t *testing.T) {
}
func
TestRegisterHandlerInvalidJsonPayload
(
t
*
testing
.
T
)
{
expectHandler
(
t
,
"
{[
"
,
application
J
son
,
http
.
StatusOK
,
expectHandler
(
t
,
`
{[
`
,
"
application
/j
son
"
,
http
.
StatusOK
,
"fails on parsing body and proxies request to upstream"
)
}
func
TestRegisterHandlerMissingData
(
t
*
testing
.
T
)
{
dataList
:=
[]
string
{
"{
\
"
token
\
"
:
\
"
token
\
"
}
"
,
"{
\
"
last_update
\
"
:
\
"
data
\
"
}
"
}
dataList
:=
[]
string
{
`{
"token":"token"}
`
,
`{
"last_update":"data"}
`
}
for
_
,
data
:=
range
dataList
{
expectHandler
(
t
,
data
,
application
J
son
,
http
.
StatusOK
,
expectHandler
(
t
,
data
,
"
application
/j
son
"
,
http
.
StatusOK
,
"fails on argument validation and proxies request to upstream"
)
}
}
func
ex
cep
tWatcherToBeExecuted
(
t
*
testing
.
T
,
watchKeyStatus
redis
.
WatchKeyStatus
,
watchKeyError
error
,
func
ex
pec
tWatcherToBeExecuted
(
t
*
testing
.
T
,
watchKeyStatus
redis
.
WatchKeyStatus
,
watchKeyError
error
,
httpStatus
int
,
msgAndArgs
...
interface
{})
{
executed
:=
false
watchKeyHandler
:=
func
(
key
,
value
string
,
timeout
time
.
Duration
)
(
redis
.
WatchKeyStatus
,
error
)
{
...
...
@@ -71,33 +69,33 @@ func exceptWatcherToBeExecuted(t *testing.T, watchKeyStatus redis.WatchKeyStatus
return
watchKeyStatus
,
watchKeyError
}
parsableData
:=
"{
\
"
token
\
"
:
\
"
token
\
"
,
\
"
last_update
\
"
:
\
"
last_update
\
"
}
"
parsableData
:=
`{
"token":"token","last_update":"last_update"}
`
expectHandlerWithWatcher
(
t
,
watchKeyHandler
,
parsableData
,
application
J
son
,
httpStatus
,
msgAndArgs
...
)
expectHandlerWithWatcher
(
t
,
watchKeyHandler
,
parsableData
,
"
application
/j
son
"
,
httpStatus
,
msgAndArgs
...
)
assert
.
True
(
t
,
executed
,
msgAndArgs
...
)
}
func
TestRegisterHandlerWatcherError
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
errors
.
New
(
"redis connection"
),
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
errors
.
New
(
"redis connection"
),
http
.
StatusOK
,
"proxies data to upstream"
)
}
func
TestRegisterHandlerWatcherAlreadyChanged
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusAlreadyChanged
,
nil
,
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusAlreadyChanged
,
nil
,
http
.
StatusOK
,
"proxies data to upstream"
)
}
func
TestRegisterHandlerWatcherSeenChange
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusSeenChange
,
nil
,
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusSeenChange
,
nil
,
http
.
StatusNoContent
)
}
func
TestRegisterHandlerWatcherTimeout
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusTimeout
,
nil
,
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusTimeout
,
nil
,
http
.
StatusNoContent
)
}
func
TestRegisterHandlerWatcherNoChange
(
t
*
testing
.
T
)
{
ex
cep
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
nil
,
ex
pec
tWatcherToBeExecuted
(
t
,
redis
.
WatchKeyStatusNoChange
,
nil
,
http
.
StatusNoContent
)
}
此差异已折叠。
点击以展开。
internal/helper/helpers.go
+
1
−
0
浏览文件 @
2ff811ed
...
...
@@ -197,6 +197,7 @@ func ReadRequestBody(w http.ResponseWriter, r *http.Request, maxBodySize int64)
func
CloneRequestWithNewBody
(
r
*
http
.
Request
,
body
[]
byte
)
*
http
.
Request
{
newReq
:=
*
r
newReq
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
body
))
newReq
.
Header
=
HeaderClone
(
r
.
Header
)
newReq
.
ContentLength
=
int64
(
len
(
body
))
return
&
newReq
}
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录