Skip to content

Commit e1b94c4

Browse files
committed
Move into dedicated package
1 parent f1b9098 commit e1b94c4

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/RateLimitedClientWrapper.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.downloader.ratelimiting;
22

3+
import org.schabi.newpipe.downloader.ratelimiting.limiter.RateLimiter;
4+
35
import java.io.IOException;
46
import java.net.ProtocolException;
57
import java.time.Duration;
@@ -12,6 +14,9 @@
1214
import okhttp3.Response;
1315

1416
public 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
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* the License.
1313
*/
1414

15-
package org.schabi.newpipe.downloader.ratelimiting;
15+
package org.schabi.newpipe.downloader.ratelimiting.limiter;
1616

1717
import static java.lang.Math.max;
1818
import static java.util.concurrent.TimeUnit.MICROSECONDS;
@@ -21,7 +21,7 @@
2121

2222
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2323

24-
import org.schabi.newpipe.downloader.ratelimiting.SmoothRateLimiter.SmoothWarmingUp;
24+
import org.schabi.newpipe.downloader.ratelimiting.limiter.SmoothRateLimiter.SmoothWarmingUp;
2525

2626
import java.time.Duration;
2727
import java.util.Locale;

extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/SmoothRateLimiter.java renamed to extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/limiter/SmoothRateLimiter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* the License.
1313
*/
1414

15-
package org.schabi.newpipe.downloader.ratelimiting;
15+
package org.schabi.newpipe.downloader.ratelimiting.limiter;
1616

1717
import static java.lang.Math.min;
1818
import static java.util.concurrent.TimeUnit.SECONDS;

extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/package-info.java renamed to extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting/limiter/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
*
88
* @author litetex
99
*/
10-
package org.schabi.newpipe.downloader.ratelimiting;
10+
package org.schabi.newpipe.downloader.ratelimiting.limiter;

0 commit comments

Comments
 (0)