kopia lustrzana https://github.com/TeamNewPipe/NewPipeExtractor
Move into dedicated package
rodzic
f1b90985da
commit
e1b94c4148
extractor/src/test/java/org/schabi/newpipe/downloader/ratelimiting
|
@ -1,5 +1,7 @@
|
|||
package org.schabi.newpipe.downloader.ratelimiting;
|
||||
|
||||
import org.schabi.newpipe.downloader.ratelimiting.limiter.RateLimiter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ProtocolException;
|
||||
import java.time.Duration;
|
||||
|
@ -12,6 +14,9 @@ import okhttp3.Request;
|
|||
import okhttp3.Response;
|
||||
|
||||
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 Map<Predicate<String>, RateLimiter> FORCED_RATE_LIMITERS = Map.ofEntries(
|
||||
|
@ -43,8 +48,10 @@ public class RateLimitedClientWrapper {
|
|||
for (int tries = 1; tries <= 3; tries++) {
|
||||
try {
|
||||
final double rateLimitedSec = getRateLimiterFor(request).acquire();
|
||||
System.out.println(
|
||||
"[RATE-LIMIT] Waited " + rateLimitedSec + "s for " + request.url());
|
||||
if (DEBUG_PRINT) {
|
||||
System.out.println(
|
||||
"[RATE-LIMIT] Waited " + rateLimitedSec + "s for " + request.url());
|
||||
}
|
||||
|
||||
final Response response = client.newCall(request).execute();
|
||||
if(response.code() != 429) { // 429 = Too many requests
|
||||
|
@ -59,8 +66,10 @@ public class RateLimitedClientWrapper {
|
|||
}
|
||||
|
||||
final int waitMs = REQUEST_RATE_LIMITED_WAIT_MS * tries;
|
||||
System.out.println(
|
||||
"[TOO-MANY-REQUESTS] Waiting " + waitMs + "ms for " + request.url());
|
||||
if (DEBUG_PRINT) {
|
||||
System.out.println(
|
||||
"[TOO-MANY-REQUESTS] Waiting " + waitMs + "ms for " + request.url());
|
||||
}
|
||||
try {
|
||||
Thread.sleep(waitMs);
|
||||
} catch (final InterruptedException iex) {
|
||||
|
@ -69,8 +78,4 @@ public class RateLimitedClientWrapper {
|
|||
}
|
||||
throw new IllegalStateException("Retrying/Rate-limiting for " + request.url() + "failed", cause);
|
||||
}
|
||||
|
||||
public OkHttpClient getClient() {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
* the License.
|
||||
*/
|
||||
|
||||
package org.schabi.newpipe.downloader.ratelimiting;
|
||||
package org.schabi.newpipe.downloader.ratelimiting.limiter;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
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 org.schabi.newpipe.downloader.ratelimiting.SmoothRateLimiter.SmoothWarmingUp;
|
||||
import org.schabi.newpipe.downloader.ratelimiting.limiter.SmoothRateLimiter.SmoothWarmingUp;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Locale;
|
|
@ -12,7 +12,7 @@
|
|||
* the License.
|
||||
*/
|
||||
|
||||
package org.schabi.newpipe.downloader.ratelimiting;
|
||||
package org.schabi.newpipe.downloader.ratelimiting.limiter;
|
||||
|
||||
import static java.lang.Math.min;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
|
@ -7,4 +7,4 @@
|
|||
*
|
||||
* @author litetex
|
||||
*/
|
||||
package org.schabi.newpipe.downloader.ratelimiting;
|
||||
package org.schabi.newpipe.downloader.ratelimiting.limiter;
|
Ładowanie…
Reference in New Issue