Skip to content

Commit cb46c8b

Browse files
authored
Merge pull request #33 from eadwinCode/controller_throttle_doc
Doc update
2 parents ebf4b56 + b11bc39 commit cb46c8b

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

docs/settings.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ NINJA_EXTRA = {
99
'PAGINATION_CLASS':"ninja_extra.pagination.PageNumberPaginationExtra",
1010
'PAGINATION_PER_PAGE': 100,
1111
'INJECTOR_MODULES': [],
12+
'THROTTLE_CLASSES': [
13+
"ninja_extra.throttling.AnonRateThrottle",
14+
"ninja_extra.throttling.UserRateThrottle",
15+
],
16+
'THROTTLE_RATES': {
17+
'user': '1000/day',
18+
'anon': '100/day',
19+
},
20+
'NUM_PROXIES': None
1221
}
1322
```
1423
You can override what you don't need. It is not necessary need to override everything.
@@ -30,3 +39,18 @@ default: `100`
3039
It contains a list of strings that defines the path to injector `Module`.
3140
default: `[]`
3241

42+
`THROTTLE_CLASSES`
43+
=======================
44+
It contains a list of strings that defines the path default throttling classes.
45+
default: `[
46+
"ninja_extra.throttling.AnonRateThrottle",
47+
"ninja_extra.throttling.UserRateThrottle",
48+
]`
49+
50+
`THROTTLE_RATES`
51+
=======================
52+
It contains a key-value pair of different throttling rates which are applies to different `THROTTLING_CLASSES`.
53+
default: `{
54+
'user': '1000/day',
55+
'anon': '100/day',
56+
}`

docs/tutorial/throttling.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,43 @@ def get_user_by_id(request, id: int):
177177
return 'foo'
178178
```
179179
Here, we dynamically applied `sustained` rates and `burst` rates to `get_users` and `get_user_by_id` respectively
180+
181+
182+
!!! info "new in v0.15.8"
183+
You can throttle all controller endpoints actions at the controller class level
184+
185+
## **Controller Throttling**
186+
187+
```python
188+
# api.py
189+
from ninja_extra import (
190+
NinjaExtraAPI, throttle, api_controller, ControllerBase,
191+
http_get
192+
)
193+
from ninja_extra.throttling import DynamicRateThrottle
194+
api = NinjaExtraAPI()
195+
196+
@api_controller("/throttled-controller")
197+
class ThrottlingControllerSample(ControllerBase):
198+
throttling_classes = [
199+
DynamicRateThrottle,
200+
]
201+
throttling_init_kwargs = dict(scope="sustained")
202+
203+
@http_get("/endpoint_1")
204+
@throttle(DynamicRateThrottle, scope='burst')
205+
def endpoint_1(self, request):
206+
# this will override the generally throttling applied at the controller
207+
return "foo"
208+
209+
@http_get("/endpoint_2")
210+
def endpoint_2(self, request):
211+
return "foo"
212+
213+
@http_get("/endpoint_3")
214+
def endpoint_3(self, request):
215+
return "foo"
216+
217+
218+
api.register_controllers(ThrottlingControllerSample)
219+
```

0 commit comments

Comments
 (0)