From 287123b0fd2911e58180de9edea9ee7a5b8cc50b Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Tue, 11 Feb 2025 21:49:48 +0100 Subject: [PATCH] Add rate limiter with default cold factor --- .../ratelimiting/limiter/RateLimiter.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/limiter/RateLimiter.java b/extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/limiter/RateLimiter.java index eef98fb79..4ad8a8221 100644 --- a/extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/limiter/RateLimiter.java +++ b/extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/limiter/RateLimiter.java @@ -86,6 +86,39 @@ import java.util.concurrent.TimeUnit; * @since 13.0 */ public abstract class RateLimiter { + public static final double DEFAULT_COLD_FACTOR = 3.0; + + /** + * Creates a {@code RateLimiter} with the specified stable throughput, given as "permits per + * second" (commonly referred to as QPS, queries per second), and a warmup period, + * during which the {@code RateLimiter} smoothly ramps up its rate, until it reaches its maximum + * rate at the end of the period (as long as there are enough requests to saturate it). Similarly, + * if the {@code RateLimiter} is left unused for a duration of {@code warmupPeriod}, it + * will gradually return to its "cold" state, i.e. it will go through the same warming up process + * as when it was first created. + * + *
The returned {@code RateLimiter} is intended for cases where the resource that actually + * fulfills the requests (e.g., a remote server) needs "warmup" time, rather than being + * immediately accessed at the stable (maximum) rate. + * + *
The returned {@code RateLimiter} starts in a "cold" state (i.e. the warmup period will + * follow), and if it is left unused for long enough, it will return to that state. + * + * @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in how many + * permits become available per second + * @param warmupPeriod the duration of the period where the {@code RateLimiter} ramps up its rate, + * before reaching its stable (maximum) rate + * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero or {@code + * warmupPeriod} is negative + * @since 28.0 (but only since 33.4.0 in the Android flavor) + */ + public static RateLimiter create( + final double permitsPerSecond, + final Duration warmupPeriod + ) { + return create(permitsPerSecond, warmupPeriod, DEFAULT_COLD_FACTOR); + } + /** * Creates a {@code RateLimiter} with the specified stable throughput, given as "permits per * second" (commonly referred to as QPS, queries per second), and a warmup period,