django-rest-framework/api-guide/throttling/index.html

649 lines
28 KiB
HTML
Raw Normal View History

2014-11-25 19:04:38 +03:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
2015-05-15 11:19:49 +03:00
<title>Throttling - Django REST framework</title>
2014-11-25 19:04:38 +03:00
<link href="../../img/favicon.ico" rel="icon" type="image/x-icon">
<link rel="canonical" href="http://www.django-rest-framework.org/api-guide/throttling/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Django, API, REST, Throttling">
<meta name="author" content="Tom Christie">
<!-- Le styles -->
<link href="../../css/prettify.css" rel="stylesheet">
<link href="../../css/bootstrap.css" rel="stylesheet">
<link href="../../css/bootstrap-responsive.css" rel="stylesheet">
<link href="../../css/default.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18852272-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<style>
2016-05-26 13:48:39 +03:00
#sidebarInclude img {
margin-bottom: 10px;
}
#sidebarInclude a.promo {
2014-11-25 19:04:38 +03:00
color: black;
2016-05-26 13:48:39 +03:00
}
2014-11-25 19:04:38 +03:00
@media (max-width: 767px) {
div.promo {
display: none;
}
}
</style>
</head>
<body onload="prettyPrint()" class="-page">
<div class="wrapper">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="repo-link btn btn-primary btn-small" href="https://github.com/encode/django-rest-framework/tree/master">GitHub</a>
2015-06-04 17:37:22 +03:00
<a class="repo-link btn btn-inverse btn-small " rel="prev" href="../filtering/">
2014-11-25 19:04:38 +03:00
Next <i class="icon-arrow-right icon-white"></i>
</a>
2015-06-04 17:37:22 +03:00
<a class="repo-link btn btn-inverse btn-small " rel="next" href="../permissions/">
2014-11-25 19:04:38 +03:00
<i class="icon-arrow-left icon-white"></i> Previous
</a>
2015-06-04 17:37:22 +03:00
<a id="search_modal_show" class="repo-link btn btn-inverse btn-small" href="#mkdocs_search_modal" data-toggle="modal" data-target="#mkdocs_search_modal"><i class="icon-search icon-white"></i> Search</a>
2014-11-25 19:04:38 +03:00
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="http://www.django-rest-framework.org">Django REST framework</a>
<div class="nav-collapse collapse">
<!-- Main navigation -->
<ul class="nav navbar-nav">
2015-06-04 17:37:22 +03:00
<li >
<a href="../..">Home</a>
</li>
2014-11-25 19:04:38 +03:00
<li class="dropdown">
2014-11-25 19:31:00 +03:00
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Tutorial <b class="caret"></b></a>
2014-11-25 19:04:38 +03:00
<ul class="dropdown-menu">
<li >
2015-06-04 17:37:22 +03:00
<a href="../../tutorial/quickstart/">Quickstart</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../tutorial/1-serialization/">1 - Serialization</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../tutorial/2-requests-and-responses/">2 - Requests and responses</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../tutorial/3-class-based-views/">3 - Class based views</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../tutorial/4-authentication-and-permissions/">4 - Authentication and permissions</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../tutorial/5-relationships-and-hyperlinked-apis/">5 - Relationships and hyperlinked APIs</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../tutorial/6-viewsets-and-routers/">6 - Viewsets and routers</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
<a href="../../tutorial/7-schemas-and-client-libraries/">7 - Schemas and client libraries</a>
</li>
2014-11-25 19:04:38 +03:00
</ul>
</li>
<li class="dropdown active">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">API Guide <b class="caret"></b></a>
<ul class="dropdown-menu">
<li >
2015-06-04 17:37:22 +03:00
<a href="../requests/">Requests</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../responses/">Responses</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../views/">Views</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../generic-views/">Generic views</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../viewsets/">Viewsets</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../routers/">Routers</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../parsers/">Parsers</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../renderers/">Renderers</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../serializers/">Serializers</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../fields/">Serializer fields</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../relations/">Serializer relations</a>
2014-11-25 19:04:38 +03:00
</li>
2014-12-01 15:20:07 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../validators/">Validators</a>
2014-12-01 15:20:07 +03:00
</li>
2014-11-25 19:04:38 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../authentication/">Authentication</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../permissions/">Permissions</a>
2014-11-25 19:04:38 +03:00
</li>
<li class="active" >
2015-06-04 17:37:22 +03:00
<a href="./">Throttling</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../filtering/">Filtering</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../pagination/">Pagination</a>
2014-11-25 19:04:38 +03:00
</li>
2015-03-06 15:05:16 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../versioning/">Versioning</a>
2015-03-06 15:05:16 +03:00
</li>
2014-11-25 19:04:38 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../content-negotiation/">Content negotiation</a>
2014-11-25 19:04:38 +03:00
</li>
2014-12-18 18:42:42 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../metadata/">Metadata</a>
2014-12-18 18:42:42 +03:00
</li>
<li >
<a href="../schemas/">Schemas</a>
</li>
2014-11-25 19:04:38 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../format-suffixes/">Format suffixes</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../reverse/">Returning URLs</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../exceptions/">Exceptions</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../status-codes/">Status codes</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../testing/">Testing</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../settings/">Settings</a>
2014-11-25 19:04:38 +03:00
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Topics <b class="caret"></b></a>
<ul class="dropdown-menu">
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/documenting-your-api/">Documenting your API</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
<a href="../../topics/api-clients/">API Clients</a>
</li>
2015-03-06 15:05:16 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/internationalization/">Internationalization</a>
2015-03-06 15:05:16 +03:00
</li>
2014-11-25 19:04:38 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/ajax-csrf-cors/">AJAX, CSRF & CORS</a>
2014-11-25 19:04:38 +03:00
</li>
2015-10-28 14:35:39 +03:00
<li >
<a href="../../topics/html-and-forms/">HTML & Forms</a>
</li>
2014-11-25 19:04:38 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/browser-enhancements/">Browser Enhancements</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/browsable-api/">The Browsable API</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/rest-hypermedia-hateoas/">REST, Hypermedia & HATEOAS</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
<a href="../../topics/third-party-packages/">Third Party Packages</a>
</li>
<li >
<a href="../../topics/tutorials-and-resources/">Tutorials and Resources</a>
2014-11-25 19:04:38 +03:00
</li>
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/contributing/">Contributing to REST framework</a>
2014-11-25 19:04:38 +03:00
</li>
2014-12-18 16:49:50 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/project-management/">Project management</a>
2014-12-18 16:49:50 +03:00
</li>
<li >
<a href="../../topics/jobs/">Jobs</a>
</li>
2014-11-25 19:04:38 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/3.0-announcement/">3.0 Announcement</a>
2014-11-25 19:04:38 +03:00
</li>
2014-12-01 15:20:07 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/3.1-announcement/">3.1 Announcement</a>
2014-12-01 15:20:07 +03:00
</li>
2015-08-06 16:31:52 +03:00
<li >
<a href="../../topics/3.2-announcement/">3.2 Announcement</a>
</li>
2015-10-28 14:35:39 +03:00
<li >
<a href="../../topics/3.3-announcement/">3.3 Announcement</a>
</li>
<li >
<a href="../../topics/3.4-announcement/">3.4 Announcement</a>
</li>
<li >
<a href="../../topics/3.5-announcement/">3.5 Announcement</a>
</li>
<li >
<a href="../../topics/3.6-announcement/">3.6 Announcement</a>
</li>
<li >
<a href="../../topics/3.7-announcement/">3.7 Announcement</a>
</li>
<li >
<a href="../../topics/3.8-announcement/">3.8 Announcement</a>
</li>
2014-11-25 19:04:38 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/kickstarter-announcement/">Kickstarter Announcement</a>
2014-11-25 19:04:38 +03:00
</li>
2016-05-26 13:48:39 +03:00
<li >
<a href="../../topics/mozilla-grant/">Mozilla Grant</a>
</li>
<li >
<a href="../../topics/funding/">Funding</a>
</li>
2014-11-25 19:04:38 +03:00
<li >
2015-06-04 17:37:22 +03:00
<a href="../../topics/release-notes/">Release Notes</a>
2014-11-25 19:04:38 +03:00
</li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
</div>
<div class="body-content">
<div class="container-fluid">
<!-- Search Modal -->
2015-06-04 17:37:22 +03:00
<div id="mkdocs_search_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
2014-11-25 19:04:38 +03:00
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h3 id="myModalLabel">Documentation search</h3>
</div>
<div class="modal-body">
2015-08-06 16:31:52 +03:00
<form role="form" autocomplete="off">
2015-06-04 17:37:22 +03:00
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search..." id="mkdocs-search-query">
</div>
</form>
<div id="mkdocs-search-results"></div>
2014-11-25 19:04:38 +03:00
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
<div class="row-fluid">
<div class="span3">
<div id="table-of-contents">
<ul class="nav nav-list side-nav well sidebar-nav-fixed">
<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="#how-clients-are-identified">How clients are identified</a>
</li>
<li>
<a href="#setting-up-the-cache">Setting up the cache</a>
</li>
<li class="main">
<a href="#api-reference">API Reference</a>
</li>
<li>
<a href="#anonratethrottle">AnonRateThrottle</a>
</li>
<li>
<a href="#userratethrottle">UserRateThrottle</a>
</li>
<li>
<a href="#scopedratethrottle">ScopedRateThrottle</a>
</li>
<li class="main">
<a href="#custom-throttles">Custom throttles</a>
</li>
<li>
<a href="#example">Example</a>
</li>
2016-05-26 13:48:39 +03:00
<div class="promo">
<hr/>
<div id="sidebarInclude">
</div>
2014-11-25 19:04:38 +03:00
</ul>
</div>
</div>
<div id="main-content" class="span9">
2015-06-04 17:37:22 +03:00
<a class="github" href="https://github.com/encode/django-rest-framework/tree/master/rest_framework/throttling.py">
2015-06-04 17:37:22 +03:00
<span class="label label-info">throttling.py</span>
</a>
2014-11-25 19:04:38 +03:00
2015-11-04 17:59:23 +03:00
<h1 id="throttling"><a class="toclink" href="#throttling">Throttling</a></h1>
2014-11-25 19:04:38 +03:00
<blockquote>
<p>HTTP/1.1 420 Enhance Your Calm</p>
<p><a href="https://developer.twitter.com/en/docs/basics/rate-limiting">Twitter API rate limiting response</a></p>
2014-11-25 19:04:38 +03:00
</blockquote>
2015-06-04 17:37:22 +03:00
<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>
2014-11-25 19:04:38 +03:00
<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 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>
2015-11-04 17:59:23 +03:00
<h2 id="how-throttling-is-determined"><a class="toclink" href="#how-throttling-is-determined">How throttling is determined</a></h2>
2014-11-25 19:04:38 +03:00
<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>
2015-11-04 17:59:23 +03:00
<h2 id="setting-the-throttling-policy"><a class="toclink" href="#setting-the-throttling-policy">Setting the throttling policy</a></h2>
2014-11-25 19:04:38 +03:00
<p>The default throttling policy may be set globally, using the <code>DEFAULT_THROTTLE_CLASSES</code> and <code>DEFAULT_THROTTLE_RATES</code> settings. For example.</p>
<pre><code>REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
),
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
</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 or per-viewset basis,
using the <code>APIView</code> class-based views.</p>
2014-11-25 19:04:38 +03:00
<pre><code>from rest_framework.response import Response
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView
class ExampleView(APIView):
throttle_classes = (UserRateThrottle,)
def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
</code></pre>
<p>Or, if you're using the <code>@api_view</code> decorator with function based views.</p>
<pre><code>@api_view(['GET'])
@throttle_classes([UserRateThrottle])
def example_view(request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
</code></pre>
2015-11-04 17:59:23 +03:00
<h2 id="how-clients-are-identified"><a class="toclink" href="#how-clients-are-identified">How clients are identified</a></h2>
<p>The <code>X-Forwarded-For</code> HTTP header and <code>REMOTE_ADDR</code> WSGI variable are used to uniquely identify client IP addresses for throttling. If the <code>X-Forwarded-For</code> header is present then it will be used, otherwise the value of the <code>REMOTE_ADDR</code> variable from the WSGI environment will be used.</p>
<p>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 <code>NUM_PROXIES</code> setting. This setting should be an integer of zero or more. If set to non-zero then the client IP will be identified as being the last IP address in the <code>X-Forwarded-For</code> header, once any application proxy IP addresses have first been excluded. If set to zero, then the <code>REMOTE_ADDR</code> value will always be used as the identifying IP address.</p>
<p>It is important to understand that if you configure the <code>NUM_PROXIES</code> setting, then all clients behind a unique <a href="https://en.wikipedia.org/wiki/Network_address_translation">NAT'd</a> gateway will be treated as a single client.</p>
2014-11-25 19:04:38 +03:00
<p>Further context on how the <code>X-Forwarded-For</code> header works, and identifying a remote client IP can be <a href="http://oxpedia.org/wiki/index.php?title=AppSuite:Grizzly#Multiple_Proxies_in_front_of_the_cluster">found here</a>.</p>
2015-11-04 17:59:23 +03:00
<h2 id="setting-up-the-cache"><a class="toclink" href="#setting-up-the-cache">Setting up the cache</a></h2>
<p>The throttle classes provided by REST framework use Django's cache backend. You should make sure that you've set appropriate <a href="https://docs.djangoproject.com/en/stable/ref/settings/#caches">cache settings</a>. The default value of <code>LocMemCache</code> backend should be okay for simple setups. See Django's <a href="https://docs.djangoproject.com/en/stable/topics/cache/#setting-up-the-cache">cache documentation</a> for more details.</p>
2014-11-25 19:04:38 +03:00
<p>If you need to use a cache other than <code>'default'</code>, you can do so by creating a custom throttle class and setting the <code>cache</code> attribute. For example:</p>
<pre><code>class CustomAnonRateThrottle(AnonRateThrottle):
cache = get_cache('alternate')
</code></pre>
<p>You'll need to remember to also set your custom throttle class in the <code>'DEFAULT_THROTTLE_CLASSES'</code> settings key, or using the <code>throttle_classes</code> view attribute.</p>
<hr />
2015-11-04 17:59:23 +03:00
<h1 id="api-reference"><a class="toclink" href="#api-reference">API Reference</a></h1>
<h2 id="anonratethrottle"><a class="toclink" href="#anonratethrottle">AnonRateThrottle</a></h2>
2014-11-25 19:04:38 +03:00
<p>The <code>AnonRateThrottle</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>AnonRateThrottle</code> and setting the property.</li>
<li>The <code>DEFAULT_THROTTLE_RATES['anon']</code> setting.</li>
</ul>
<p><code>AnonRateThrottle</code> is suitable if you want to restrict the rate of requests from unknown sources.</p>
2015-11-04 17:59:23 +03:00
<h2 id="userratethrottle"><a class="toclink" href="#userratethrottle">UserRateThrottle</a></h2>
2014-11-25 19:04:38 +03:00
<p>The <code>UserRateThrottle</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. Unauthenticated requests will fall back to using the IP address of the incoming request 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>UserRateThrottle</code> and setting the property.</li>
<li>The <code>DEFAULT_THROTTLE_RATES['user']</code> setting.</li>
</ul>
<p>An API may have multiple <code>UserRateThrottles</code> in place at the same time. To do so, override <code>UserRateThrottle</code> and set a unique "scope" for each class.</p>
<p>For example, multiple user throttle rates could be implemented by using the following classes...</p>
<pre><code>class BurstRateThrottle(UserRateThrottle):
scope = 'burst'
class SustainedRateThrottle(UserRateThrottle):
scope = 'sustained'
</code></pre>
<p>...and the following settings.</p>
<pre><code>REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'example.throttles.BurstRateThrottle',
'example.throttles.SustainedRateThrottle'
),
'DEFAULT_THROTTLE_RATES': {
'burst': '60/min',
'sustained': '1000/day'
}
}
</code></pre>
<p><code>UserRateThrottle</code> is suitable if you want simple global rate restrictions per-user.</p>
2015-11-04 17:59:23 +03:00
<h2 id="scopedratethrottle"><a class="toclink" href="#scopedratethrottle">ScopedRateThrottle</a></h2>
2014-11-25 19:04:38 +03:00
<p>The <code>ScopedRateThrottle</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 unique 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><code>class ContactListView(APIView):
throttle_scope = 'contacts'
...
2015-09-11 15:06:48 +03:00
class ContactDetailView(APIView):
2014-11-25 19:04:38 +03:00
throttle_scope = 'contacts'
...
class UploadView(APIView):
throttle_scope = 'uploads'
...
</code></pre>
<p>...and the following settings.</p>
<pre><code>REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.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>
<hr />
2015-11-04 17:59:23 +03:00
<h1 id="custom-throttles"><a class="toclink" href="#custom-throttles">Custom throttles</a></h1>
2014-11-25 19:04:38 +03:00
<p>To create a custom throttle, override <code>BaseThrottle</code> and implement <code>.allow_request(self, request, view)</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 recommended 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>.allow_request()</code> has previously returned <code>False</code>.</p>
<p>If the <code>.wait()</code> method is implemented and the request is throttled, then a <code>Retry-After</code> header will be included in the response.</p>
2015-11-04 17:59:23 +03:00
<h2 id="example"><a class="toclink" href="#example">Example</a></h2>
2014-11-25 19:04:38 +03:00
<p>The following is an example of a rate throttle, that will randomly throttle 1 in every 10 requests.</p>
<pre><code>import random
class RandomRateThrottle(throttling.BaseThrottle):
2014-11-25 19:04:38 +03:00
def allow_request(self, request, view):
return random.randint(1, 10) != 1
2014-11-25 19:04:38 +03:00
</code></pre>
2015-06-04 17:37:22 +03:00
2015-06-23 14:05:53 +03:00
</div> <!--/span-->
</div> <!--/row-->
</div> <!--/.fluid-container-->
</div> <!--/.body content-->
2014-11-25 19:04:38 +03:00
<div id="push"></div>
2015-06-23 14:05:53 +03:00
</div> <!--/.wrapper -->
2014-11-25 19:04:38 +03:00
<footer class="span12">
2015-10-28 14:35:39 +03:00
<p>Documentation built with <a href="http://www.mkdocs.org/">MkDocs</a>.
2014-11-25 19:04:38 +03:00
</p>
</footer>
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../../js/jquery-1.8.1-min.js"></script>
<script src="../../js/prettify-1.0.js"></script>
<script src="../../js/bootstrap-2.1.1-min.js"></script>
2016-05-26 13:48:39 +03:00
<script src="https://fund.django-rest-framework.org/sidebar_include.js"></script>
2015-06-04 17:37:22 +03:00
<script>var base_url = '../..';</script>
<script src="../../mkdocs/js/require.js"></script>
2014-12-11 12:55:10 +03:00
<script src="../../js/theme.js"></script>
2014-11-25 19:04:38 +03:00
<script>
var shiftWindow = function() {
scrollBy(0, -50)
};
2015-06-23 14:05:53 +03:00
2014-11-25 19:04:38 +03:00
if (location.hash) shiftWindow();
window.addEventListener("hashchange", shiftWindow);
$('.dropdown-menu').on('click touchstart', function(event) {
event.stopPropagation();
});
2015-06-23 14:05:53 +03:00
// Dynamically force sidenav/dropdown to no higher than browser window
$('.side-nav, .dropdown-menu').css('max-height', window.innerHeight - 130);
2014-11-25 19:04:38 +03:00
$(function() {
$(window).resize(function() {
2015-06-23 14:05:53 +03:00
$('.side-nav, .dropdown-menu').css('max-height', window.innerHeight - 130);
2014-11-25 19:04:38 +03:00
});
});
</script>
</body>
</html>