From 0429357a41ab0224ae48348ef27cc2d041670ce4 Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Wed, 30 May 2018 14:53:51 -0400 Subject: [PATCH 01/17] Add consume units --- ratelimiter/_sync.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ratelimiter/_sync.py b/ratelimiter/_sync.py index 577dfb4..667193e 100644 --- a/ratelimiter/_sync.py +++ b/ratelimiter/_sync.py @@ -12,7 +12,7 @@ class RateLimiter(object): requests for a time period. """ - def __init__(self, max_calls, period=1.0, callback=None): + def __init__(self, max_calls, period=1.0, callback=None, consume=1): """Initialize a RateLimiter object which enforces as much as max_calls operations on period (eventually floating) number of seconds. """ @@ -28,6 +28,7 @@ def __init__(self, max_calls, period=1.0, callback=None): self.period = period self.max_calls = max_calls self.callback = callback + self.consume = consume self._lock = threading.Lock() self._alock = None @@ -49,7 +50,7 @@ def __enter__(self): # We want to ensure that no more than max_calls were run in the allowed # period. For this, we store the last timestamps of each call and run # the rate verification upon each __enter__ call. - if len(self.calls) >= self.max_calls: + if len(self.calls) + self.consume > self.max_calls: until = time.time() + self.period - self._timespan if self.callback: t = threading.Thread(target=self.callback, args=(until,)) @@ -63,7 +64,8 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb): with self._lock: # Store the last operation timestamp. - self.calls.append(time.time()) + timestamps = [ time.time() ] * self.consume + self.calls.append(timestamps) # Pop the timestamp list front (ie: the older calls) until the sum goes # back below the period. This is our 'sliding period' window. @@ -73,3 +75,7 @@ def __exit__(self, exc_type, exc_val, exc_tb): @property def _timespan(self): return self.calls[-1] - self.calls[0] + + @property + def _remaining(self): + return self.max_calls -self.calls \ No newline at end of file From 94330fd315cb262db01b9c44ebca5a0685964127 Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Thu, 31 May 2018 07:06:05 -0400 Subject: [PATCH 02/17] Extend vs append --- .cache/v/cache/lastfailed | 1 + ratelimiter/_sync.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .cache/v/cache/lastfailed diff --git a/.cache/v/cache/lastfailed b/.cache/v/cache/lastfailed new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/.cache/v/cache/lastfailed @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/ratelimiter/_sync.py b/ratelimiter/_sync.py index 667193e..74cb3c5 100644 --- a/ratelimiter/_sync.py +++ b/ratelimiter/_sync.py @@ -65,7 +65,7 @@ def __exit__(self, exc_type, exc_val, exc_tb): with self._lock: # Store the last operation timestamp. timestamps = [ time.time() ] * self.consume - self.calls.append(timestamps) + self.calls.extend(timestamps) # Pop the timestamp list front (ie: the older calls) until the sum goes # back below the period. This is our 'sliding period' window. From 13f4c6353d5d74a9482bb2904788336ef9a40082 Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Fri, 8 Jun 2018 13:18:14 -0400 Subject: [PATCH 03/17] Initial implementation of unit test 3 --- tests/test_ratelimiter.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_ratelimiter.py b/tests/test_ratelimiter.py index abe5ed4..4206e82 100644 --- a/tests/test_ratelimiter.py +++ b/tests/test_ratelimiter.py @@ -80,6 +80,15 @@ def test_limit_2(self): self.assertEqual(len(calls), 3 * self.max_calls) self.validate_call_times(calls, self.max_calls, self.period) + def test_limit_3(self): + with Timer() as timer: + obj = RateLimiter(self.max_calls, self.period, consume=2) + for i in range(self.max_calls+1): + with obj: + pass + + self.assertGreaterEqual(timer.duration, self.period * obj.consume) + def test_decorator_1(self): @RateLimiter(self.max_calls, self.period) def f(): From 5be9f49305ed222400ce8677f4ad6556b90e0373 Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Fri, 8 Jun 2018 13:37:57 -0400 Subject: [PATCH 04/17] More consume unit test - test_limit_4 --- tests/test_ratelimiter.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/test_ratelimiter.py b/tests/test_ratelimiter.py index 4206e82..d317172 100644 --- a/tests/test_ratelimiter.py +++ b/tests/test_ratelimiter.py @@ -75,7 +75,7 @@ def test_limit_2(self): obj = RateLimiter(self.max_calls, self.period) for i in range(3 * self.max_calls): with obj: - calls.append(time.time()) + calls.extend([time.time()] * obj.consume) self.assertEqual(len(calls), 3 * self.max_calls) self.validate_call_times(calls, self.max_calls, self.period) @@ -89,6 +89,16 @@ def test_limit_3(self): self.assertGreaterEqual(timer.duration, self.period * obj.consume) + def test_limit_4(self): + calls = [] + obj = RateLimiter(self.max_calls, self.period, consume=3) + for i in range(3 * self.max_calls): + with obj: + calls.extend([time.time()] * obj.consume) + + self.assertEqual(len(calls), 3 * self.max_calls * obj.consume) + self.validate_call_times(calls, self.max_calls, self.period) + def test_decorator_1(self): @RateLimiter(self.max_calls, self.period) def f(): From 3856aa7c0aa18007bf18ec140c90942eac9a02d1 Mon Sep 17 00:00:00 2001 From: dedoussis Date: Sat, 9 Jun 2018 09:32:02 -0400 Subject: [PATCH 05/17] Remove remaining property --- ratelimiter/_sync.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ratelimiter/_sync.py b/ratelimiter/_sync.py index 74cb3c5..1213094 100644 --- a/ratelimiter/_sync.py +++ b/ratelimiter/_sync.py @@ -75,7 +75,3 @@ def __exit__(self, exc_type, exc_val, exc_tb): @property def _timespan(self): return self.calls[-1] - self.calls[0] - - @property - def _remaining(self): - return self.max_calls -self.calls \ No newline at end of file From e39bf7a9b684d24d14def57249eb49e03d6baff5 Mon Sep 17 00:00:00 2001 From: dedoussis Date: Sat, 9 Jun 2018 09:57:57 -0400 Subject: [PATCH 06/17] Value errors for consume attribute --- ratelimiter/_sync.py | 2 ++ tests/test_ratelimiter.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ratelimiter/_sync.py b/ratelimiter/_sync.py index 1213094..d6d5e89 100644 --- a/ratelimiter/_sync.py +++ b/ratelimiter/_sync.py @@ -20,6 +20,8 @@ def __init__(self, max_calls, period=1.0, callback=None, consume=1): raise ValueError('Rate limiting period should be > 0') if max_calls <= 0: raise ValueError('Rate limiting number of calls should be > 0') + if consume < 1: + raise ValueError('Number of calls to consume should be >= 1') # We're using a deque to store the last execution timestamps, not for # its maxlen attribute, but to allow constant time front removal. diff --git a/tests/test_ratelimiter.py b/tests/test_ratelimiter.py index d317172..8fdba6f 100644 --- a/tests/test_ratelimiter.py +++ b/tests/test_ratelimiter.py @@ -52,8 +52,9 @@ def validate_call_times(self, ts, max_calls, period): self.assertGreaterEqual(ts[i + max_calls] - ts[i], period) def test_bad_args(self): - self.assertRaises(ValueError, RateLimiter, -1, self.period) - self.assertRaises(ValueError, RateLimiter, +1, -self.period) + self.assertRaises(ValueError, RateLimiter, -self.max_calls, self.period) + self.assertRaises(ValueError, RateLimiter, self.max_calls, -self.period) + self.assertRaises(ValueError, RateLimiter, self.max_calls, self.period, consume=0) def test_limit_1(self): with Timer() as timer: From 63c8efafe8589b537c1c7cfa359f849f764a892f Mon Sep 17 00:00:00 2001 From: dedoussis Date: Sat, 9 Jun 2018 10:13:38 -0400 Subject: [PATCH 07/17] Consume property getters and setters, with error restrictions --- ratelimiter/_sync.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ratelimiter/_sync.py b/ratelimiter/_sync.py index d6d5e89..94a03c6 100644 --- a/ratelimiter/_sync.py +++ b/ratelimiter/_sync.py @@ -77,3 +77,20 @@ def __exit__(self, exc_type, exc_val, exc_tb): @property def _timespan(self): return self.calls[-1] - self.calls[0] + + @property + def consume(self): + """The consume attribute allows to modify the number of calls + that will be consumed with a single rate limited run.""" + return self._consume + + @consume.setter + def consume(self, value): + if value >= 1: + self._consume = value + else: + raise ValueError('Number of calls to consume should be >= 1') + + @consume.deleter + def consume(self): + raise AttributeError('Consume attribute cannot be dereferenced') From e192b874205d8cdf0fe9de484621933f907460b4 Mon Sep 17 00:00:00 2001 From: dedoussis Date: Sat, 9 Jun 2018 18:00:57 -0400 Subject: [PATCH 08/17] Test case for consume attribute deletion --- tests/test_ratelimiter.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_ratelimiter.py b/tests/test_ratelimiter.py index 8fdba6f..b173893 100644 --- a/tests/test_ratelimiter.py +++ b/tests/test_ratelimiter.py @@ -55,6 +55,7 @@ def test_bad_args(self): self.assertRaises(ValueError, RateLimiter, -self.max_calls, self.period) self.assertRaises(ValueError, RateLimiter, self.max_calls, -self.period) self.assertRaises(ValueError, RateLimiter, self.max_calls, self.period, consume=0) + self.assertRaises(AttributeError, delattr, RateLimiter, 'consume') def test_limit_1(self): with Timer() as timer: From 1908b01c3e2214ea3863832a7d6430f096bc1f44 Mon Sep 17 00:00:00 2001 From: dedoussis Date: Sun, 10 Jun 2018 17:03:43 +0100 Subject: [PATCH 09/17] Decorator unit tests for static consume --- ratelimiter/_sync.py | 4 ++-- tests/test_ratelimiter.py | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ratelimiter/_sync.py b/ratelimiter/_sync.py index 94a03c6..8cd4195 100644 --- a/ratelimiter/_sync.py +++ b/ratelimiter/_sync.py @@ -86,10 +86,10 @@ def consume(self): @consume.setter def consume(self, value): - if value >= 1: + if 1 <= value <= self.max_calls: self._consume = value else: - raise ValueError('Number of calls to consume should be >= 1') + raise ValueError('Number of calls to consume should be 1 <= consume value <= max calls') @consume.deleter def consume(self): diff --git a/tests/test_ratelimiter.py b/tests/test_ratelimiter.py index b173893..5de78d7 100644 --- a/tests/test_ratelimiter.py +++ b/tests/test_ratelimiter.py @@ -120,7 +120,7 @@ def f(): def test_decorator_2(self): @RateLimiter(self.max_calls, self.period) def f(): - f.calls.append(time.time()) + f.calls.extend([time.time()]) f.calls = [] [f() for i in range(3 * self.max_calls)] @@ -128,6 +128,28 @@ def f(): self.assertEqual(len(f.calls), 3 * self.max_calls) self.validate_call_times(f.calls, self.max_calls, self.period) + def test_decorator_3(self): + consume = 2 + @RateLimiter(self.max_calls, self.period, consume=consume) + def f(): + pass + with Timer() as timer: + [f() for i in range(self.max_calls + 1)] + + self.assertGreaterEqual(timer.duration, self.period * consume) + + def test_decorator_4(self): + consume = 3 + @RateLimiter(self.max_calls, self.period, consume=consume) + def f(): + f.calls.extend([time.time()] * consume) + f.calls = [] + + [f() for i in range(3 * self.max_calls)] + + self.assertEqual(len(f.calls), 3 * self.max_calls * consume) + self.validate_call_times(f.calls, self.max_calls, self.period) + def test_random(self): for _ in range(10): calls = [] From 3ecf81f4c6cd8e771c60a147b1bd63f48134abf3 Mon Sep 17 00:00:00 2001 From: dedoussis Date: Sun, 10 Jun 2018 21:24:11 +0100 Subject: [PATCH 10/17] Varying but restricted consume unit tests --- tests/test_ratelimiter.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/tests/test_ratelimiter.py b/tests/test_ratelimiter.py index 5de78d7..208fd46 100644 --- a/tests/test_ratelimiter.py +++ b/tests/test_ratelimiter.py @@ -60,7 +60,7 @@ def test_bad_args(self): def test_limit_1(self): with Timer() as timer: obj = RateLimiter(self.max_calls, self.period) - for i in range(self.max_calls + 1): + for _ in range(self.max_calls + 1): with obj: # After the 'self.max_calls' iteration the execution # inside the context manager should be blocked @@ -75,7 +75,7 @@ def test_limit_1(self): def test_limit_2(self): calls = [] obj = RateLimiter(self.max_calls, self.period) - for i in range(3 * self.max_calls): + for _ in range(3 * self.max_calls): with obj: calls.extend([time.time()] * obj.consume) @@ -85,7 +85,7 @@ def test_limit_2(self): def test_limit_3(self): with Timer() as timer: obj = RateLimiter(self.max_calls, self.period, consume=2) - for i in range(self.max_calls+1): + for _ in range(self.max_calls+1): with obj: pass @@ -94,13 +94,24 @@ def test_limit_3(self): def test_limit_4(self): calls = [] obj = RateLimiter(self.max_calls, self.period, consume=3) - for i in range(3 * self.max_calls): + for _ in range(3 * self.max_calls): with obj: calls.extend([time.time()] * obj.consume) self.assertEqual(len(calls), 3 * self.max_calls * obj.consume) self.validate_call_times(calls, self.max_calls, self.period) + def test_limit_5(self): + calls = [] + obj = RateLimiter(self.max_calls, self.period) + while obj.consume < self.max_calls: + with obj: + calls.extend([time.time()] * obj.consume) + obj.consume += 1 + + self.assertEqual(len(calls), sum(range(self.max_calls))) + self.validate_call_times(calls, self.max_calls, self.period) + def test_decorator_1(self): @RateLimiter(self.max_calls, self.period) def f(): @@ -109,7 +120,7 @@ def f(): pass with Timer() as timer: - [f() for i in range(self.max_calls + 1)] + [f() for _ in range(self.max_calls + 1)] # The sum of the time in the iterations without the rate limit blocking # is way lower than 'self.period'. If the duration of the all @@ -123,7 +134,7 @@ def f(): f.calls.extend([time.time()]) f.calls = [] - [f() for i in range(3 * self.max_calls)] + [f() for _ in range(3 * self.max_calls)] self.assertEqual(len(f.calls), 3 * self.max_calls) self.validate_call_times(f.calls, self.max_calls, self.period) @@ -134,7 +145,7 @@ def test_decorator_3(self): def f(): pass with Timer() as timer: - [f() for i in range(self.max_calls + 1)] + [f() for _ in range(self.max_calls + 1)] self.assertGreaterEqual(timer.duration, self.period * consume) @@ -145,7 +156,7 @@ def f(): f.calls.extend([time.time()] * consume) f.calls = [] - [f() for i in range(3 * self.max_calls)] + [f() for _ in range(3 * self.max_calls)] self.assertEqual(len(f.calls), 3 * self.max_calls * consume) self.validate_call_times(f.calls, self.max_calls, self.period) From f29a04a8f80fa9bb7b840dc1b2c97e00e7c153dc Mon Sep 17 00:00:00 2001 From: dedoussis Date: Wed, 13 Jun 2018 22:31:19 +0100 Subject: [PATCH 11/17] Algorithm finalisation --- ratelimiter/_sync.py | 10 +- tests/calls_array.log | 208 ++++++++++++++++++++++++++++++++++ tests/calls_array_new.log | 205 +++++++++++++++++++++++++++++++++ tests/calls_array_new_new.log | 205 +++++++++++++++++++++++++++++++++ tests/test_ratelimiter.py | 18 ++- 5 files changed, 636 insertions(+), 10 deletions(-) create mode 100644 tests/calls_array.log create mode 100644 tests/calls_array_new.log create mode 100644 tests/calls_array_new_new.log diff --git a/ratelimiter/_sync.py b/ratelimiter/_sync.py index 8cd4195..d9c7ac2 100644 --- a/ratelimiter/_sync.py +++ b/ratelimiter/_sync.py @@ -4,6 +4,7 @@ import functools import threading import collections +import math class RateLimiter(object): @@ -53,7 +54,8 @@ def __enter__(self): # period. For this, we store the last timestamps of each call and run # the rate verification upon each __enter__ call. if len(self.calls) + self.consume > self.max_calls: - until = time.time() + self.period - self._timespan + cycles = math.ceil(self.consume / self.max_calls) + until = time.time() + cycles * self.period - self._timespan if self.callback: t = threading.Thread(target=self.callback, args=(until,)) t.daemon = True @@ -76,7 +78,7 @@ def __exit__(self, exc_type, exc_val, exc_tb): @property def _timespan(self): - return self.calls[-1] - self.calls[0] + return self.calls[-1] - self.calls[0] if self.calls else 0 @property def consume(self): @@ -86,10 +88,10 @@ def consume(self): @consume.setter def consume(self, value): - if 1 <= value <= self.max_calls: + if value >= 1: self._consume = value else: - raise ValueError('Number of calls to consume should be 1 <= consume value <= max calls') + raise ValueError('Number of calls to consume should be >= 1') @consume.deleter def consume(self): diff --git a/tests/calls_array.log b/tests/calls_array.log new file mode 100644 index 0000000..f947bb9 --- /dev/null +++ b/tests/calls_array.log @@ -0,0 +1,208 @@ +[ 1528672465.918541, + + 1528672465.918553, + 1528672465.918553, + + 1528672465.918561, + 1528672465.918561, + 1528672465.918561, +1 + 1528672465.929727, + 1528672465.929727, + 1528672465.929727, + 1528672465.929727, + + 1528672465.929852, + 1528672465.929852, + 1528672465.929852, + 1528672465.929852, + 1528672465.929852, +1 + 1528672465.9423559, + 1528672465.9423559, + 1528672465.9423559, + 1528672465.9423559, + 1528672465.9423559, + 1528672465.9423559, +1 + 1528672465.953177, + 1528672465.953177, + 1528672465.953177, + 1528672465.953177, + 1528672465.953177, + 1528672465.953177, + 1528672465.953177, +1 + 1528672465.965902, + 1528672465.965902, + 1528672465.965902, + 1528672465.965902, + 1528672465.965902, + 1528672465.965902, + 1528672465.965902, + 1528672465.965902, +1 + 1528672465.97682, + 1528672465.97682, + 1528672465.97682, + 1528672465.97682, + 1528672465.97682, + 1528672465.97682, + 1528672465.97682, + 1528672465.97682, + 1528672465.97682, +1 + 1528672465.989581, + 1528672465.989581, + 1528672465.989581, + 1528672465.989581, + 1528672465.989581, + 1528672465.989581, + 1528672465.989581, + 1528672465.989581, + 1528672465.989581, + 1528672465.989581, +1 + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 1528672466.01481, + 2 + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, + 1528672466.040031, +2 + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, + 1528672466.06527, +2 + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + 1528672466.090539, + + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + 1528672466.1114838, + + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + 1528672466.142797, + + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + 1528672466.178136, + + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + 1528672466.208367, + + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558, + 1528672466.243558] \ No newline at end of file diff --git a/tests/calls_array_new.log b/tests/calls_array_new.log new file mode 100644 index 0000000..c4a0e13 --- /dev/null +++ b/tests/calls_array_new.log @@ -0,0 +1,205 @@ +[ 1528758097.503532, + 1528758097.503538, + 1528758097.503538, + 1528758097.503542, + 1528758097.503542, + 1528758097.503542, + 1528758097.503545, + 1528758097.503545, + 1528758097.503545, + 1528758097.503545, +1 + 1528758097.515908, + 1528758097.515908, + 1528758097.515908, + 1528758097.515908, + 1528758097.515908, +1 + 1528758097.528503, + 1528758097.528503, + 1528758097.528503, + 1528758097.528503, + 1528758097.528503, + 1528758097.528503, +1 + 1528758097.540742, + 1528758097.540742, + 1528758097.540742, + 1528758097.540742, + 1528758097.540742, + 1528758097.540742, + 1528758097.540742, +1 + 1528758097.551475, + 1528758097.551475, + 1528758097.551475, + 1528758097.551475, + 1528758097.551475, + 1528758097.551475, + 1528758097.551475, + 1528758097.551475, +1 + 1528758097.562289, + 1528758097.562289, + 1528758097.562289, + 1528758097.562289, + 1528758097.562289, + 1528758097.562289, + 1528758097.562289, + 1528758097.562289, + 1528758097.562289, +1 + 1528758097.57264, + 1528758097.57264, + 1528758097.57264, + 1528758097.57264, + 1528758097.57264, + 1528758097.57264, + 1528758097.57264, + 1528758097.57264, + 1528758097.57264, + 1528758097.57264, +1 + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, + 1528758097.5842261, +1 + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, + 1528758097.595617, +1 + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, + 1528758097.607198, +1 + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, + 1528758097.619914, +1 + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, + 1528758097.631978, +2 + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, + 1528758097.655241, +2 + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, + 1528758097.679192, +2 + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, + 1528758097.7023711, +2 + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878, + 1528758097.725878] \ No newline at end of file diff --git a/tests/calls_array_new_new.log b/tests/calls_array_new_new.log new file mode 100644 index 0000000..0df1329 --- /dev/null +++ b/tests/calls_array_new_new.log @@ -0,0 +1,205 @@ +[ 1528760644.827745, + 1528760644.827753, + 1528760644.827753, + 1528760644.827756, + 1528760644.827756, + 1528760644.827756, + 1528760644.8277602, + 1528760644.8277602, + 1528760644.8277602, + 1528760644.8277602, +1 + 1528760644.8394911, + 1528760644.8394911, + 1528760644.8394911, + 1528760644.8394911, + 1528760644.8394911, +1 + 1528760644.851112, + 1528760644.851112, + 1528760644.851112, + 1528760644.851112, + 1528760644.851112, + 1528760644.851112, +1 + 1528760644.862779, + 1528760644.862779, + 1528760644.862779, + 1528760644.862779, + 1528760644.862779, + 1528760644.862779, + 1528760644.862779, +1 + 1528760644.8743732, + 1528760644.8743732, + 1528760644.8743732, + 1528760644.8743732, + 1528760644.8743732, + 1528760644.8743732, + 1528760644.8743732, + 1528760644.8743732, +1 + 1528760644.885939, + 1528760644.885939, + 1528760644.885939, + 1528760644.885939, + 1528760644.885939, + 1528760644.885939, + 1528760644.885939, + 1528760644.885939, + 1528760644.885939, +1 + 1528760644.8975549, + 1528760644.8975549, + 1528760644.8975549, + 1528760644.8975549, + 1528760644.8975549, + 1528760644.8975549, + 1528760644.8975549, + 1528760644.8975549, + 1528760644.8975549, + 1528760644.8975549, +2 + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, + 1528760644.918436, +2 + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, + 1528760644.9411972, +2 + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, + 1528760644.965787, +2 + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, + 1528760644.990529, +2 + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, + 1528760645.013729, +3 + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, + 1528760645.0485451, +3 + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, + 1528760645.083415, +3 + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, + 1528760645.115642, +3 + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768, + 1528760645.1505768] \ No newline at end of file diff --git a/tests/test_ratelimiter.py b/tests/test_ratelimiter.py index 208fd46..58df8e0 100644 --- a/tests/test_ratelimiter.py +++ b/tests/test_ratelimiter.py @@ -71,6 +71,7 @@ def test_limit_1(self): # iterations is greater or equal to the 'self.period' then blocking # and sleeping after the 'self.max_calls' iteration has been occured. self.assertGreaterEqual(timer.duration, self.period) + self.assertLessEqual(timer.duration, 2 * self.period) def test_limit_2(self): calls = [] @@ -90,6 +91,7 @@ def test_limit_3(self): pass self.assertGreaterEqual(timer.duration, self.period * obj.consume) + self.assertLessEqual(timer.duration, self.period * (obj.consume + 1)) def test_limit_4(self): calls = [] @@ -104,13 +106,15 @@ def test_limit_4(self): def test_limit_5(self): calls = [] obj = RateLimiter(self.max_calls, self.period) - while obj.consume < self.max_calls: - with obj: - calls.extend([time.time()] * obj.consume) - obj.consume += 1 + with Timer() as timer: + while obj.consume < 2 * self.max_calls: + with obj: + calls.extend([time.time()] * obj.consume) + obj.consume += 1 - self.assertEqual(len(calls), sum(range(self.max_calls))) - self.validate_call_times(calls, self.max_calls, self.period) + self.assertEqual(len(calls), sum(range(2 * self.max_calls))) + self.assertGreaterEqual(timer.duration, self.period * math.ceil(len(calls) / self.max_calls)) + self.validate_call_times(calls, 2 * self.max_calls - 1, 2 * self.period) def test_decorator_1(self): @RateLimiter(self.max_calls, self.period) @@ -127,6 +131,7 @@ def f(): # iterations is greater or equal to the 'self.period' then blocking # and sleeping after the 'self.max_calls' iteration has been occured. self.assertGreaterEqual(timer.duration, self.period) + self.assertLessEqual(timer.duration, 2 * self.period) def test_decorator_2(self): @RateLimiter(self.max_calls, self.period) @@ -148,6 +153,7 @@ def f(): [f() for _ in range(self.max_calls + 1)] self.assertGreaterEqual(timer.duration, self.period * consume) + self.assertLessEqual(timer.duration, self.period * (consume + 1)) def test_decorator_4(self): consume = 3 From bc5d597ad1e2e57461c48af6f1fc4eddadccfb54 Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Wed, 13 Jun 2018 22:36:40 +0100 Subject: [PATCH 12/17] Delete test logs 1 --- tests/calls_array_new.log | 205 -------------------------------------- 1 file changed, 205 deletions(-) delete mode 100644 tests/calls_array_new.log diff --git a/tests/calls_array_new.log b/tests/calls_array_new.log deleted file mode 100644 index c4a0e13..0000000 --- a/tests/calls_array_new.log +++ /dev/null @@ -1,205 +0,0 @@ -[ 1528758097.503532, - 1528758097.503538, - 1528758097.503538, - 1528758097.503542, - 1528758097.503542, - 1528758097.503542, - 1528758097.503545, - 1528758097.503545, - 1528758097.503545, - 1528758097.503545, -1 - 1528758097.515908, - 1528758097.515908, - 1528758097.515908, - 1528758097.515908, - 1528758097.515908, -1 - 1528758097.528503, - 1528758097.528503, - 1528758097.528503, - 1528758097.528503, - 1528758097.528503, - 1528758097.528503, -1 - 1528758097.540742, - 1528758097.540742, - 1528758097.540742, - 1528758097.540742, - 1528758097.540742, - 1528758097.540742, - 1528758097.540742, -1 - 1528758097.551475, - 1528758097.551475, - 1528758097.551475, - 1528758097.551475, - 1528758097.551475, - 1528758097.551475, - 1528758097.551475, - 1528758097.551475, -1 - 1528758097.562289, - 1528758097.562289, - 1528758097.562289, - 1528758097.562289, - 1528758097.562289, - 1528758097.562289, - 1528758097.562289, - 1528758097.562289, - 1528758097.562289, -1 - 1528758097.57264, - 1528758097.57264, - 1528758097.57264, - 1528758097.57264, - 1528758097.57264, - 1528758097.57264, - 1528758097.57264, - 1528758097.57264, - 1528758097.57264, - 1528758097.57264, -1 - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, - 1528758097.5842261, -1 - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, - 1528758097.595617, -1 - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, - 1528758097.607198, -1 - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, - 1528758097.619914, -1 - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, - 1528758097.631978, -2 - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, - 1528758097.655241, -2 - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, - 1528758097.679192, -2 - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, - 1528758097.7023711, -2 - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878, - 1528758097.725878] \ No newline at end of file From f278bc44bbb1890d395da8d0a538041d841ca55d Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Wed, 13 Jun 2018 22:36:56 +0100 Subject: [PATCH 13/17] Delete test logs 2 --- tests/calls_array.log | 208 ------------------------------------------ 1 file changed, 208 deletions(-) delete mode 100644 tests/calls_array.log diff --git a/tests/calls_array.log b/tests/calls_array.log deleted file mode 100644 index f947bb9..0000000 --- a/tests/calls_array.log +++ /dev/null @@ -1,208 +0,0 @@ -[ 1528672465.918541, - - 1528672465.918553, - 1528672465.918553, - - 1528672465.918561, - 1528672465.918561, - 1528672465.918561, -1 - 1528672465.929727, - 1528672465.929727, - 1528672465.929727, - 1528672465.929727, - - 1528672465.929852, - 1528672465.929852, - 1528672465.929852, - 1528672465.929852, - 1528672465.929852, -1 - 1528672465.9423559, - 1528672465.9423559, - 1528672465.9423559, - 1528672465.9423559, - 1528672465.9423559, - 1528672465.9423559, -1 - 1528672465.953177, - 1528672465.953177, - 1528672465.953177, - 1528672465.953177, - 1528672465.953177, - 1528672465.953177, - 1528672465.953177, -1 - 1528672465.965902, - 1528672465.965902, - 1528672465.965902, - 1528672465.965902, - 1528672465.965902, - 1528672465.965902, - 1528672465.965902, - 1528672465.965902, -1 - 1528672465.97682, - 1528672465.97682, - 1528672465.97682, - 1528672465.97682, - 1528672465.97682, - 1528672465.97682, - 1528672465.97682, - 1528672465.97682, - 1528672465.97682, -1 - 1528672465.989581, - 1528672465.989581, - 1528672465.989581, - 1528672465.989581, - 1528672465.989581, - 1528672465.989581, - 1528672465.989581, - 1528672465.989581, - 1528672465.989581, - 1528672465.989581, -1 - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 1528672466.01481, - 2 - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, - 1528672466.040031, -2 - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, - 1528672466.06527, -2 - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - 1528672466.090539, - - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - 1528672466.1114838, - - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - 1528672466.142797, - - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - 1528672466.178136, - - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - 1528672466.208367, - - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558, - 1528672466.243558] \ No newline at end of file From f710ddb67dcbf3476bbb60edf65dae0a59a99b61 Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Wed, 13 Jun 2018 22:37:13 +0100 Subject: [PATCH 14/17] Delete test logs 3 --- tests/calls_array_new_new.log | 205 ---------------------------------- 1 file changed, 205 deletions(-) delete mode 100644 tests/calls_array_new_new.log diff --git a/tests/calls_array_new_new.log b/tests/calls_array_new_new.log deleted file mode 100644 index 0df1329..0000000 --- a/tests/calls_array_new_new.log +++ /dev/null @@ -1,205 +0,0 @@ -[ 1528760644.827745, - 1528760644.827753, - 1528760644.827753, - 1528760644.827756, - 1528760644.827756, - 1528760644.827756, - 1528760644.8277602, - 1528760644.8277602, - 1528760644.8277602, - 1528760644.8277602, -1 - 1528760644.8394911, - 1528760644.8394911, - 1528760644.8394911, - 1528760644.8394911, - 1528760644.8394911, -1 - 1528760644.851112, - 1528760644.851112, - 1528760644.851112, - 1528760644.851112, - 1528760644.851112, - 1528760644.851112, -1 - 1528760644.862779, - 1528760644.862779, - 1528760644.862779, - 1528760644.862779, - 1528760644.862779, - 1528760644.862779, - 1528760644.862779, -1 - 1528760644.8743732, - 1528760644.8743732, - 1528760644.8743732, - 1528760644.8743732, - 1528760644.8743732, - 1528760644.8743732, - 1528760644.8743732, - 1528760644.8743732, -1 - 1528760644.885939, - 1528760644.885939, - 1528760644.885939, - 1528760644.885939, - 1528760644.885939, - 1528760644.885939, - 1528760644.885939, - 1528760644.885939, - 1528760644.885939, -1 - 1528760644.8975549, - 1528760644.8975549, - 1528760644.8975549, - 1528760644.8975549, - 1528760644.8975549, - 1528760644.8975549, - 1528760644.8975549, - 1528760644.8975549, - 1528760644.8975549, - 1528760644.8975549, -2 - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, - 1528760644.918436, -2 - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, - 1528760644.9411972, -2 - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, - 1528760644.965787, -2 - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, - 1528760644.990529, -2 - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, - 1528760645.013729, -3 - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, - 1528760645.0485451, -3 - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, - 1528760645.083415, -3 - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, - 1528760645.115642, -3 - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768, - 1528760645.1505768] \ No newline at end of file From bb0013056e1b0438bafb74e026c61f683935da28 Mon Sep 17 00:00:00 2001 From: Dimitrios Dedoussis Date: Wed, 13 Jun 2018 22:39:56 +0100 Subject: [PATCH 15/17] Delete chache --- .cache/v/cache/lastfailed | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .cache/v/cache/lastfailed diff --git a/.cache/v/cache/lastfailed b/.cache/v/cache/lastfailed deleted file mode 100644 index 9e26dfe..0000000 --- a/.cache/v/cache/lastfailed +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file From 0219a41286fecc798f2704b642750bcf43d2b41e Mon Sep 17 00:00:00 2001 From: dedoussis Date: Wed, 13 Jun 2018 23:29:34 +0100 Subject: [PATCH 16/17] Update README --- README.rst | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.rst b/README.rst index 5fc9551..c8b69ec 100644 --- a/README.rst +++ b/README.rst @@ -40,6 +40,41 @@ Context Manager with rate_limiter: do_something() +Consume +~~~~~~~ + +For the case of limiting the rate of a specific unit metric rather than the execution +of an operation as a whole, the ``ratelimiter`` module provides the consume attribute. +This allows to limit our execution at a rate of bytes/s instead of block-operations/s. +Note that bytes/s is just an example, similar to any other unit such as database +capacitiy per second etc. The consume attribute can be modified during execution to account +for varying unit sizes. + +.. code:: python + + from ratelimiter import RateLimiter + + rate_limiter = RateLimiter(max_calls=10, period=1, consume=2) + # Time duration of the following operation will be doubled. + for i in range(100): + with rate_limiter: + do_something() + +.. code:: python + + from ratelimiter import RateLimiter + + rate_limiter = RateLimiter(max_calls=1024, period=1) + # Stream of data + packets = [ bytearray(512), bytearray(2048), bytearray(1024), bytearray(512) ] + + for packet in packets: + with rate_limiter: + send(packet) + # Consume the length of the packet + rate_limiter.consume = packet.length + + Callback ~~~~~~~~ From 5726b8ea97ddbc412a96e38af30ea46f3d379888 Mon Sep 17 00:00:00 2001 From: dedoussis Date: Wed, 13 Jun 2018 23:30:28 +0100 Subject: [PATCH 17/17] Remove dot --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index c8b69ec..43b93d2 100644 --- a/README.rst +++ b/README.rst @@ -55,7 +55,7 @@ for varying unit sizes. from ratelimiter import RateLimiter rate_limiter = RateLimiter(max_calls=10, period=1, consume=2) - # Time duration of the following operation will be doubled. + # Time duration of the following operation will be doubled for i in range(100): with rate_limiter: do_something()