Skip to content

Commit

Permalink
Remove non standard memcached args from calls if not available.
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Mar 20, 2015
1 parent 3e8ec0e commit f504de5
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions limits/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
from abc import abstractmethod, ABCMeta
import inspect

from six.moves import urllib

Expand Down Expand Up @@ -343,6 +344,13 @@ def get_client(self, module, hosts):
"""
return module.Client(*hosts)

def call_memcached_func(self, func, *args, **kwargs):
if 'noreply' in kwargs:
if 'noreply' not in inspect.getargspec(func).args:
kwargs.pop('noreply')
return func(*args, **kwargs)


@property
def storage(self):
"""
Expand All @@ -367,21 +375,21 @@ def incr(self, key, expiry, elastic_expiry=False):
:param bool elastic_expiry: whether to keep extending the rate limit
window every hit.
"""
if not self.storage.add(key, 1, expiry, noreply=False):
if not self.call_memcached_func(self.storage.add, key, 1, expiry, noreply=False):
if elastic_expiry:
value, cas = self.storage.gets(key)
retry = 0
while (
not self.storage.cas(key, int(value or 0)+1, cas, expiry)
not self.call_memcached_func(self.storage.cas, key, int(value or 0)+1, cas, expiry)
and retry < self.MAX_CAS_RETRIES
):
value, cas = self.storage.gets(key)
retry += 1
self.storage.set(key + "/expires", expiry + time.time(), expire=expiry, noreply=False)
self.call_memcached_func(self.storage.set, key + "/expires", expiry + time.time(), expire=expiry, noreply=False)
return int(value or 0) + 1
else:
return self.storage.incr(key, 1)
self.storage.set(key + "/expires", expiry + time.time(), expire=expiry, noreply=False)
self.call_memcached_func(self.storage.set, key + "/expires", expiry + time.time(), expire=expiry, noreply=False)
return 1

def get_expiry(self, key):
Expand Down

0 comments on commit f504de5

Please sign in to comment.