Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
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-ce
Commits
54f13b1e
Commit
54f13b1e
authored
Dec 09, 2017
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add rate limiting to guard against excessive scheduling of pipelines
parent
f8c3a58a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
1 deletion
+43
-1
app/controllers/projects/pipeline_schedules_controller.rb
app/controllers/projects/pipeline_schedules_controller.rb
+11
-0
lib/gitlab/action_rate_limiter.rb
lib/gitlab/action_rate_limiter.rb
+31
-0
spec/controllers/projects/pipeline_schedules_controller_spec.rb
...ontrollers/projects/pipeline_schedules_controller_spec.rb
+1
-1
No files found.
app/controllers/projects/pipeline_schedules_controller.rb
View file @
54f13b1e
...
@@ -42,6 +42,13 @@ class Projects::PipelineSchedulesController < Projects::ApplicationController
...
@@ -42,6 +42,13 @@ class Projects::PipelineSchedulesController < Projects::ApplicationController
end
end
def
play
def
play
limiter
=
::
Gitlab
::
ActionRateLimiter
.
new
(
action:
'play_pipeline_schedule'
)
if
limiter
.
throttled?
(
throttle_key
,
1
)
flash
[
:notice
]
=
'You cannot play this scheduled pipeline at the moment. Please wait a minute.'
return
redirect_to
pipeline_schedules_path
(
@project
)
end
job_id
=
RunPipelineScheduleWorker
.
perform_async
(
schedule
.
id
,
current_user
.
id
)
job_id
=
RunPipelineScheduleWorker
.
perform_async
(
schedule
.
id
,
current_user
.
id
)
flash
[
:notice
]
=
flash
[
:notice
]
=
...
@@ -74,6 +81,10 @@ class Projects::PipelineSchedulesController < Projects::ApplicationController
...
@@ -74,6 +81,10 @@ class Projects::PipelineSchedulesController < Projects::ApplicationController
private
private
def
throttle_key
"user:
#{
current_user
.
id
}
:schedule:
#{
schedule
.
id
}
"
end
def
schedule
def
schedule
@schedule
||=
project
.
pipeline_schedules
.
find
(
params
[
:id
])
@schedule
||=
project
.
pipeline_schedules
.
find
(
params
[
:id
])
end
end
...
...
lib/gitlab/action_rate_limiter.rb
0 → 100644
View file @
54f13b1e
module
Gitlab
# This class implements a simple rate limiter that can be used to throttle
# certain actions. Unlike Rack Attack and Rack::Throttle, which operate at
# the middleware level, this can be used at the controller level.
class
ActionRateLimiter
TIME_TO_EXPIRE
=
60
# 1 min
attr_accessor
:action
,
:expiry_time
def
initialize
(
action
:,
expiry_time:
TIME_TO_EXPIRE
)
@action
=
action
@expiry_time
=
expiry_time
end
def
increment
(
key
)
value
=
0
Gitlab
::
Redis
::
Cache
.
with
do
|
redis
|
cache_key
=
"action_rate_limiter:
#{
action
}
:
#{
key
}
"
value
=
redis
.
incr
(
cache_key
)
redis
.
expire
(
cache_key
,
expiry_time
)
if
value
==
1
end
value
.
to_i
end
def
throttled?
(
key
,
threshold_value
)
self
.
increment
(
key
)
>
threshold_value
end
end
end
spec/controllers/projects/pipeline_schedules_controller_spec.rb
View file @
54f13b1e
...
@@ -366,7 +366,7 @@ describe Projects::PipelineSchedulesController do
...
@@ -366,7 +366,7 @@ describe Projects::PipelineSchedulesController do
end
end
end
end
describe
'POST #play'
do
describe
'POST #play'
,
:clean_gitlab_redis_cache
do
set
(
:user
)
{
create
(
:user
)
}
set
(
:user
)
{
create
(
:user
)
}
let
(
:ref
)
{
'master'
}
let
(
:ref
)
{
'master'
}
...
...
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