Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
886f8b47
Commit
886f8b47
authored
Sep 14, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweak throttles and improve docs
parent
30050798
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
6 deletions
+30
-6
djangorestframework/tests/throttling.py
+2
-0
djangorestframework/throttling.py
+5
-6
docs/api-guide/throttling.md
+23
-0
No files found.
djangorestframework/tests/throttling.py
View file @
886f8b47
...
@@ -14,10 +14,12 @@ from djangorestframework.response import Response
...
@@ -14,10 +14,12 @@ from djangorestframework.response import Response
class
User3SecRateThrottle
(
UserRateThrottle
):
class
User3SecRateThrottle
(
UserRateThrottle
):
rate
=
'3/sec'
rate
=
'3/sec'
scope
=
'seconds'
class
User3MinRateThrottle
(
UserRateThrottle
):
class
User3MinRateThrottle
(
UserRateThrottle
):
rate
=
'3/min'
rate
=
'3/min'
scope
=
'minutes'
class
MockView
(
APIView
):
class
MockView
(
APIView
):
...
...
djangorestframework/throttling.py
View file @
886f8b47
...
@@ -44,7 +44,7 @@ class SimpleRateThottle(BaseThrottle):
...
@@ -44,7 +44,7 @@ class SimpleRateThottle(BaseThrottle):
timer
=
time
.
time
timer
=
time
.
time
settings
=
api_settings
settings
=
api_settings
cache_format
=
'
%(class)
s
_
%(scope)
s_
%(ident)
s'
cache_format
=
'
throtte
_
%(scope)
s_
%(ident)
s'
scope
=
None
scope
=
None
def
__init__
(
self
,
view
):
def
__init__
(
self
,
view
):
...
@@ -144,7 +144,6 @@ class AnonRateThrottle(SimpleRateThottle):
...
@@ -144,7 +144,6 @@ class AnonRateThrottle(SimpleRateThottle):
ident
=
request
.
META
.
get
(
'REMOTE_ADDR'
,
None
)
ident
=
request
.
META
.
get
(
'REMOTE_ADDR'
,
None
)
return
self
.
cache_format
%
{
return
self
.
cache_format
%
{
'class'
:
self
.
__class__
.
__name__
,
'scope'
:
self
.
scope
,
'scope'
:
self
.
scope
,
'ident'
:
ident
'ident'
:
ident
}
}
...
@@ -167,7 +166,6 @@ class UserRateThrottle(SimpleRateThottle):
...
@@ -167,7 +166,6 @@ class UserRateThrottle(SimpleRateThottle):
ident
=
request
.
META
.
get
(
'REMOTE_ADDR'
,
None
)
ident
=
request
.
META
.
get
(
'REMOTE_ADDR'
,
None
)
return
self
.
cache_format
%
{
return
self
.
cache_format
%
{
'class'
:
self
.
__class__
.
__name__
,
'scope'
:
self
.
scope
,
'scope'
:
self
.
scope
,
'ident'
:
ident
'ident'
:
ident
}
}
...
@@ -181,11 +179,13 @@ class ScopedRateThrottle(SimpleRateThottle):
...
@@ -181,11 +179,13 @@ class ScopedRateThrottle(SimpleRateThottle):
user id of the request, and the scope of the view being accessed.
user id of the request, and the scope of the view being accessed.
"""
"""
scope_attr
=
'throttle_scope'
def
__init__
(
self
,
view
):
def
__init__
(
self
,
view
):
"""
"""
Scope is determined from the view being accessed.
Scope is determined from the view being accessed.
"""
"""
self
.
scope
=
getattr
(
self
.
view
,
'throttle_scope'
,
None
)
self
.
scope
=
getattr
(
self
.
view
,
self
.
scope_attr
,
None
)
super
(
ScopedRateThrottle
,
self
)
.
__init__
(
view
)
super
(
ScopedRateThrottle
,
self
)
.
__init__
(
view
)
def
parse_rate_description
(
self
,
rate
):
def
parse_rate_description
(
self
,
rate
):
...
@@ -204,7 +204,7 @@ class ScopedRateThrottle(SimpleRateThottle):
...
@@ -204,7 +204,7 @@ class ScopedRateThrottle(SimpleRateThottle):
with the '.throttle_scope` property of the view.
with the '.throttle_scope` property of the view.
"""
"""
if
not
self
.
scope
:
if
not
self
.
scope
:
return
None
# Only throttle views
with `.throttle_scope`
set.
return
None
# Only throttle views
if `.throttle_scope` is
set.
if
request
.
user
.
is_authenticated
():
if
request
.
user
.
is_authenticated
():
ident
=
request
.
user
.
id
ident
=
request
.
user
.
id
...
@@ -212,7 +212,6 @@ class ScopedRateThrottle(SimpleRateThottle):
...
@@ -212,7 +212,6 @@ class ScopedRateThrottle(SimpleRateThottle):
ident
=
request
.
META
.
get
(
'REMOTE_ADDR'
,
None
)
ident
=
request
.
META
.
get
(
'REMOTE_ADDR'
,
None
)
return
self
.
cache_format
%
{
return
self
.
cache_format
%
{
'class'
:
self
.
__class__
.
__name__
,
'scope'
:
self
.
scope
,
'scope'
:
self
.
scope
,
'ident'
:
ident
'ident'
:
ident
}
}
docs/api-guide/throttling.md
View file @
886f8b47
...
@@ -83,6 +83,29 @@ The allowed request rate is determined from one of the following (in order of pr
...
@@ -83,6 +83,29 @@ The allowed request rate is determined from one of the following (in order of pr
*
The
`rate`
property on the class, which may be provided by overriding
`UserThrottle`
and setting the property.
*
The
`rate`
property on the class, which may be provided by overriding
`UserThrottle`
and setting the property.
*
The
`DEFAULT_THROTTLE_RATES['user']`
setting.
*
The
`DEFAULT_THROTTLE_RATES['user']`
setting.
An API may have multiple
`UserRateThrottles`
in place at the same time. To do so, override
`UserRateThrottle`
and set a unique "scope" for each class.
For example, multiple user throttle rates could be implemented by using the following classes...
class BurstRateThrottle(UserRateThrottle):
scope = 'burst'
class SustainedRateThrottle(UserRateThrottle):
scope = 'sustained'
...and the following settings.
API_SETTINGS = {
'DEFAULT_THROTTLES': (
'example.throttles.BurstRateThrottle',
'example.throttles.SustainedRateThrottle',
)
'DEFAULT_THROTTLE_RATES': {
'burst': '60/min',
'sustained': '1000/day'
}
}
`UserThrottle`
is suitable if you want a simple global rate restriction per-user.
`UserThrottle`
is suitable if you want a simple global rate restriction per-user.
## ScopedRateThrottle
## ScopedRateThrottle
...
...
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