File: storage.rst

package info (click to toggle)
python-limits 4.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 7,833; makefile: 162; sh: 59
file content (271 lines) | stat: -rw-r--r-- 9,464 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
.. currentmodule:: limits

.. _storage:

================
Storage Backends
================

Supported versions
==================

**limits** is tested and known to work with the following versions of the
dependency libraries and the associated storage versions.

The CI tests against these versions on a nightly basis and you can see
the results in `github <https://github.com/alisaifee/limits/actions/workflows/compatibility.yml>`_.

.. tab:: Redis

   Dependency versions:

   .. literalinclude:: ../../requirements/storage/redis.txt

   Dependency versions (async):

   .. literalinclude:: ../../requirements/storage/async-redis.txt

   .. note::
     .. versionadded:: 4.2
        :pypi:`redis` can be used instead of :pypi:`coredis` by setting
        :paramref:`limits.aio.storage.RedisStorage.implementation` to ``redispy``

   `Redis <https://redis.io>`_

   .. program-output:: bash -c "cat ../../.github/workflows/compatibility.yml | grep -o -P 'LIMITS_REDIS_SERVER_VERSION=[\d\.]+' | cut -d = -f 2 | sort --version-sort | uniq"

   Redis with SSL

   .. program-output:: bash -c "cat ../../.github/workflows/compatibility.yml | grep -o -P 'LIMITS_REDIS_SERVER_SSL_VERSION=[\d\.]+' | cut -d = -f 2 | sort --version-sort | uniq"

   `Redis Sentinel <https://redis.io/topics/sentinel>`_

   .. program-output:: bash -c "cat ../../.github/workflows/compatibility.yml | grep -o -P 'LIMITS_REDIS_SENTINEL_SERVER_VERSION=[\d\.]+' | cut -d = -f 2 | sort --version-sort | uniq"

.. tab:: Redis Cluster

   Dependency versions:

   .. literalinclude:: ../../requirements/storage/rediscluster.txt

   Dependency versions (async):

   .. literalinclude:: ../../requirements/storage/async-redis.txt

   .. note::
      .. versionadded:: 4.2
         :pypi:`redis` can be used instead of :pypi:`coredis` by setting
         :paramref:`limits.aio.storage.RedisClusterStorage.implementation` to ``redispy``

   `Redis cluster <https://redis.io/topics/cluster-tutorial>`_

   .. program-output:: bash -c "cat ../../.github/workflows/compatibility.yml | grep -o -P 'LIMITS_REDIS_SERVER_VERSION=[\d\.]+' | cut -d = -f 2 | sort --version-sort | uniq"

.. tab:: Memcached

   Dependency versions:

   .. literalinclude:: ../../requirements/storage/memcached.txt

   Dependency versions (async):

   .. literalinclude:: ../../requirements/storage/async-memcached.txt

   `Memcached <https://memcached.org/>`_

   .. program-output:: bash -c "cat ../../.github/workflows/compatibility.yml | grep -o -P 'LIMITS_MEMCACHED_SERVER_VERSION=[\d\.]+' | cut -d = -f 2 | sort --version-sort | uniq"

.. tab:: MongoDB

   Dependency versions:

   .. literalinclude:: ../../requirements/storage/mongodb.txt

   Dependency versions (async):

   .. literalinclude:: ../../requirements/storage/async-mongodb.txt

   `MongoDB <https://www.mongodb.com/>`_

   .. program-output:: bash -c "cat ../../.github/workflows/compatibility.yml | grep -o -P 'LIMITS_MONGODB_SERVER_VERSION=[\d\.]+' | cut -d = -f 2 | sort --version-sort | uniq"

.. tab:: Etcd

   Dependency versions:

   .. literalinclude:: ../../requirements/storage/etcd.txt

   Dependency versions (async):

   .. literalinclude:: ../../requirements/storage/async-etcd.txt

   `Etcd <https://www.etcd.io/>`_

   .. program-output:: bash -c "cat ../../.github/workflows/compatibility.yml | grep -o -P 'LIMITS_ETCD_SERVER_VERSION=[\d\.]+' | cut -d = -f 2 | sort --version-sort | uniq"

.. tab:: Valkey

   Dependency versions:

   .. literalinclude:: ../../requirements/storage/valkey.txt

   Dependency versions (async):

   .. literalinclude:: ../../requirements/storage/async-valkey.txt

   `Valkey <https://www.valkey.io/>`_

   .. program-output:: bash -c "cat ../../.github/workflows/compatibility.yml | grep -o -P 'LIMITS_VALKEY_SERVER_VERSION=[\d\.]+' | cut -d = -f 2 | sort --version-sort | uniq"

Storage scheme
==============

**limits** uses a url style storage scheme notation (similar to the JDBC driver
connection string notation) for configuring and initializing
storage backends. This notation additionally provides a simple
mechanism to both identify and configure the backend implementation based on
a single string argument.

The storage scheme follows the format :code:`{scheme}://{parameters}`

:func:`limits.storage.storage_from_string` is provided to
lookup and construct an instance of a storage based on the storage scheme. For example::

 import limits.storage
 uri = "redis://localhost:9999"
 options = {}
 redis_storage = limits.storage.storage_from_string(uri, **options)

The additional `options` key-word arguments are passed as is to the constructor of the storage
and handled differently by each implementation. Please refer to the API documentation in
the :ref:`api:storage` section for details.


Examples
========

In-Memory Storage
-----------------
The in-memory storage (:class:`~limits.storage.MemoryStorage`) takes no parameters so the only relevant value is :code:`memory://`

Memcached Storage
-----------------

Requires the location of the memcached server(s). As such
the parameters is a comma separated list of :code:`{host}:{port}` locations such as
:code:`memcached://localhost:11211` or
:code:`memcached://localhost:11211,localhost:11212,192.168.1.1:11211` etc...
or a path to a unix domain socket such as :code:`memcached:///var/tmp/path/to/sock`

Depends on: :pypi:`pymemcache`

Redis Storage
-------------

Requires the location of the redis server and optionally the database number.
:code:`redis://localhost:6379` or :code:`redis://localhost:6379/n` (for database `n`).

If the redis server is listening over a unix domain socket you can use :code:`redis+unix:///path/to/sock`
or :code:`redis+unix:///path/to/socket?db=n` (for database `n`).

If the database is password protected the password can be provided in the url, for example
:code:`redis://:foobared@localhost:6379` or :code:`redis+unix://:foobered/path/to/socket` if using a UDS.

For scenarios where a redis connection pool is already available and can be reused, it can be provided
in :paramref:`~limits.storage.storage_from_string.options`, for example::

    pool = redis.connections.BlockingConnectionPool.from_url("redis://.....")
    storage_from_string("redis://", connection_pool=pool)

Depends on: :pypi:`redis`


.. versionadded:: 4.3

   If the database ``uri`` scheme uses ``valkey`` instead of ``redis`` the implementation
   used will be from :pypi:`valkey` instead of :pypi:`redis`.

Redis+SSL Storage
-----------------

The official Redis client :pypi:`redis` supports redis connections over SSL with the scheme
You can add ssl related parameters in the url itself, for example:
:code:`rediss://localhost:6379/0?ssl_ca_certs=./tls/ca.crt&ssl_keyfile=./tls/client.key`.


Depends on: :pypi:`redis`

Redis+Sentinel Storage
----------------------

Requires the location(s) of the redis sentinel instances and the `service-name`
that is monitored by the sentinels.
:code:`redis+sentinel://localhost:26379/my-redis-service`
or :code:`redis+sentinel://localhost:26379,localhost:26380/my-redis-service`.

If the sentinel is password protected the username and/or password can be provided in the url,
for example  :code:`redis+sentinel://:sekret@localhost:26379/my-redis-service`

When authentication details are provided in the url they will be used for both the sentinel
and as connection arguments for the underlying redis nodes managed by the sentinel.

If you need fine grained control it is recommended to use the additional :paramref:`~limits.storage.storage_from_string.options`
arguments. More details can be found in the API documentation for :class:`~limits.storage.RedisSentinelStorage` (or the async version: :class:`~limits.aio.storage.RedisSentinelStorage`).

Depends on: :pypi:`redis`

Redis Cluster Storage
---------------------

Requires the location(s) of the redis cluster startup nodes (One is enough).
:code:`redis+cluster://localhost:7000`
or :code:`redis+cluster://localhost:7000,localhost:7001`

If the cluster is password protected the username and/or password can be provided in the url,
for example  :code:`redis+cluster://:sekret@localhost:7000,localhost:7001`

Depends on: :pypi:`redis`

MongoDB Storage
---------------

Requires the location(s) of a mongodb installation using the uri schema
described by the `Mongodb URI Specification <https://github.com/mongodb/specifications/blob/master/source/uri-options/uri-options.md>`_

Examples:

  - Local instance: ``mongodb://localhost:27017/``
  - Instance with SSL: ``mongodb://mymongo.com/?tls=true``
  - Local instance with SSL & self signed/invalid certificate: ``mongodb://localhost:27017/?tls=true&tlsAllowInvalidCertificates=true``

Depends on: :pypi:`pymongo`

Etcd Storage
------------

Requires the location of an etcd node

Example: ``etcd://localhost:2379``

Depends on: :pypi:`etcd3`

Async Storage
=============

.. versionadded:: 2.1

When using limits in an async code base the same uri schema can be used
to query for an async implementation of the storage by prefixing the
scheme with ``async+``.

For example:

- ``async+redis://localhost:6379/0``
- ``async+rediss://localhost:6379/0``
- ``async+redis+cluster://localhost:7000,localhost:7001``
- ``async+redis+sentinel://:sekret@localhost:26379/my-redis-service``
- ``async+memcached://localhost:11211``
- ``async+etcd://localhost:2379``
- ``async+memory://``

For implementation details of currently supported async backends refer to :ref:`api:async storage`