Skip to content

Commit 287123b

Browse files
committed
Add rate limiter with default cold factor
1 parent 4d7df14 commit 287123b

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

  • extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/limiter

extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/limiter/RateLimiter.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,39 @@
8686
* @since 13.0
8787
*/
8888
public abstract class RateLimiter {
89+
public static final double DEFAULT_COLD_FACTOR = 3.0;
90+
91+
/**
92+
* Creates a {@code RateLimiter} with the specified stable throughput, given as "permits per
93+
* second" (commonly referred to as <i>QPS</i>, queries per second), and a <i>warmup period</i>,
94+
* during which the {@code RateLimiter} smoothly ramps up its rate, until it reaches its maximum
95+
* rate at the end of the period (as long as there are enough requests to saturate it). Similarly,
96+
* if the {@code RateLimiter} is left <i>unused</i> for a duration of {@code warmupPeriod}, it
97+
* will gradually return to its "cold" state, i.e. it will go through the same warming up process
98+
* as when it was first created.
99+
*
100+
* <p>The returned {@code RateLimiter} is intended for cases where the resource that actually
101+
* fulfills the requests (e.g., a remote server) needs "warmup" time, rather than being
102+
* immediately accessed at the stable (maximum) rate.
103+
*
104+
* <p>The returned {@code RateLimiter} starts in a "cold" state (i.e. the warmup period will
105+
* follow), and if it is left unused for long enough, it will return to that state.
106+
*
107+
* @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in how many
108+
* permits become available per second
109+
* @param warmupPeriod the duration of the period where the {@code RateLimiter} ramps up its rate,
110+
* before reaching its stable (maximum) rate
111+
* @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero or {@code
112+
* warmupPeriod} is negative
113+
* @since 28.0 (but only since 33.4.0 in the Android flavor)
114+
*/
115+
public static RateLimiter create(
116+
final double permitsPerSecond,
117+
final Duration warmupPeriod
118+
) {
119+
return create(permitsPerSecond, warmupPeriod, DEFAULT_COLD_FACTOR);
120+
}
121+
89122
/**
90123
* Creates a {@code RateLimiter} with the specified stable throughput, given as "permits per
91124
* second" (commonly referred to as <i>QPS</i>, queries per second), and a <i>warmup period</i>,

0 commit comments

Comments
 (0)