11package org .schabi .newpipe .downloader .ratelimiting ;
22
3+ import org .schabi .newpipe .downloader .ratelimiting .limiter .RateLimiter ;
4+
35import java .io .IOException ;
46import java .net .ProtocolException ;
57import java .time .Duration ;
1214import okhttp3 .Response ;
1315
1416public class RateLimitedClientWrapper {
17+ private static final boolean DEBUG_PRINT =
18+ "1" .equals (System .getProperty ("rateLimitClientDebugPrint" ,
19+ System .getenv ("RATE_LIMIT_CLIENT_DEBUG_PRINT" )));
1520
1621 private static final int REQUEST_RATE_LIMITED_WAIT_MS = 5_000 ;
1722 private static final Map <Predicate <String >, RateLimiter > FORCED_RATE_LIMITERS = Map .ofEntries (
@@ -43,8 +48,10 @@ public Response executeRequestWithLimit(final Request request) throws IOExceptio
4348 for (int tries = 1 ; tries <= 3 ; tries ++) {
4449 try {
4550 final double rateLimitedSec = getRateLimiterFor (request ).acquire ();
46- System .out .println (
47- "[RATE-LIMIT] Waited " + rateLimitedSec + "s for " + request .url ());
51+ if (DEBUG_PRINT ) {
52+ System .out .println (
53+ "[RATE-LIMIT] Waited " + rateLimitedSec + "s for " + request .url ());
54+ }
4855
4956 final Response response = client .newCall (request ).execute ();
5057 if (response .code () != 429 ) { // 429 = Too many requests
@@ -59,8 +66,10 @@ public Response executeRequestWithLimit(final Request request) throws IOExceptio
5966 }
6067
6168 final int waitMs = REQUEST_RATE_LIMITED_WAIT_MS * tries ;
62- System .out .println (
63- "[TOO-MANY-REQUESTS] Waiting " + waitMs + "ms for " + request .url ());
69+ if (DEBUG_PRINT ) {
70+ System .out .println (
71+ "[TOO-MANY-REQUESTS] Waiting " + waitMs + "ms for " + request .url ());
72+ }
6473 try {
6574 Thread .sleep (waitMs );
6675 } catch (final InterruptedException iex ) {
@@ -69,8 +78,4 @@ public Response executeRequestWithLimit(final Request request) throws IOExceptio
6978 }
7079 throw new IllegalStateException ("Retrying/Rate-limiting for " + request .url () + "failed" , cause );
7180 }
72-
73- public OkHttpClient getClient () {
74- return client ;
75- }
7681}
0 commit comments