diff --git a/include/ucore/rate_limit.h b/include/ucore/rate_limit.h index 51c70b4..e7bc651 100644 --- a/include/ucore/rate_limit.h +++ b/include/ucore/rate_limit.h @@ -34,6 +34,13 @@ * * uc_ratelimit_init(&r,20, 1, time(NULL)); * + * NOTE. The the internals uses integer arithmetic to replenish the tickets, + * this can lead to greater errors estimating the available tickets the smaller + * the number of tickets is in relation to the period. The arithmetic is scaled + * by 1000 currently, so if ticket = 1 and period = 60, we get 1000/60 = 16. This + * should be 16.6667, which is an error of about 3.6%, which means we really just + * rate limit to 1 per 62 seconds. + * * for (;;) { * int fd = accept(..); * if (uc_ratelimit_allow(&r, time(NULL)) { diff --git a/src/rate_limit.c b/src/rate_limit.c index fec4c79..6bebef0 100644 --- a/src/rate_limit.c +++ b/src/rate_limit.c @@ -1,6 +1,6 @@ #include "ucore/rate_limit.h" -#define SCALE_FACTOR 100 +#define SCALE_FACTOR 1000 void uc_ratelimit_init(struct RateLimit *r, long tickets, long period, long current_ts)