Exemplo n.º 1
0
 def test_redis_sentinel_reset(self):
     storage = RedisSentinelStorage("redis+sentinel://localhost:26379", service_name="localhost-redis-sentinel")
     limiter = FixedWindowRateLimiter(storage)
     for i in range(0, 10000):
         rate = RateLimitItemPerMinute(i)
         limiter.hit(rate)
     self.assertEqual(storage.reset(), 10000)
Exemplo n.º 2
0
 def test_redis_sentinel_reset(self):
     storage = RedisSentinelStorage("redis+sentinel://localhost:26379",
                                    service_name="localhost-redis-sentinel")
     limiter = FixedWindowRateLimiter(storage)
     for i in range(0, 10000):
         rate = RateLimitItemPerMinute(i)
         limiter.hit(rate)
     self.assertEqual(storage.reset(), 10000)
Exemplo n.º 3
0
 def setUp(self):
     self.storage_url = 'redis+sentinel://localhost:26379'
     self.service_name = 'localhost-redis-sentinel'
     self.storage = RedisSentinelStorage(self.storage_url,
                                         service_name=self.service_name)
     redis.sentinel.Sentinel([("localhost", 26379)
                              ]).master_for(self.service_name).flushall()
Exemplo n.º 4
0
    def test_large_dataset_redis_sentinel_moving_window_expiry(self):
        storage = RedisSentinelStorage("redis+sentinel://localhost:26379",
                                       service_name="localhost-redis-sentinel")
        limiter = MovingWindowRateLimiter(storage)
        limit = RateLimitItemPerSecond(1000)
        keys_start = storage.sentinel.slave_for(
            "localhost-redis-sentinel").keys("%s/*" % limit.namespace)
        # 100 routes
        fake_routes = [uuid4().hex for _ in range(0, 100)]
        # go as fast as possible in 2 seconds.
        start = time.time()

        def smack(e):
            while not e.is_set():
                self.assertTrue(limiter.hit(limit, random.choice(fake_routes)))

        events = [threading.Event() for _ in range(0, 100)]
        threads = [threading.Thread(target=smack, args=(e, )) for e in events]
        [k.start() for k in threads]
        while time.time() - start < 2:
            time.sleep(0.1)
        [k.set() for k in events]
        time.sleep(2)
        self.assertTrue(
            storage.sentinel.slave_for("localhost-redis-sentinel").keys(
                "%s/*" % limit.namespace) == [])
Exemplo n.º 5
0
 def test_fixed_window_with_elastic_expiry_redis_sentinel(self):
     storage = RedisSentinelStorage("redis+sentinel://localhost:26379",
                                    service_name="localhost-redis-sentinel")
     limiter = FixedWindowElasticExpiryRateLimiter(storage)
     limit = RateLimitItemPerSecond(10, 2)
     self.assertTrue(all([limiter.hit(limit) for _ in range(0, 10)]))
     time.sleep(1)
     self.assertFalse(limiter.hit(limit))
     time.sleep(1)
     self.assertFalse(limiter.hit(limit))
     self.assertEqual(limiter.get_window_stats(limit)[1], 0)
Exemplo n.º 6
0
 def test_redis_sentinel(self):
     storage = RedisSentinelStorage("redis+sentinel://localhost:26379",
                                    service_name="localhost-redis-sentinel")
     limiter = FixedWindowRateLimiter(storage)
     per_min = RateLimitItemPerSecond(10)
     start = time.time()
     count = 0
     while time.time() - start < 0.5 and count < 10:
         self.assertTrue(limiter.hit(per_min))
         count += 1
     self.assertFalse(limiter.hit(per_min))
     while time.time() - start <= 1:
         time.sleep(0.1)
     self.assertTrue(limiter.hit(per_min))