Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
funkwhale
Manage
Activity
Members
Labels
Plan
Issues
502
Issue boards
Milestones
Wiki
Code
Merge requests
19
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
funkwhale
funkwhale
Commits
f7e463b5
Verified
Commit
f7e463b5
authored
5 years ago
by
Eliot Berriot
Browse files
Options
Downloads
Patches
Plain Diff
See
#261
: More verbose X-RateLimit headers
parent
e274eab2
No related branches found
No related tags found
1 merge request
!877
: Rate limiting
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/funkwhale_api/common/middleware.py
+8
-1
8 additions, 1 deletion
api/funkwhale_api/common/middleware.py
api/tests/common/test_middleware.py
+5
-2
5 additions, 2 deletions
api/tests/common/test_middleware.py
with
13 additions
and
3 deletions
api/funkwhale_api/common/middleware.py
+
8
−
1
View file @
f7e463b5
...
...
@@ -230,9 +230,16 @@ class ThrottleStatusMiddleware:
)
response
[
"
X-RateLimit-Duration
"
]
=
str
(
throttle_status
[
"
duration
"
])
if
throttle_status
[
"
history
"
]:
latest_request
=
throttle_status
[
"
history
"
][
0
]
now
=
int
(
time
.
time
())
# At this point, the client can send additional requests
oldtest_request
=
throttle_status
[
"
history
"
][
-
1
]
remaining
=
throttle_status
[
"
duration
"
]
-
(
now
-
int
(
oldtest_request
))
response
[
"
X-RateLimit-Available
"
]
=
str
(
now
+
remaining
)
response
[
"
X-RateLimit-AvailableSeconds
"
]
=
str
(
remaining
)
# At this point, all Rate Limit is reset to 0
latest_request
=
throttle_status
[
"
history
"
][
0
]
remaining
=
throttle_status
[
"
duration
"
]
-
(
now
-
int
(
latest_request
))
response
[
"
X-RateLimit-Reset
"
]
=
str
(
now
+
remaining
)
response
[
"
X-RateLimit-ResetSeconds
"
]
=
str
(
remaining
)
return
response
This diff is collapsed.
Click to expand it.
api/tests/common/test_middleware.py
+
5
−
2
View file @
f7e463b5
...
...
@@ -202,7 +202,7 @@ def test_throttle_status_middleware_includes_info_in_response_headers(mocker):
"
num_requests
"
:
42
,
"
duration
"
:
3600
,
"
scope
"
:
"
hello
"
,
"
history
"
:
[
time
.
time
()
-
1600
],
"
history
"
:
[
time
.
time
()
-
1600
,
time
.
time
()
-
1800
],
}
),
)
...
...
@@ -210,10 +210,13 @@ def test_throttle_status_middleware_includes_info_in_response_headers(mocker):
assert
m
(
request
)
==
response
assert
response
[
"
X-RateLimit-Limit
"
]
==
"
42
"
assert
response
[
"
X-RateLimit-Remaining
"
]
==
"
4
1
"
assert
response
[
"
X-RateLimit-Remaining
"
]
==
"
4
0
"
assert
response
[
"
X-RateLimit-Duration
"
]
==
"
3600
"
assert
response
[
"
X-RateLimit-Scope
"
]
==
"
hello
"
assert
response
[
"
X-RateLimit-Reset
"
]
==
str
(
int
(
time
.
time
())
+
2000
)
assert
response
[
"
X-RateLimit-ResetSeconds
"
]
==
str
(
2000
)
assert
response
[
"
X-RateLimit-Available
"
]
==
str
(
int
(
time
.
time
())
+
1800
)
assert
response
[
"
X-RateLimit-AvailableSeconds
"
]
==
str
(
1800
)
def
test_throttle_status_middleware_returns_proper_response
(
mocker
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment