|
| 1 | +# **Throttling** |
| 2 | + |
| 3 | +Throttling can be seen as a permission that determines if a request should be authorized. |
| 4 | +It indicates a temporary state used to control the rate of requests that clients can make to an API. |
| 5 | + |
| 6 | +```python |
| 7 | +from ninja_extra import NinjaExtraAPI, throttle |
| 8 | +api = NinjaExtraAPI() |
| 9 | + |
| 10 | +@api.get('/users') |
| 11 | +@throttle # this will apply default throttle classes [UserRateThrottle, AnonRateThrottle] |
| 12 | +def my_throttled_endpoint(request): |
| 13 | + return 'foo' |
| 14 | +``` |
| 15 | + |
| 16 | +!!! info |
| 17 | + The above example won't be throttled because the default scope for `UserRateThrottle` and `AnonRateThrottle` |
| 18 | + is `none` |
| 19 | + |
| 20 | +## **Multiple Throttling** |
| 21 | +Django-ninja-extra throttle supposes multiple throttles which is useful to impose different |
| 22 | +constraints, which could be burst throttling rate or sustained throttling rates, on an API. |
| 23 | +for example, you might want to limit a user to a maximum of 60 requests per minute, and 1000 requests per day. |
| 24 | + |
| 25 | +```python |
| 26 | +from ninja_extra import NinjaExtraAPI, throttle |
| 27 | +from ninja_extra.throttling import UserRateThrottle |
| 28 | +api = NinjaExtraAPI() |
| 29 | + |
| 30 | +class User60MinRateThrottle(UserRateThrottle): |
| 31 | + rate = "60/min" |
| 32 | + scope = "minutes" |
| 33 | + |
| 34 | + |
| 35 | +class User1000PerDayRateThrottle(UserRateThrottle): |
| 36 | + rate = "1000/day" |
| 37 | + scope = "days" |
| 38 | + |
| 39 | +@api.get('/users') |
| 40 | +@throttle(User60MinRateThrottle, User1000PerDayRateThrottle) |
| 41 | +def my_throttled_endpoint(request): |
| 42 | + return 'foo' |
| 43 | + |
| 44 | +``` |
| 45 | +## **Throttling Policy Settings** |
| 46 | +You can set globally default throttling classes and rates in your project `settings.py` by overriding the keys below: |
| 47 | +```python |
| 48 | +# django settings.py |
| 49 | +NINJA_EXTRA = { |
| 50 | + 'THROTTLE_CLASSES': [ |
| 51 | + "ninja_extra.throttling.AnonRateThrottle", |
| 52 | + "ninja_extra.throttling.UserRateThrottle", |
| 53 | + ], |
| 54 | + 'THROTTLE_RATES': { |
| 55 | + 'user': '1000/day', |
| 56 | + 'anon': '100/day', |
| 57 | + }, |
| 58 | + 'NUM_PROXIES': None |
| 59 | +} |
| 60 | +``` |
| 61 | +The rate descriptions used in `THROTTLE_RATES` may include `second`, `minute`, `hour` or `day` as the throttle period. |
| 62 | + |
| 63 | +```python |
| 64 | +from ninja_extra import NinjaExtraAPI, throttle |
| 65 | +from ninja_extra.throttling import UserRateThrottle |
| 66 | + |
| 67 | +api = NinjaExtraAPI() |
| 68 | + |
| 69 | +@api.get('/users') |
| 70 | +@throttle(UserRateThrottle) |
| 71 | +def my_throttled_endpoint(request): |
| 72 | + return 'foo' |
| 73 | +``` |
| 74 | + |
| 75 | +## **Clients Identification** |
| 76 | +Clients are identified by x-Forwarded-For in HTTP header and REMOTE_ADDR from WSGI variable. |
| 77 | +These are unique identities which identifies clients IP addresses used for throttling. |
| 78 | +`X-Forwarded-For` is preferable over `REMOTE_ADDR` and is used as so. |
| 79 | + |
| 80 | +#### **Limit Clients Proxies** |
| 81 | +If you need to strictly identify unique client IP addresses, you'll need to first configure the number of application proxies that the API runs behind by setting the `NUM_PROXIES` setting. This setting should be an integer of zero or more. |
| 82 | +If set to non-zero then the client IP will be identified as being the last IP address in the X-Forwarded-For header, once any application proxy IP addresses have first been excluded. If set to zero, then the REMOTE_ADDR value will always be used as the identifying IP address. |
| 83 | +It is important to understand that if you configure the `NUM_PROXIES` setting, then all clients behind a unique [NAT'd](https://en.wikipedia.org/wiki/Network_address_translation) gateway will be treated as a single client. |
| 84 | + |
| 85 | +!!! info |
| 86 | + Further context on how the X-Forwarded-For header works, and identifying a remote client IP can be found here. |
| 87 | + |
| 88 | +## **Throttling Model Cache setup** |
| 89 | +The throttling models used in django-ninja-extra utilizes Django cache backend. It uses the `default` value of [`LocMemCache`]() |
| 90 | +See Django's [cache documentation](https://docs.djangoproject.com/en/stable/topics/cache/#setting-up-the-cache) for more details. |
| 91 | + |
| 92 | +If you dont want to use the default cache defined in throttle model, here is an example on how to define a different cache for a throttling model |
| 93 | +```python |
| 94 | + |
| 95 | +from django.core.cache import caches |
| 96 | +from ninja_extra.throttling import AnonRateThrottle |
| 97 | + |
| 98 | + |
| 99 | +class CustomAnonRateThrottle(AnonRateThrottle): |
| 100 | + cache = caches['alternate'] |
| 101 | +``` |
| 102 | +# **API Reference** |
| 103 | + |
| 104 | +## **AnonRateThrottle** |
| 105 | +`AnonRateThrottle` model is for throttling unauthenticated users using their IP address as key to throttle against. |
| 106 | +It is suitable for restricting rate of requests from an unknown source |
| 107 | + |
| 108 | +Request Permission is determined by: |
| 109 | +- `rate` defined in derived class |
| 110 | +- `anon` scope defined in `THROTTLE_RATES` in `NINJA_EXTRA` settings in `settings.py` |
| 111 | + |
| 112 | +## **UserRateThrottle** |
| 113 | +`UserRateThrottle` model is for throttling authenticated users using user id or pk to generate a key to throttle against. |
| 114 | +Unauthenticated requests will fall back to using the IP address of the incoming request to generate a unique key to throttle against. |
| 115 | + |
| 116 | +Request Permission is determined by: |
| 117 | +- `rate` defined in derived class |
| 118 | +- `user` scope defined in `THROTTLE_RATES` in `NINJA_EXTRA` settings in `settings.py` |
| 119 | + |
| 120 | +You can use multiple user throttle rates for a `UserRateThrottle` model, for example: |
| 121 | +```python |
| 122 | +# example/throttles.py |
| 123 | +from ninja_extra.throttling import UserRateThrottle |
| 124 | + |
| 125 | + |
| 126 | +class BurstRateThrottle(UserRateThrottle): |
| 127 | + scope = 'burst' |
| 128 | + |
| 129 | + |
| 130 | +class SustainedRateThrottle(UserRateThrottle): |
| 131 | + scope = 'sustained' |
| 132 | +``` |
| 133 | + |
| 134 | +```python |
| 135 | +# django settings.py |
| 136 | +NINJA_EXTRA = { |
| 137 | + 'THROTTLE_CLASSES': [ |
| 138 | + 'example.throttles.BurstRateThrottle', |
| 139 | + 'example.throttles.SustainedRateThrottle' |
| 140 | + ], |
| 141 | + 'THROTTLE_RATES': { |
| 142 | + 'burst': '60/min', |
| 143 | + 'sustained': '1000/day' |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | +## **DynamicRateThrottle** |
| 148 | +`DynamicRateThrottle` model is for throttling authenticated and unauthenticated users in similar way as `UserRateThrottle`. |
| 149 | +Its key feature is in the ability to dynamically set `scope` where its used. |
| 150 | +for an example: |
| 151 | +we can defined a scope in settings |
| 152 | + |
| 153 | +```python |
| 154 | +# django settings.py |
| 155 | +NINJA_EXTRA = { |
| 156 | + 'THROTTLE_RATES': { |
| 157 | + 'burst': '60/min', |
| 158 | + 'sustained': '1000/day' |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +```python |
| 164 | +# api.py |
| 165 | +from ninja_extra import NinjaExtraAPI, throttle |
| 166 | +from ninja_extra.throttling import DynamicRateThrottle |
| 167 | +api = NinjaExtraAPI() |
| 168 | + |
| 169 | +@api.get('/users') |
| 170 | +@throttle(DynamicRateThrottle, scope='burst') |
| 171 | +def get_users(request): |
| 172 | + return 'foo' |
| 173 | + |
| 174 | +@api.get('/users/<int:id>') |
| 175 | +@throttle(DynamicRateThrottle, scope='sustained') |
| 176 | +def get_user_by_id(request, id: int): |
| 177 | + return 'foo' |
| 178 | +``` |
| 179 | +Here, we dynamically applied `sustained` rates and `burst` rates to `get_users` and `get_user_by_id` respectively |
0 commit comments