Skip to content

Commit

Permalink
Refactor RedisCommon to use new style class.
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Oct 5, 2015
1 parent 7f89a48 commit 14b9739
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions limits/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def check(self):
"""
return True

class RedisCommon:
class RedisInteractor(object):
SCRIPT_MOVING_WINDOW = """
local items = redis.call('lrange', KEYS[1], 0, tonumber(ARGV[2]))
local expiry = tonumber(ARGV[1])
Expand All @@ -234,7 +234,7 @@ class RedisCommon:
return {oldest, a}
"""

def redis_incr(self, connection, key, expiry, elastic_expiry=False):
def incr(self, key, expiry, connection, elastic_expiry=False):
"""
increments the counter for a given rate limit key
Expand All @@ -247,7 +247,7 @@ def redis_incr(self, connection, key, expiry, elastic_expiry=False):
connection.expire(key, expiry)
return value

def redis_get(self, connection, key):
def get(self,key, connection):
"""
:param connection: Redis connection
:param str key: the key to get the counter value for
Expand All @@ -267,14 +267,16 @@ def get_moving_window(self, key, limit, expiry):
)
return window or (timestamp, 0)

def redis_acquire_entry(self, connection, key, limit, expiry, no_add=False):
def acquire_entry(
self, key, limit, expiry, connection, no_add=False
):
"""
:param connection: Redis connection
:param str key: rate limit key to acquire an entry in
:param int limit: amount of entries allowed
:param int expiry: expiry of the entry
:param bool no_add: if False an entry is not actually acquired but instead
serves as a 'check'
:param connection: Redis connection
:return: True/False
"""
timestamp = time.time()
Expand All @@ -291,14 +293,14 @@ def redis_acquire_entry(self, connection, key, limit, expiry, no_add=False):
pipeline.execute()
return True

def redis_get_expiry(self, connection, key):
def get_expiry(self, key, connection=None):
"""
:param connection: Redis connection
:param str key: the key to get the expiry for
:param connection: Redis connection
"""
return int((connection.ttl(key) or 0) + time.time())

def redis_check(self, connection):
def check(self, connection):
"""
:param connection: Redis connection
check if storage is healthy
Expand All @@ -308,7 +310,7 @@ def redis_check(self, connection):
except: # noqa
return False

class RedisStorage(Storage, RedisCommon):
class RedisStorage(RedisInteractor, Storage):
"""
rate limit storage with redis as backend
"""
Expand All @@ -331,7 +333,7 @@ def initialize_storage(self, uri):
if not self.storage.ping():
raise ConfigurationError("unable to connect to redis at %s" % uri) # pragma: no cover
self.lua_moving_window = self.storage.register_script(
RedisCommon.SCRIPT_MOVING_WINDOW
RedisStorage.SCRIPT_MOVING_WINDOW
)
self.lock_impl = self.storage.lock

Expand All @@ -342,13 +344,15 @@ def incr(self, key, expiry, elastic_expiry=False):
:param str key: the key to increment
:param int expiry: amount in seconds for the key to expire in
"""
return self.redis_incr(self.storage, key, expiry, elastic_expiry)
return super(RedisStorage, self).incr(
key, expiry, self.storage, elastic_expiry
)

def get(self, key):
"""
:param str key: the key to get the counter value for
"""
return self.redis_get(self.storage, key)
return super(RedisStorage, self).get(key, self.storage)

def acquire_entry(self, key, limit, expiry, no_add=False):
"""
Expand All @@ -359,22 +363,24 @@ def acquire_entry(self, key, limit, expiry, no_add=False):
serves as a 'check'
:return: True/False
"""
return self.redis_acquire_entry(self.storage, key, limit, expiry, no_add)
return super(RedisStorage, self).acquire_entry(
key, limit, expiry, self.storage, no_add=no_add
)

def get_expiry(self, key):
"""
:param str key: the key to get the expiry for
"""
return self.redis_get_expiry(self.storage, key)
return super(RedisStorage, self).get_expiry(key, self.storage)

def check(self):
"""
check if storage is healthy
"""
return self.redis_check(self.storage)
return super(RedisStorage, self).check(self.storage)


class RedisSentinelStorage(Storage, RedisCommon):
class RedisSentinelStorage(RedisInteractor, Storage):
"""
rate limit storage with redis sentinel as backend
"""
Expand Down Expand Up @@ -408,7 +414,7 @@ def initialize_storage(self):
if not master.ping():
raise ConfigurationError("unable to connect to redis at %s" % self.sentinel) # pragma: no cover
self.lua_moving_window = master.register_script(
RedisCommon.SCRIPT_MOVING_WINDOW
RedisSentinelStorage.SCRIPT_MOVING_WINDOW
)
self.lock_impl = master.lock

Expand All @@ -421,14 +427,16 @@ def incr(self, key, expiry, elastic_expiry=False):
:param int expiry: amount in seconds for the key to expire in
"""
master = self.sentinel.master_for(self.service_name)
return self.redis_incr(master, key, expiry, elastic_expiry)
return super(RedisSentinelStorage, self).incr(
key, expiry, master, elastic_expiry
)

def get(self, key):
"""
:param str key: the key to get the counter value for
"""
slave = self.sentinel.slave_for(self.service_name)
return self.redis_get(slave, key)
return super(RedisSentinelStorage, self).get(key, slave)

def acquire_entry(self, key, limit, expiry, no_add=False):
"""
Expand All @@ -440,21 +448,23 @@ def acquire_entry(self, key, limit, expiry, no_add=False):
:return: True/False
"""
master = self.sentinel.master_for(self.service_name)
return self.redis_acquire_entry(master, key, limit, expiry, no_add)
return super(RedisSentinelStorage, self).acquire_entry(
key, limit, expiry, master, no_add
)

def get_expiry(self, key):
"""
:param str key: the key to get the expiry for
"""
slave = self.sentinel.slave_for(self.service_name)
return self.redis_get_expiry(slave, key)
return super(RedisSentinelStorage, self).get_expiry(key, slave)

def check(self):
"""
check if storage is healthy
"""
slave = self.sentinel.slave_for(self.service_name)
return self.redis_check(slave)
return super(RedisSentinelStorage, self).check(slave)


class MemcachedStorage(Storage):
Expand Down

0 comments on commit 14b9739

Please sign in to comment.