diff --git a/swift/common/memcached.py b/swift/common/memcached.py index e232e404f8..71fa7519dc 100644 --- a/swift/common/memcached.py +++ b/swift/common/memcached.py @@ -190,6 +190,32 @@ class MemcacheRing(object): except Exception, e: self._exception_occurred(server, e) + def decr(self, key, delta=1, timeout=0): + """ + Decrements a key which has a numeric value by delta. + If the key can't be found, it's added as 0. Memcached + will treat data values below 0 as 0 with incr/decr. + + :param key: key + :param delta: amount to subtract to the value of key (or set + as the value if the key is not found) + :param timeout: ttl in memcache + """ + key = md5hash(key) + for (server, fp, sock) in self._get_conns(key): + try: + sock.sendall('decr %s %s\r\n' % (key, delta)) + line = fp.readline().strip().split() + if line[0].upper() == 'NOT_FOUND': + line[0] = '0' + sock.sendall('add %s %d %d %s noreply\r\n%s\r\n' % + (key, 0, timeout, len(line[0]), line[0])) + ret = int(line[0].strip()) + self._return_conn(server, fp, sock) + return ret + except Exception, e: + self._exception_occurred(server, e) + def delete(self, key): """ Deletes a key/value pair from memcache. diff --git a/swift/common/middleware/ratelimit.py b/swift/common/middleware/ratelimit.py index ca0cd6e427..fea11ae811 100644 --- a/swift/common/middleware/ratelimit.py +++ b/swift/common/middleware/ratelimit.py @@ -148,7 +148,7 @@ class RateLimitMiddleware(object): max_sleep_m = self.max_sleep_time_seconds * self.clock_accuracy if max_sleep_m - need_to_sleep_m <= self.clock_accuracy * 0.01: # treat as no-op decrement time - self.memcache_client.incr(key, delta=-time_per_request_m) + self.memcache_client.decr(key, delta=time_per_request_m) raise MaxSleepTimeHit("Max Sleep Time Exceeded: %s" % need_to_sleep_m)

AltStyle によって変換されたページ (->オリジナル) /