Skip to content

Commit 9f94a29

Browse files
committed
Remove recording retry/throttle as this is now handled in a more general way
1 parent 460e0f7 commit 9f94a29

1 file changed

Lines changed: 15 additions & 70 deletions

File tree

extractor/src/test/java/org/schabi/newpipe/downloader/RecordingDownloader.java

Lines changed: 15 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.nio.file.Files;
1616
import java.nio.file.Path;
1717
import java.nio.file.Paths;
18-
import java.util.Random;
1918

2019
import javax.annotation.Nonnull;
2120

@@ -47,20 +46,6 @@ class RecordingDownloader extends Downloader {
4746
private int index = 0;
4847
private final String path;
4948

50-
// try to prevent ReCaptchaExceptions / rate limits by tracking and throttling the requests
51-
/**
52-
* The maximum number of requests per 20 seconds which are executed
53-
* by the {@link RecordingDownloader}.
54-
* 20 seconds is used as upper bound because the rate limit can be triggered within 30 seconds
55-
* and hitting the rate limit should be prevented because it comes with a bigger delay.
56-
* The values can be adjusted when executing the downloader and running into problems.
57-
* <p>TODO: Allow adjusting the value by setting a param in the gradle command</p>
58-
*/
59-
private static final int MAX_REQUESTS_PER_20_SECONDS = 30;
60-
private static final long[] requestTimes = new long[MAX_REQUESTS_PER_20_SECONDS];
61-
private static int requestTimesCursor = -1;
62-
private static final Random throttleRandom = new Random();
63-
6449
/**
6550
* Creates the folder described by {@code stringPath} if it does not exist.
6651
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
@@ -84,71 +69,31 @@ public RecordingDownloader(final String stringPath) throws IOException {
8469
@Override
8570
public Response execute(@Nonnull final Request request) throws IOException,
8671
ReCaptchaException {
87-
88-
// Delay the execution if the max number of requests per minute is reached
89-
final long currentTime = System.currentTimeMillis();
90-
// the cursor points to the latest request time and the next position is the oldest one
91-
final int oldestRequestTimeCursor = (requestTimesCursor + 1) % requestTimes.length;
92-
final long oldestRequestTime = requestTimes[oldestRequestTimeCursor];
93-
if (oldestRequestTime + 20_000 >= currentTime) {
94-
try {
95-
// sleep at least until the oldest request is 20s old, but not more than 20s
96-
final int minSleepTime = (int) (currentTime - oldestRequestTime);
97-
Thread.sleep(minSleepTime + throttleRandom.nextInt(20_000 - minSleepTime));
98-
} catch (InterruptedException e) {
99-
// handle the exception gracefully because it's not critical for the test
100-
System.err.println("Error while throttling the RecordingDownloader.");
101-
e.printStackTrace();
102-
}
103-
}
104-
requestTimesCursor = oldestRequestTimeCursor; // the oldest value needs to be overridden
105-
requestTimes[requestTimesCursor] = System.currentTimeMillis();
106-
107-
// Handle ReCaptchaExceptions by retrying the request once after a while
108-
try {
109-
return executeRequest(request);
110-
} catch (ReCaptchaException e) {
111-
try {
112-
// sleep for 35-60 seconds to circumvent the rate limit
113-
System.out.println("Throttling the RecordingDownloader to handle a ReCaptcha."
114-
+ " Sleeping for 35-60 seconds.");
115-
Thread.sleep(35_000 + throttleRandom.nextInt(25_000));
116-
} catch (InterruptedException ie) {
117-
// handle the exception gracefully because it's not critical for the test
118-
System.err.println("Error while throttling the RecordingDownloader.");
119-
ie.printStackTrace();
120-
e.printStackTrace();
121-
}
122-
return executeRequest(request);
123-
}
124-
}
125-
126-
@Nonnull
127-
private Response executeRequest(@Nonnull final Request request) throws IOException,
128-
ReCaptchaException {
12972
final Downloader downloader = DownloaderTestImpl.getInstance();
13073
Response response = downloader.execute(request);
131-
String cleanedResponseBody = response.responseBody().replaceAll(IP_V4_PATTERN, "127.0.0.1");
13274
response = new Response(
133-
response.responseCode(),
134-
response.responseMessage(),
135-
response.responseHeaders(),
136-
cleanedResponseBody,
137-
response.latestUrl()
75+
response.responseCode(),
76+
response.responseMessage(),
77+
response.responseHeaders(),
78+
response.responseBody().replaceAll(IP_V4_PATTERN, "127.0.0.1"),
79+
response.latestUrl()
13880
);
13981

140-
final File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index
141-
+ ".json");
82+
final File outputFile = new File(
83+
path + File.separator + FILE_NAME_PREFIX + index + ".json");
14284
index++;
14385
outputFile.createNewFile();
144-
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile),
145-
StandardCharsets.UTF_8);
146-
new GsonBuilder()
86+
87+
try (final OutputStreamWriter writer = new OutputStreamWriter(
88+
new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
89+
90+
new GsonBuilder()
14791
.setPrettyPrinting()
14892
.create()
14993
.toJson(new TestRequestResponse(request, response), writer);
150-
writer.flush();
151-
writer.close();
94+
95+
writer.flush();
96+
}
15297

15398
return response;
15499
}

0 commit comments

Comments
 (0)