@@ -12,9 +12,11 @@ Throttling is similar to [permissions], in that it determines if a request shoul
...
@@ -12,9 +12,11 @@ Throttling is similar to [permissions], in that it determines if a request shoul
As with permissions, multiple throttles may be used. Your API might have a restrictive throttle for unauthenticated requests, and a less restrictive throttle for authenticated requests.
As with permissions, multiple throttles may be used. Your API might have a restrictive throttle for unauthenticated requests, and a less restrictive throttle for authenticated requests.
Another scenario where you might want to use multiple throttles would be if you need to impose different constraints on different parts of the API, due ato some services being particularly resource-intensive.
Another scenario where you might want to use multiple throttles would be if you need to impose different constraints on different parts of the API, due to some services being particularly resource-intensive.
Throttles do not necessarily only refer to rate-limiting requests. For example a storage service might also need to throttle against bandwidth.
Multiple throttles can also be used if you want to impose both burst throttling rates, and sustained throttling rates. For example, you might want to limit a user to a maximum of 60 requests per minute, and 1000 requests per day.
Throttles do not necessarily only refer to rate-limiting requests. For example a storage service might also need to throttle against bandwidth, and a paid data service might want to throttle against a certain number of a records being accessed.
## How throttling is determined
## How throttling is determined
...
@@ -25,7 +27,7 @@ If any throttle check fails an `exceptions.Throttled` exception will be raised,
...
@@ -25,7 +27,7 @@ If any throttle check fails an `exceptions.Throttled` exception will be raised,
## Setting the throttling policy
## Setting the throttling policy
The default throttling policy may be set globally, using the `DEFAULT_THROTTLES`setting. For example.
The default throttling policy may be set globally, using the `DEFAULT_THROTTLES`and `DEFAULT_THROTTLE_RATES` settings. For example.
API_SETTINGS = {
API_SETTINGS = {
'DEFAULT_THROTTLES': (
'DEFAULT_THROTTLES': (
...
@@ -38,6 +40,8 @@ The default throttling policy may be set globally, using the `DEFAULT_THROTTLES`
...
@@ -38,6 +40,8 @@ The default throttling policy may be set globally, using the `DEFAULT_THROTTLES`
}
}
}
}
The rate descriptions used in `DEFAULT_THROTTLE_RATES` may include `second`, `minute`, `hour` or `day` as the throttle period.
You can also set the throttling policy on a per-view basis, using the `APIView` class based views.
You can also set the throttling policy on a per-view basis, using the `APIView` class based views.
class ExampleView(APIView):
class ExampleView(APIView):
...
@@ -59,18 +63,67 @@ Or, if you're using the `@api_view` decorator with function based views.
...
@@ -59,18 +63,67 @@ Or, if you're using the `@api_view` decorator with function based views.
}
}
return Response(content)
return Response(content)
## AnonThrottle
## AnonRateThrottle
The `AnonThrottle` will only ever throttle unauthenticated users. The IP address of the incoming request is used to generate a unique key to throttle against.
The allowed request rate is determined from one of the following (in order of preference).
The `AnonThrottle` will only ever throttle unauthenticated users. The IP address of the incoming request is used to identify
* The `rate` property on the class, which may be provided by overriding `AnonThrottle` and setting the property.
* The `DEFAULT_THROTTLE_RATES['anon']` setting.
`AnonThrottle` is suitable if you want to restrict the rate of requests from unknown sources.
`AnonThrottle` is suitable if you want to restrict the rate of requests from unknown sources.
## UserThrottle
## UserRateThrottle
The `UserThrottle` will throttle users to a given rate of requests across the API. The user id is used to generate a unique key to throttle against. Unauthenticted requests will fall back to using the IP address of the incoming request is used to generate a unique key to throttle against.
The allowed request rate is determined from one of the following (in order of preference).
* The `rate` property on the class, which may be provided by overriding `UserThrottle` and setting the property.
* The `DEFAULT_THROTTLE_RATES['user']` setting.
`UserThrottle` is suitable if you want a simple global rate restriction per-user.
## ScopedRateThrottle
The `ScopedThrottle` class can be used to restrict access to specific parts of the API. This throttle will only be applied if the view that is being accessed includes a `.throttle_scope` property. The unique throttle key will then be formed by concatenating the "scope" of the request with the unqiue user id or IP address.
The allowed request rate is determined by the `DEFAULT_THROTTLE_RATES` setting using a key from the request "scope".
For example, given the following views...
class ContactListView(APIView):
throttle_scope = 'contacts'
...
`UserThrottle` is suitable if you want a simple restriction
User requests to either `ContactListView` or `ContactDetailView` would be restricted to a total of 1000 requests per-day. User requests to `UploadView` would be restricted to 20 requests per day.
## Custom throttles
## Custom throttles
To implement a custom throttle, override `BaseThrottle` and implement `.allow_request(request)`. The method should return `True` if the request should be allowed, and `False` otherwise.
Optionally you may also override the `.wait()` method. If implemented, `.wait()` should return a recomended number of seconds to wait before attempting the next request, or `None`. The `.wait()` method will only be called if `.check_throttle()` has previously returned `False`.