Skip to content

Commit

Permalink
feat: default throttles
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 17, 2024
1 parent b42710e commit e69a195
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
13 changes: 13 additions & 0 deletions codeforlife/settings/third_party.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
"django_filters.rest_framework.DjangoFilterBackend"
],
"DEFAULT_PAGINATION_CLASS": "codeforlife.pagination.LimitOffsetPagination",
"DEFAULT_THROTTLE_CLASSES": [
"codeforlife.user.throttles.AnonBurstRateThrottle",
"codeforlife.user.throttles.BurstRateThrottle",
"codeforlife.user.throttles.AnonSustainedRateThrottle",
"codeforlife.user.throttles.SustainedRateThrottle",
"rest_framework.throttling.ScopedRateThrottle",
],
"DEFAULT_THROTTLE_RATES": {
"anon_burst": "60/min",
"user_burst": "120/min",
"anon_sustained": "10000/day",
"user_sustained": "50000/day",
},
}

# Django Extensions - Graph Models
Expand Down
7 changes: 7 additions & 0 deletions codeforlife/user/throttles/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
© Ocado Group
Created on 17/01/2024 at 09:42:34(+00:00).
"""

from .burst_rate import AnonBurstRateThrottle, BurstRateThrottle
from .sustained_rate import AnonSustainedRateThrottle, SustainedRateThrottle
18 changes: 18 additions & 0 deletions codeforlife/user/throttles/burst_rate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
© Ocado Group
Created on 17/01/2024 at 09:36:24(+00:00).
"""

from rest_framework.throttling import AnonRateThrottle, UserRateThrottle


class BurstRateThrottle(UserRateThrottle):
"""Throttles requests sent in bursts from authenticated users."""

scope = "user_burst"


class AnonBurstRateThrottle(AnonRateThrottle):
"""Throttles requests sent in bursts from anonymous users."""

scope = "anon_burst"
24 changes: 24 additions & 0 deletions codeforlife/user/throttles/sustained_rate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
© Ocado Group
Created on 17/01/2024 at 09:37:14(+00:00).
"""

from rest_framework.throttling import AnonRateThrottle, UserRateThrottle


class SustainedRateThrottle(UserRateThrottle):
"""
Throttles requests sent over a sustained period of time from authenticated
users.
"""

scope = "user_sustained"


class AnonSustainedRateThrottle(AnonRateThrottle):
"""
Throttles requests sent over a sustained period of time from anonymous
users.
"""

scope = "anon_sustained"

0 comments on commit e69a195

Please sign in to comment.