mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-12-04 07:24:03 +03:00
Improve throttling docs
This commit is contained in:
parent
41633b9cfb
commit
24d0f24527
|
@ -170,7 +170,7 @@ def example_view(request, format=None):
|
|||
<p>To use custom model permissions, override <code>DjangoModelPermissions</code> and set the <code>.perms_map</code> property. Refer to the source code for details.</p>
|
||||
<p>The <code>DjangoModelPermissions</code> class also supports object-level permissions. Third-party authorization backends such as <a href="https://github.com/lukaszb/django-guardian">django-guardian</a> that provide object-level permissions should work just fine with <code>DjangoModelPermissions</code> without any custom configuration required.</p>
|
||||
<h2 id="custom-permissions">Custom permissions</h2>
|
||||
<p>To implement a custom permission, override <code>BasePermission</code> and implement the <code>.check_permission(self, request, obj=None)</code> method.</p>
|
||||
<p>To implement a custom permission, override <code>BasePermission</code> and implement the <code>.has_permission(self, request, obj=None)</code> method.</p>
|
||||
<p>The method should return <code>True</code> if the request should be granted access, and <code>False</code> otherwise.</p>
|
||||
</div><!--/span-->
|
||||
</div><!--/row-->
|
||||
|
|
|
@ -95,9 +95,9 @@
|
|||
<li class="main"><a href="#throttling">Throttling</a></li>
|
||||
<li><a href="#how-throttling-is-determined">How throttling is determined</a></li>
|
||||
<li><a href="#setting-the-throttling-policy">Setting the throttling policy</a></li>
|
||||
<li><a href="#anonthrottle">AnonThrottle</a></li>
|
||||
<li><a href="#userthrottle">UserThrottle</a></li>
|
||||
<li><a href="#scopedthrottle">ScopedThrottle</a></li>
|
||||
<li><a href="#anonratethrottle">AnonRateThrottle</a></li>
|
||||
<li><a href="#userratethrottle">UserRateThrottle</a></li>
|
||||
<li><a href="#scopedratethrottle">ScopedRateThrottle</a></li>
|
||||
<li><a href="#custom-throttles">Custom throttles</a></li>
|
||||
|
||||
</ul>
|
||||
|
@ -113,14 +113,15 @@
|
|||
</blockquote>
|
||||
<p>Throttling is similar to <a href="permissions">permissions</a>, in that it determines if a request should be authorized. Throttles indicate a temporary state, and are used to control the rate of requests that clients can make to an API.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>Throttles do not necessarily only refer to rate-limiting requests. For example a storage service might also need to throttle against bandwidth.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<h2 id="how-throttling-is-determined">How throttling is determined</h2>
|
||||
<p>As with permissions and authentication, throttling in REST framework is always defined as a list of classes.</p>
|
||||
<p>Before running the main body of the view each throttle in the list is checked.
|
||||
If any throttle check fails an <code>exceptions.Throttled</code> exception will be raised, and the main body of the view will not run.</p>
|
||||
<h2 id="setting-the-throttling-policy">Setting the throttling policy</h2>
|
||||
<p>The default throttling policy may be set globally, using the <code>DEFAULT_THROTTLES</code> setting. For example.</p>
|
||||
<p>The default throttling policy may be set globally, using the <code>DEFAULT_THROTTLES</code> and <code>DEFAULT_THROTTLE_RATES</code> settings. For example.</p>
|
||||
<pre class="prettyprint lang-py"><code>API_SETTINGS = {
|
||||
'DEFAULT_THROTTLES': (
|
||||
'djangorestframework.throttles.AnonThrottle',
|
||||
|
@ -132,6 +133,7 @@ If any throttle check fails an <code>exceptions.Throttled</code> exception will
|
|||
}
|
||||
}
|
||||
</code></pre>
|
||||
<p>The rate descriptions used in <code>DEFAULT_THROTTLE_RATES</code> may include <code>second</code>, <code>minute</code>, <code>hour</code> or <code>day</code> as the throttle period.</p>
|
||||
<p>You can also set the throttling policy on a per-view basis, using the <code>APIView</code> class based views.</p>
|
||||
<pre class="prettyprint lang-py"><code>class ExampleView(APIView):
|
||||
throttle_classes = (UserThrottle,)
|
||||
|
@ -151,13 +153,53 @@ def example_view(request, format=None):
|
|||
}
|
||||
return Response(content)
|
||||
</code></pre>
|
||||
<h2 id="anonthrottle">AnonThrottle</h2>
|
||||
<p>The <code>AnonThrottle</code> will only ever throttle unauthenticated users. The IP address of the incoming request is used to identify </p>
|
||||
<h2 id="anonratethrottle">AnonRateThrottle</h2>
|
||||
<p>The <code>AnonThrottle</code> will only ever throttle unauthenticated users. The IP address of the incoming request is used to generate a unique key to throttle against.</p>
|
||||
<p>The allowed request rate is determined from one of the following (in order of preference).</p>
|
||||
<ul>
|
||||
<li>The <code>rate</code> property on the class, which may be provided by overriding <code>AnonThrottle</code> and setting the property.</li>
|
||||
<li>The <code>DEFAULT_THROTTLE_RATES['anon']</code> setting.</li>
|
||||
</ul>
|
||||
<p><code>AnonThrottle</code> is suitable if you want to restrict the rate of requests from unknown sources.</p>
|
||||
<h2 id="userthrottle">UserThrottle</h2>
|
||||
<p><code>UserThrottle</code> is suitable if you want a simple restriction</p>
|
||||
<h2 id="scopedthrottle">ScopedThrottle</h2>
|
||||
<h2 id="userratethrottle">UserRateThrottle</h2>
|
||||
<p>The <code>UserThrottle</code> 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.</p>
|
||||
<p>The allowed request rate is determined from one of the following (in order of preference).</p>
|
||||
<ul>
|
||||
<li>The <code>rate</code> property on the class, which may be provided by overriding <code>UserThrottle</code> and setting the property.</li>
|
||||
<li>The <code>DEFAULT_THROTTLE_RATES['user']</code> setting.</li>
|
||||
</ul>
|
||||
<p><code>UserThrottle</code> is suitable if you want a simple global rate restriction per-user.</p>
|
||||
<h2 id="scopedratethrottle">ScopedRateThrottle</h2>
|
||||
<p>The <code>ScopedThrottle</code> 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 <code>.throttle_scope</code> property. The unique throttle key will then be formed by concatenating the "scope" of the request with the unqiue user id or IP address.</p>
|
||||
<p>The allowed request rate is determined by the <code>DEFAULT_THROTTLE_RATES</code> setting using a key from the request "scope".</p>
|
||||
<p>For example, given the following views...</p>
|
||||
<pre class="prettyprint lang-py"><code>class ContactListView(APIView):
|
||||
throttle_scope = 'contacts'
|
||||
...
|
||||
|
||||
class ContactDetailView(ApiView):
|
||||
throttle_scope = 'contacts'
|
||||
...
|
||||
|
||||
class UploadView(APIView):
|
||||
throttle_scope = 'uploads'
|
||||
...
|
||||
</code></pre>
|
||||
<p>...and the following settings.</p>
|
||||
<pre class="prettyprint lang-py"><code>API_SETTINGS = {
|
||||
'DEFAULT_THROTTLES': (
|
||||
'djangorestframework.throttles.ScopedRateThrottle',
|
||||
)
|
||||
'DEFAULT_THROTTLE_RATES': {
|
||||
'contacts': '1000/day',
|
||||
'uploads': '20/day'
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<p>User requests to either <code>ContactListView</code> or <code>ContactDetailView</code> would be restricted to a total of 1000 requests per-day. User requests to <code>UploadView</code> would be restricted to 20 requests per day.</p>
|
||||
<h2 id="custom-throttles">Custom throttles</h2>
|
||||
<p>To implement a custom throttle, override <code>BaseThrottle</code> and implement <code>.allow_request(request)</code>. The method should return <code>True</code> if the request should be allowed, and <code>False</code> otherwise.</p>
|
||||
<p>Optionally you may also override the <code>.wait()</code> method. If implemented, <code>.wait()</code> should return a recomended number of seconds to wait before attempting the next request, or <code>None</code>. The <code>.wait()</code> method will only be called if <code>.check_throttle()</code> has previously returned <code>False</code>.</p>
|
||||
</div><!--/span-->
|
||||
</div><!--/row-->
|
||||
</div><!--/.fluid-container-->
|
||||
|
|
Loading…
Reference in New Issue
Block a user