File: rates.rst

package info (click to toggle)
python-django-ratelimit 4.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 348 kB
  • sloc: python: 892; makefile: 134; sh: 49
file content (69 lines) | stat: -rw-r--r-- 1,634 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
.. _rates-chapter:

=====
Rates
=====


.. _rates-simple:

Simple rates
============

Simple rates are of the form ``X/u`` where ``X`` is a number of requests
and ``u`` is a unit from this list:

* ``s`` - second
* ``m`` - minute
* ``h`` - hour
* ``d`` - day

(For example, you can read ``5/s`` as "five per second.")

.. note::

    Setting a rate of 0 per any unit of time will disallow requests,
    e.g.  ``0/s`` will prevent any requests to the endpoint.

Rates may also be set to ``None``, which indicates "there is no limit."
Usage will not be tracked.

You may also specify a number of units, i.e.: ``X/Yu`` where ``Y`` is a
number of units. If ``u`` is omitted, it is presumed to be seconds. So,
the following are equivalent, and all mean "one hundred requests per
five minutes":

* ``100/5m``
* ``100/300s``
* ``100/300``


.. _rates-callable:

Callables
=========

.. versionadded:: 0.5

Rates can also be callables (or dotted paths to callables, which are
assumed if there is a ``.`` in the value).

Callables receive two values, the :ref:`group <usage-chapter>` and the
``request`` object. They should return a simple rate string, or a tuple
of integers ``(count, seconds)``. For example::

    def my_rate(group, request):
        if request.user.is_authenticated:
            return '1000/m'
        return '100/m'

Or equivalently::

    def my_rate_tuples(group, request):
        if request.user.is_authenticated:
            return (1000, 60)
        return (100, 60)

Callables can return ``0`` in the first place to disallow any requests
(e.g.: ``0/s``, ``(0, 60)``). They can return ``None`` for "no
ratelimit".