示例#1
0
    def get_ratelimitable_key_tuples(self, req_method, account_name,
                                     container_name=None, obj_name=None):
        """
        Returns a list of key (used in memcache), ratelimit tuples. Keys
        should be checked in order.

        :param req_method: HTTP method
        :param account_name: account name from path
        :param container_name: container name from path
        :param obj_name: object name from path
        """
        keys = []
        # COPYs are not limited
        if self.account_ratelimit and \
                account_name and container_name and not obj_name and \
                req_method in ('PUT', 'DELETE'):
            keys.append(("ratelimit/%s" % account_name,
                         self.account_ratelimit))

        if account_name and container_name and obj_name and \
                req_method in ('PUT', 'DELETE', 'POST'):
            container_size = None
            memcache_key = get_container_memcache_key(account_name,
                                                      container_name)
            container_info = self.memcache_client.get(memcache_key)
            if isinstance(container_info, dict):
                container_size = container_info.get('container_size', 0)
                container_rate = self.get_container_maxrate(container_size)
                if container_rate:
                    keys.append(("ratelimit/%s/%s" % (account_name,
                                                      container_name),
                                 container_rate))
        return keys
示例#2
0
 def test_get_ratelimitable_key_tuples(self):
     current_rate = 13
     conf_dict = {'account_ratelimit': current_rate,
                  'container_ratelimit_3': 200}
     fake_memcache = FakeMemcache()
     fake_memcache.store[get_container_memcache_key('a', 'c')] = \
         {'container_size': 5}
     the_app = ratelimit.RateLimitMiddleware(None, conf_dict,
                                             logger=FakeLogger())
     the_app.memcache_client = fake_memcache
     self.assertEquals(len(the_app.get_ratelimitable_key_tuples(
                 'DELETE', 'a', None, None)), 0)
     self.assertEquals(len(the_app.get_ratelimitable_key_tuples(
                 'PUT', 'a', 'c', None)), 1)
     self.assertEquals(len(the_app.get_ratelimitable_key_tuples(
                 'DELETE', 'a', 'c', None)), 1)
     self.assertEquals(len(the_app.get_ratelimitable_key_tuples(
                 'GET', 'a', 'c', 'o')), 0)
     self.assertEquals(len(the_app.get_ratelimitable_key_tuples(
                 'PUT', 'a', 'c', 'o')), 1)
示例#3
0
    def get_ratelimitable_key_tuples(self,
                                     req_method,
                                     account_name,
                                     container_name=None,
                                     obj_name=None):
        """
        Returns a list of key (used in memcache), ratelimit tuples. Keys
        should be checked in order.

        :param req_method: HTTP method
        :param account_name: account name from path
        :param container_name: container name from path
        :param obj_name: object name from path
        """
        keys = []
        # COPYs are not limited
        if self.account_ratelimit and \
                account_name and container_name and not obj_name and \
                req_method in ('PUT', 'DELETE'):
            keys.append(
                ("ratelimit/%s" % account_name, self.account_ratelimit))

        if account_name and container_name and obj_name and \
                req_method in ('PUT', 'DELETE', 'POST'):
            container_size = None
            memcache_key = get_container_memcache_key(account_name,
                                                      container_name)
            container_info = self.memcache_client.get(memcache_key)
            if isinstance(container_info, dict):
                container_size = container_info.get('container_size', 0)
                container_rate = self.get_container_maxrate(container_size)
                if container_rate:
                    keys.append(
                        ("ratelimit/%s/%s" % (account_name, container_name),
                         container_rate))
        return keys