File: ratelimitqueue.py

package info (click to toggle)
python-ratelimitqueue 0.2.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: python: 463; makefile: 15
file content (394 lines) | stat: -rw-r--r-- 13,320 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import time
import random

import queue
import multiprocessing.dummy as mp

from .exceptions import RateLimitException
from . import utils


class RateLimitGetMixin:
    """Adds rate limiting to another class' `get()` method.

    Assumes that the class being extended has properties `calls` (int),
    `per` (float), `fuzz` (float), and `_call_log` (queue.Queue), else will
    raise AttributeError on call of get().
    """

    def get(self, block=True, timeout=None):
        """
        Get an item from the queue.

        If optional args `block` is True and `timeout` is None (the default),
        block if necessary until a free slot is available and the rate limit
        has not been reached. If `timeout` is a non-negative number, it blocks
        at most `timeout` seconds and raises the RateLimitException if
        the required rate limit waiting time is shorter than the given timeout,
        or the Empty exception if no item was available within that time.

        Otherwise (`block` is False), get an item on the queue if an item
        is immediately available and the rate limit has not been hit. Else
        raise the RateLimitException if waiting on the rate limit, or
        Empty exception if there is no item available in the queue. Timeout
        is ignored in this case.

        Parameters
        ----------
        block : bool, optional, default True
            Whether to block until an item can be gotten from the queue

        timeout : float, optional, default None
            The maximum amount of time to block for

        """
        start = time.time()
        if timeout is not None and timeout < 0:
            raise ValueError("`timeout` must be a non-negative number")

        # acquire lock
        self._acquire_or_raise(self._pending_get, block, timeout)

        # make sure child class has the required attributes
        self._check_attributes()

        # get snapshot of properties so no need to lock
        per = self.per
        fuzz = self.fuzz

        if self._call_log.qsize() >= self.calls:
            # get the earliest call in the queue
            first_call = self._call_log.get()

            time_since_call = time.time() - first_call

            if time_since_call < per:
                # sleep long enough that we don't
                # go over the calls per unit time
                if block:
                    time_remaining = utils.get_time_remaining(start, timeout)
                    sleep_time = per - time_since_call

                    # not enough time to complete sleep -> exception
                    if (
                        time_remaining is not None
                        and time_remaining < sleep_time
                    ):
                        self._call_log.task_done()
                        raise RateLimitException(
                            "Not enough time in timeout to wait for next item"
                        )
                    else:
                        time.sleep(sleep_time)

                # too fast but not blocking -> exception
                else:
                    self._call_log.task_done()
                    raise RateLimitException("Too many requests")

            self._call_log.task_done()

        # starting to load up the queue, don't hammer gets with all allowed
        # calls at once
        elif fuzz > 0:
            time_remaining = utils.get_time_remaining(start, timeout)
            fuzz_time = random.uniform(0, fuzz)

            if time_remaining is not None:
                # timeout is set, so leave a bit of leeway from time_remaining
                # to not time out due to fuzzing
                fuzz_time = min(fuzz_time, time_remaining - 0.01)

            time.sleep(fuzz_time)

        # get remaining timeout time for the call to super().get()
        time_remaining = utils.get_time_remaining(start, timeout)

        if time_remaining is not None and time_remaining <= 0:
            raise TimeoutError

        # log the call, release the lock, and return the next item
        self._call_log.put(time.time())
        self._pending_get.release()
        return super().get(block, timeout=time_remaining)

    def _check_attributes(self):
        """Check that calling object has properties calls, per, fuzz,
        _call_log, and get()"""
        if not hasattr(self, "calls"):
            raise AttributeError(
                "RateLimitGetMixin requires the `.calls` property"
            )

        if not hasattr(self, "per"):
            raise AttributeError(
                "RateLimitGetMixin requires the `.per` property"
            )

        if not hasattr(self, "fuzz"):
            raise AttributeError(
                "RateLimitGetMixin requires the `.fuzz` property"
            )

        if not hasattr(self, "_call_log"):
            raise AttributeError(
                "RateLimitGetMixin requires the `._call_log` Queue"
            )

        if not hasattr(super(), "get"):
            raise AttributeError(
                "RateLimitGetMixin must be mixed into a base class with"
                " the `.get()` method"
            )

    @staticmethod
    def _acquire_or_raise(lock, block=True, timeout=None):
        """Attempt to acquire `lock`, else raise RateLimitException"""
        if block and timeout is not None:
            locked = lock.acquire(block, timeout)
        else:
            locked = lock.acquire(block)

        if not locked:
            raise RateLimitException("Timed out waiting for next item")


class RateLimitQueue(RateLimitGetMixin, queue.Queue):
    def __init__(self, maxsize=0, calls=1, per=1.0, fuzz=0):
        """
        A thread safe queue with a given maximum size and rate limit.

        If `maxsize` is <= 0, the queue size is infinite (see
        `queue.Queue`).

        The rate limit is described as `calls` `per` time window, with
        `per` measured in seconds. The default rate limit is 1 call per
        second. If `per` is <= 0, the rate limit is infinite.

        To avoid immediately getting the maximum allowed items at startup, an
        extra randomized wait period can be configured with `fuzz`.
        This will cause the RateLimitQueue to wait between 0 and `fuzz`
        seconds before getting the object in the queue. Fuzzing only
        occurs if there is no rate limit waiting to be done.

        Parameters
        ----------
        maxsize : int, optional, default 0
            The number of slots in the queue, <=0 for infinite.

        calls : int, optional, default 1
            The number of call per time unit `per`. Must be at least 1.

        per : float, optional, default 1.0
            The time window for tracking calls, in seconds, <=0 for
            infinite rate limit.

        fuzz: float, options, default 0
            The maximum length (in seconds) of fuzzed extra sleep, <=0
            for no fuzzing

        Examples
        --------

        Basic usage:

            >>> rlq = RateLimitQueue()
            >>> rlq.put(1)
            >>> rlq.put(2)
            >>> rlq.get()
            1
            >>> rlq.get()
            2

        A rate limit of 3 calls per 5 seconds:

            >>> rlq = RateLimitQueue(calls=3, per=5)

        A queue with the default 1 call per second, with a maximum size
        of 3:

            >>> rlq = RateLimitQueue(3)

        A queue of infinite size and rate limit, equivalent to
        queue.Queue():

            >>> rlq = RateLimitQueue(per=0)

        A queue with wait time fuzzing up to 1 second so that the queue
        cannot be filled immediately directly after instantiation:

            >>> rlq = RateLimitQueue(fuzz=1)

        """
        if calls < 1:
            raise ValueError("`calls` must be an integer >= 1")

        super().__init__(maxsize)
        self.calls = int(calls)
        self.per = float(per)
        self.fuzz = float(fuzz)

        self._call_log = queue.Queue(maxsize=self.calls)
        self._pending_get = mp.Lock()


class RateLimitLifoQueue(RateLimitGetMixin, queue.LifoQueue):
    def __init__(self, maxsize=0, calls=1, per=1.0, fuzz=0):
        """
        A thread safe LIFO queue with a given maximum size and rate limit.

        If `maxsize` is <= 0, the queue size is infinite (see
        `queue.LifoQueue`).

        The rate limit is described as `calls` `per` time window, with
        `per` measured in seconds. The default rate limit is 1 call per
        second. If `per` is <= 0, the rate limit is infinite.

        To avoid immediately filling the whole queue at startup, an
        extra randomized wait period can be configured with `fuzz`.
        This will cause the RateLimitQueue to wait between 0 and `fuzz`
        seconds before putting the object in the queue. Fuzzing only
        occurs if there is no rate limit waiting to be done.

        Parameters
        ----------
        maxsize : int, optional, default 0
            The number of slots in the queue, <=0 for infinite.

        calls : int, optional, default 1
            The number of call per time unit `per`. Must be at least 1.

        per : float, optional, default 1.0
            The time window for tracking calls, in seconds, <=0 for
            infinite rate limit.

        fuzz: float, options, default 0
            The maximum length (in seconds) of fuzzed extra sleep, <=0
            for no fuzzing

        Examples
        --------
        Basic usage:

            >>> rlq = RateLimitLifoQueue()
            >>> rlq.put(1)
            >>> rlq.put(2)
            >>> rlq.get()
            2
            >>> rlq.get()
            1

        A rate limit of 3 calls per 5 seconds:

            >>> rlq = RateLimitLifoQueue(calls=3, per=5)

        A queue with the default 1 call per second, with a maximum size
        of 3:

            >>> rlq = RateLimitLifoQueue(3)

        A queue of infinite size and rate limit, equivalent to
        queue.Queue():

            >>> rlq = RateLimitLifoQueue(per=0)

        A queue with wait time fuzzing up to 1 second so that the queue
        cannot be filled immediately directly after instantiation:

            >>> rlq = RateLimitLifoQueue(fuzz=1)

        """
        if calls < 1:
            raise ValueError("`calls` must be an integer >= 1")

        super().__init__(maxsize)
        self.calls = int(calls)
        self.per = float(per)
        self.fuzz = float(fuzz)

        self._call_log = queue.Queue(maxsize=self.calls)
        self._pending_get = mp.Lock()


class RateLimitPriorityQueue(RateLimitGetMixin, queue.PriorityQueue):
    def __init__(self, maxsize=0, calls=1, per=1.0, fuzz=0):
        """
        A thread safe priority queue with a given maximum size and rate
        limit.

        Prioritized items should be tuples of form (priority, item), with
        priority lowest first. Priority determines the order of items
        returned by get().

        If `maxsize` is <= 0, the queue size is infinite (see
        `queue.LifoQueue`).

        The rate limit is described as `calls` `per` time window, with
        `per` measured in seconds. The default rate limit is 1 call per
        second. If `per` is <= 0, the rate limit is infinite.

        To avoid immediately filling the whole queue at startup, an
        extra randomized wait period can be configured with `fuzz`.
        This will cause the RateLimitQueue to wait between 0 and `fuzz`
        seconds before putting the object in the queue. Fuzzing only
        occurs if there is no rate limit waiting to be done.

        Parameters
        ----------
        maxsize : int, optional, default 0
            The number of slots in the queue, <=0 for infinite.

        calls : int, optional, default 1
            The number of call per time unit `per`. Must be at least 1.

        per : float, optional, default 1.0
            The time window for tracking calls, in seconds, <=0 for
            infinite rate limit.

        fuzz: float, options, default 0
            The maximum length (in seconds) of fuzzed extra sleep, <=0
            for no fuzzing

        Examples
        --------

        Basic usage:

            >>> rlq = RateLimitPriorityQueue()
            >>> rlq.put((2, 'second'))
            >>> rlq.put((1, 'first'))
            >>> rlq.get()
            (1, 'first')
            >>> rlq.get()
            (2, 'second')

        A rate limit of 3 calls per 5 seconds:

            >>> rlq = RateLimitPriorityQueue(calls=3, per=5)

        A queue with the default 1 call per second, with a maximum size
        of 3:

            >>> rlq = RateLimitPriorityQueue(3)

        A queue of infinite size and rate limit, equivalent to
        queue.Queue():

            >>> rlq = RateLimitPriorityQueue(per=0)

        A queue with wait time fuzzing up to 1 second so that the queue
        cannot be filled immediately directly after instantiation:

            >>> rlq = RateLimitPriorityQueue(fuzz=1)

        """
        if calls < 1:
            raise ValueError("`calls` must be an integer >= 1")

        super().__init__(maxsize)
        self.calls = int(calls)
        self.per = float(per)
        self.fuzz = float(fuzz)

        self._call_log = queue.Queue(maxsize=self.calls)
        self._pending_get = mp.Lock()