forked from j62/ctbrec
1
0
Fork 0

Small improvements

- segment expiration
- bigger connection pool
- show model with site on playlist error

(cherry picked from commit 11ded4e572d0244289d3b4c9ccf201559ab327e6)
This commit is contained in:
reusedname 2025-02-15 15:09:38 +05:00
parent 4dbcbaa707
commit 6f9024838c
3 changed files with 21 additions and 4 deletions

View File

@ -38,7 +38,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
@Slf4j
public abstract class HttpClient {
@Getter
private static final ConnectionPool GLOBAL_HTTP_CONN_POOL = new ConnectionPool(10, 2, TimeUnit.MINUTES);
private static final ConnectionPool GLOBAL_HTTP_CONN_POOL = new ConnectionPool(256, 2, TimeUnit.MINUTES);
@Getter
protected CookieJarImpl cookieJar;

View File

@ -302,7 +302,7 @@ public abstract class AbstractHlsDownload extends AbstractDownload {
throw new HttpException(response.code(), response.message());
}
} catch (SocketTimeoutException e) {
LOG.debug("Playlist request timed out ({}ms) for model {} {} time{}", config.getSettings().playlistRequestTimeout, model,
LOG.debug("Playlist request timed out ({}ms) for model {}:{} {} time{}", config.getSettings().playlistRequestTimeout, model.getSite().getName(), model,
++consecutivePlaylistTimeouts, (consecutivePlaylistTimeouts > 1) ? 's' : "");
// times out, return an empty playlist, so that the process can continue without wasting much more time
recordingEvents.add(RecordingEvent.of("Playlist request timed out " + consecutivePlaylistTimeouts));
@ -310,7 +310,7 @@ public abstract class AbstractHlsDownload extends AbstractDownload {
} catch (Exception e) {
consecutivePlaylistErrors++;
throw e;
}
}
}
private SegmentPlaylist parsePlaylist(String segmentPlaylistUrl, InputStream inputStream) throws IOException, ParseException, PlaylistException {

View File

@ -12,10 +12,13 @@ import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer;
import javax.crypto.NoSuchPaddingException;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@ -23,6 +26,8 @@ import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.time.Instant;
import java.time.Duration;
import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL;
import static ctbrec.recorder.download.hls.AbstractHlsDownload.addHeaders;
@ -36,6 +41,7 @@ public class SegmentDownload implements Callable<SegmentDownload> {
protected final Segment segment;
protected final Model model;
protected final OutputStream out;
protected final Instant createdAt;
private long size = 0;
protected boolean failed = false;
@ -48,12 +54,23 @@ public class SegmentDownload implements Callable<SegmentDownload> {
this.segment = segment;
this.client = client;
this.out = out;
this.url = new URL(segment.url);
this.url = URI.create(segment.url).toURL();
this.createdAt = Instant.now();
}
@Override
public SegmentDownload call() {
var expiresAt = createdAt.plusSeconds(10);
for (int tries = 1; tries <= 3 && !Thread.currentThread().isInterrupted(); tries++) { // NOSONAR
if (expiresAt.isBefore(Instant.now())) {
// segment has sexpired, skip it
LOG.warn("Segment for model {} is late {} seconds", model, Duration.between(expiresAt, Instant.now()));
failed = true;
exception = new Exception("Segment expired");
break;
}
Request request = createRequest();
try (Response response = client.execute(request)) {
handleResponse(response);