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
0fdd003a
Commit
0fdd003a
authored
Jan 14, 2021
by
Quang-Minh Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert RateLimit-Reset format to Unix time
Issue
https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/795
parent
f2e25fe4
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
16 deletions
+19
-16
lib/gitlab/rack_attack.rb
lib/gitlab/rack_attack.rb
+5
-5
spec/lib/gitlab/rack_attack_spec.rb
spec/lib/gitlab/rack_attack_spec.rb
+10
-10
spec/support/helpers/rack_attack_spec_helpers.rb
spec/support/helpers/rack_attack_spec_helpers.rb
+4
-1
No files found.
lib/gitlab/rack_attack.rb
View file @
0fdd003a
...
...
@@ -49,9 +49,9 @@ module Gitlab
# reset. This is a standardized HTTP header:
# https://tools.ietf.org/html/rfc7231#page-69
#
# - RateLimit-Reset:
Similar to Retry-After.
# - RateLimit-Reset:
the point of time that the quest quota is reset, in Unix time
#
# - RateLimit-ResetTime: the point of time that the quest quota is reset
.
# - RateLimit-ResetTime: the point of time that the quest quota is reset
, in HTTP date format
def
self
.
throttled_response_headers
(
matched
,
match_data
)
# Match data example:
# {:discriminator=>"127.0.0.1", :count=>12, :period=>60 seconds, :limit=>1, :epoch_time=>1609833930}
...
...
@@ -62,14 +62,14 @@ module Gitlab
observed
=
match_data
[
:count
]
now
=
match_data
[
:epoch_time
]
retry_after
=
period
-
(
now
%
period
)
reset_time
=
now
+
(
period
-
now
%
period
)
reset_time
=
Time
.
at
(
now
+
(
period
-
now
%
period
))
# rubocop:disable Rails/TimeZone
{
'RateLimit-Name'
=>
matched
.
to_s
,
'RateLimit-Limit'
=>
rounded_limit
.
to_s
,
'RateLimit-Observed'
=>
observed
.
to_s
,
'RateLimit-Remaining'
=>
(
limit
>
observed
?
limit
-
observed
:
0
).
to_s
,
'RateLimit-Reset'
=>
re
try_after
.
to_s
,
'RateLimit-ResetTime'
=>
Time
.
at
(
reset_time
)
.
httpdate
,
'RateLimit-Reset'
=>
re
set_time
.
to_i
.
to_s
,
'RateLimit-ResetTime'
=>
reset_time
.
httpdate
,
'Retry-After'
=>
retry_after
.
to_s
}
end
...
...
spec/lib/gitlab/rack_attack_spec.rb
View file @
0fdd003a
...
...
@@ -116,7 +116,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'60'
,
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'1
830'
,
'RateLimit-Reset'
=>
'1
609844400'
,
# Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 11:00:00 GMT'
,
'Retry-After'
=>
'1830'
}
...
...
@@ -135,7 +135,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'60'
,
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'1
'
,
'RateLimit-Reset'
=>
'1
609844400'
,
# Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 11:00:00 GMT'
,
'Retry-After'
=>
'1'
}
...
...
@@ -154,7 +154,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'60'
,
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'
3600'
,
'RateLimit-Reset'
=>
'
1609844400'
,
# Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 11:00:00 GMT'
,
'Retry-After'
=>
'3600'
}
...
...
@@ -173,7 +173,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'60'
,
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'1
800'
,
'RateLimit-Reset'
=>
'1
609891200'
,
# Time.utc(2021, 1, 6, 0, 0, 0).to_i.to_s
'RateLimit-ResetTime'
=>
'Wed, 06 Jan 2021 00:00:00 GMT'
,
# Next day
'Retry-After'
=>
'1800'
}
...
...
@@ -192,7 +192,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'57'
,
# 56.66 requests per minute
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'1
800'
,
'RateLimit-Reset'
=>
'1
609844400'
,
# Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 11:00:00 GMT'
,
'Retry-After'
=>
'1800'
}
...
...
@@ -211,7 +211,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'62'
,
# 61.66 requests per minute
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'1
800'
,
'RateLimit-Reset'
=>
'1
609844400'
,
# Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 11:00:00 GMT'
,
'Retry-After'
=>
'1800'
}
...
...
@@ -230,7 +230,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'1'
,
# 0.9833 requests per minute
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'1
800'
,
'RateLimit-Reset'
=>
'1
609844400'
,
# Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 11:00:00 GMT'
,
'Retry-After'
=>
'1800'
}
...
...
@@ -249,7 +249,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'2'
,
# 1.016 requests per minute
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'1
800'
,
'RateLimit-Reset'
=>
'1
609844400'
,
# Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 11:00:00 GMT'
,
'Retry-After'
=>
'1800'
}
...
...
@@ -268,7 +268,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'40'
,
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'1
5'
,
'RateLimit-Reset'
=>
'1
609842615'
,
# Time.utc(2021, 1, 5, 10, 30, 15).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 10:30:15 GMT'
,
'Retry-After'
=>
'15'
}
...
...
@@ -287,7 +287,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do
'RateLimit-Limit'
=>
'23'
,
'RateLimit-Observed'
=>
'3700'
,
'RateLimit-Remaining'
=>
'0'
,
'RateLimit-Reset'
=>
'
27'
,
'RateLimit-Reset'
=>
'
1609842627'
,
# Time.utc(2021, 1, 5, 10, 30, 27).to_i.to_s
'RateLimit-ResetTime'
=>
'Tue, 05 Jan 2021 10:30:27 GMT'
,
'Retry-After'
=>
'27'
}
...
...
spec/support/helpers/rack_attack_spec_helpers.rb
View file @
0fdd003a
...
...
@@ -36,9 +36,12 @@ module RackAttackSpecHelpers
'RateLimit-Name'
=>
a_string_matching
(
/^throttle_.*$/
),
'RateLimit-Observed'
=>
a_string_matching
(
/^\d+$/
),
'RateLimit-Remaining'
=>
a_string_matching
(
/^\d+$/
),
'RateLimit-Reset'
=>
a_string_matching
(
/^\d+$/
),
'Retry-After'
=>
a_string_matching
(
/^\d+$/
)
)
expect
(
response
).
to
have_header
(
'RateLimit-Reset'
)
expect
do
DateTime
.
strptime
(
response
.
headers
[
'RateLimit-Reset'
],
'%s'
)
end
.
not_to
raise_error
expect
(
response
).
to
have_header
(
'RateLimit-ResetTime'
)
expect
do
Time
.
httpdate
(
response
.
headers
[
'RateLimit-ResetTime'
])
...
...
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