~sirn/fanboi2

1a17d8f27dcb221bc0e5d2e4b0e48f8eb6dac2d9 — Sirn Thanabulpong 4 months ago 7f228a2 main v2024.2.1
Tune the scaled rate limit algorithm a bit more
2 files changed, 2 insertions(+), 5 deletions(-)

M fanboi2/services/rate_limiter.py
M fanboi2/tests/test_services_rate_limiter.py
M fanboi2/services/rate_limiter.py => fanboi2/services/rate_limiter.py +2 -2
@@ 63,10 63,10 @@ class RateLimiterService(object):
            if t <= prune_cutoff:
                self.redis_conn.lrem(ts_key, 0, t)
        self.redis_conn.expire(ts_key, self.SCALED_LIMIT_PRUNE_PERIOD)
        count = min(count, threshold)
        count = min(count, threshold - 1)

        r = math.exp(math.log(period / 10) / (threshold - 1))
        t = round(expiration * (r ** (count - 1)))
        t = round(expiration * (r ** count))
        key = self._get_key(**kwargs)
        self.redis_conn.set(key, 1)
        self.redis_conn.expire(key, t)

M fanboi2/tests/test_services_rate_limiter.py => fanboi2/tests/test_services_rate_limiter.py +0 -3
@@ 76,9 76,6 @@ class TestRateLimiterService(unittest.TestCase):
        redis_conn = DummyRedis()
        rate_limiter_svc = self._get_target_class()(redis_conn)
        rate_limiter_svc.limit_for(10, 5, 1800, foo="bar")
        self.assertEqual(rate_limiter_svc.time_left(foo="bar"), 3)

        rate_limiter_svc.limit_for(10, 5, 1800, foo="bar")
        self.assertEqual(rate_limiter_svc.time_left(foo="bar"), 10)

        rate_limiter_svc.limit_for(10, 5, 1800, foo="bar")