-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Ignore Rate Limit for Whitelisted Clients #27
base: master
Are you sure you want to change the base?
Changes from 3 commits
7b05356
8dd5605
66c009a
0aceb5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -43,7 +43,7 @@ class RateLimit(object): | |||||||||||
This class offers an abstraction of a Rate Limit algorithm implemented on | ||||||||||||
top of Redis >= 2.6.0. | ||||||||||||
""" | ||||||||||||
def __init__(self, resource, client, max_requests, expire=None, redis_pool=REDIS_POOL): | ||||||||||||
def __init__(self, resource, client, max_requests, ignored_clients=None, expire=None, redis_pool=REDIS_POOL): | ||||||||||||
""" | ||||||||||||
Class initialization method checks if the Rate Limit algorithm is | ||||||||||||
actually supported by the installed Redis version and sets some | ||||||||||||
|
@@ -54,10 +54,14 @@ def __init__(self, resource, client, max_requests, expire=None, redis_pool=REDIS | |||||||||||
:param resource: resource identifier string (i.e. ‘user_pictures’) | ||||||||||||
:param client: client identifier string (i.e. ‘192.168.0.10’) | ||||||||||||
:param max_requests: integer (i.e. ‘10’) | ||||||||||||
:param ignored_clients: list of ip addresses (i.e. ['127.0.0.1']) | ||||||||||||
:param expire: seconds to wait before resetting counters (i.e. ‘60’) | ||||||||||||
:param redis_pool: instance of redis.ConnectionPool. | ||||||||||||
Default: ConnectionPool(host='127.0.0.1', port=6379, db=0) | ||||||||||||
""" | ||||||||||||
self.client = client | ||||||||||||
self.ignored_clients = ignored_clients | ||||||||||||
|
||||||||||||
self._redis = Redis(connection_pool=redis_pool) | ||||||||||||
if not self._is_rate_limit_supported(): | ||||||||||||
raise RedisVersionNotSupported() | ||||||||||||
|
@@ -88,7 +92,6 @@ def get_usage(self): | |||||||||||
""" | ||||||||||||
Returns actual resource usage by client. Note that it could be greater | ||||||||||||
than the maximum number of requests set. | ||||||||||||
|
||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line is still removed, could you revert it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you pushed those updates? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry forgot to push changes. Updated. |
||||||||||||
:return: integer: current usage | ||||||||||||
""" | ||||||||||||
return int(self._redis.get(self._rate_limit_key) or 0) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the check should be done here.
Suggested change
There are two reasons for it:
The reason is to avoid edge cases like this:
Or this other one:
What do you think, @italorossi? |
||||||||||||
|
@@ -129,6 +132,9 @@ def increment_usage(self, increment_by=1): | |||||||||||
|
||||||||||||
:return: integer: current usage | ||||||||||||
""" | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the first line here could be: if self.client in self.bypass_clients:
return 0 The same could be done to other methods since they don't even need to query Redis if the client is supposed to be bypassed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||
if self.ignored_clients and self.client in self.ignored_clients: | ||||||||||||
return 0 | ||||||||||||
|
||||||||||||
if increment_by > self._max_requests: | ||||||||||||
raise ValueError('increment_by {increment_by} overflows ' | ||||||||||||
'max_requests of {max_requests}' | ||||||||||||
|
@@ -172,17 +178,19 @@ def _reset(self): | |||||||||||
|
||||||||||||
|
||||||||||||
class RateLimiter(object): | ||||||||||||
def __init__(self, resource, max_requests, expire=None, redis_pool=REDIS_POOL): | ||||||||||||
def __init__(self, resource, max_requests, ignored_clients=None, expire=None, redis_pool=REDIS_POOL): | ||||||||||||
""" | ||||||||||||
Rate limit factory. Checks if RateLimit is supported when limit is called. | ||||||||||||
:param resource: resource identifier string (i.e. ‘user_pictures’) | ||||||||||||
:param max_requests: integer (i.e. ‘10’) | ||||||||||||
:param ignored_clients: list of ip addresses (i.e. ['127.0.0.1']) | ||||||||||||
:param expire: seconds to wait before resetting counters (i.e. ‘60’) | ||||||||||||
:param redis_pool: instance of redis.ConnectionPool. | ||||||||||||
Default: ConnectionPool(host='127.0.0.1', port=6379, db=0) | ||||||||||||
""" | ||||||||||||
self.resource = resource | ||||||||||||
self.max_requests = max_requests | ||||||||||||
self.ignored_clients = ignored_clients | ||||||||||||
self.expire = expire | ||||||||||||
self.redis_pool = redis_pool | ||||||||||||
|
||||||||||||
|
@@ -194,6 +202,7 @@ def limit(self, client): | |||||||||||
resource=self.resource, | ||||||||||||
client=client, | ||||||||||||
max_requests=self.max_requests, | ||||||||||||
ignored_clients=self.ignored_clients, | ||||||||||||
expire=self.expire, | ||||||||||||
redis_pool=self.redis_pool, | ||||||||||||
) | ||||||||||||
) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, revert this change leaving a blank line at the end of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've removed a bunch of blank lines from docstrings. Could you revert it, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes reverted.