mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-28 07:50:34 +05:30
Move into dedicated package
This commit is contained in:
parent
f1b90985da
commit
e1b94c4148
@ -1,5 +1,7 @@
|
|||||||
package org.schabi.newpipe.downloader.ratelimiting;
|
package org.schabi.newpipe.downloader.ratelimiting;
|
||||||
|
|
||||||
|
import org.schabi.newpipe.downloader.ratelimiting.limiter.RateLimiter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ProtocolException;
|
import java.net.ProtocolException;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
@ -12,6 +14,9 @@ import okhttp3.Request;
|
|||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|
||||||
public class RateLimitedClientWrapper {
|
public class RateLimitedClientWrapper {
|
||||||
|
private static final boolean DEBUG_PRINT =
|
||||||
|
"1".equals(System.getProperty("rateLimitClientDebugPrint",
|
||||||
|
System.getenv("RATE_LIMIT_CLIENT_DEBUG_PRINT")));
|
||||||
|
|
||||||
private static final int REQUEST_RATE_LIMITED_WAIT_MS = 5_000;
|
private static final int REQUEST_RATE_LIMITED_WAIT_MS = 5_000;
|
||||||
private static final Map<Predicate<String>, RateLimiter> FORCED_RATE_LIMITERS = Map.ofEntries(
|
private static final Map<Predicate<String>, RateLimiter> FORCED_RATE_LIMITERS = Map.ofEntries(
|
||||||
@ -43,8 +48,10 @@ public class RateLimitedClientWrapper {
|
|||||||
for (int tries = 1; tries <= 3; tries++) {
|
for (int tries = 1; tries <= 3; tries++) {
|
||||||
try {
|
try {
|
||||||
final double rateLimitedSec = getRateLimiterFor(request).acquire();
|
final double rateLimitedSec = getRateLimiterFor(request).acquire();
|
||||||
System.out.println(
|
if (DEBUG_PRINT) {
|
||||||
"[RATE-LIMIT] Waited " + rateLimitedSec + "s for " + request.url());
|
System.out.println(
|
||||||
|
"[RATE-LIMIT] Waited " + rateLimitedSec + "s for " + request.url());
|
||||||
|
}
|
||||||
|
|
||||||
final Response response = client.newCall(request).execute();
|
final Response response = client.newCall(request).execute();
|
||||||
if(response.code() != 429) { // 429 = Too many requests
|
if(response.code() != 429) { // 429 = Too many requests
|
||||||
@ -59,8 +66,10 @@ public class RateLimitedClientWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final int waitMs = REQUEST_RATE_LIMITED_WAIT_MS * tries;
|
final int waitMs = REQUEST_RATE_LIMITED_WAIT_MS * tries;
|
||||||
System.out.println(
|
if (DEBUG_PRINT) {
|
||||||
"[TOO-MANY-REQUESTS] Waiting " + waitMs + "ms for " + request.url());
|
System.out.println(
|
||||||
|
"[TOO-MANY-REQUESTS] Waiting " + waitMs + "ms for " + request.url());
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
Thread.sleep(waitMs);
|
Thread.sleep(waitMs);
|
||||||
} catch (final InterruptedException iex) {
|
} catch (final InterruptedException iex) {
|
||||||
@ -69,8 +78,4 @@ public class RateLimitedClientWrapper {
|
|||||||
}
|
}
|
||||||
throw new IllegalStateException("Retrying/Rate-limiting for " + request.url() + "failed", cause);
|
throw new IllegalStateException("Retrying/Rate-limiting for " + request.url() + "failed", cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
public OkHttpClient getClient() {
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
* the License.
|
* the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.schabi.newpipe.downloader.ratelimiting;
|
package org.schabi.newpipe.downloader.ratelimiting.limiter;
|
||||||
|
|
||||||
import static java.lang.Math.max;
|
import static java.lang.Math.max;
|
||||||
import static java.util.concurrent.TimeUnit.MICROSECONDS;
|
import static java.util.concurrent.TimeUnit.MICROSECONDS;
|
||||||
@ -21,7 +21,7 @@ import static java.util.concurrent.TimeUnit.SECONDS;
|
|||||||
|
|
||||||
import com.google.errorprone.annotations.CanIgnoreReturnValue;
|
import com.google.errorprone.annotations.CanIgnoreReturnValue;
|
||||||
|
|
||||||
import org.schabi.newpipe.downloader.ratelimiting.SmoothRateLimiter.SmoothWarmingUp;
|
import org.schabi.newpipe.downloader.ratelimiting.limiter.SmoothRateLimiter.SmoothWarmingUp;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
@ -12,7 +12,7 @@
|
|||||||
* the License.
|
* the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.schabi.newpipe.downloader.ratelimiting;
|
package org.schabi.newpipe.downloader.ratelimiting.limiter;
|
||||||
|
|
||||||
import static java.lang.Math.min;
|
import static java.lang.Math.min;
|
||||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
@ -7,4 +7,4 @@
|
|||||||
*
|
*
|
||||||
* @author litetex
|
* @author litetex
|
||||||
*/
|
*/
|
||||||
package org.schabi.newpipe.downloader.ratelimiting;
|
package org.schabi.newpipe.downloader.ratelimiting.limiter;
|
Loading…
x
Reference in New Issue
Block a user