forked from j62/ctbrec
1
0
Fork 0

Fix race condition in HlsDownload

This commit is contained in:
0xb00bface 2020-12-27 13:27:55 +01:00
parent 32429b192c
commit 1c8758ef52
1 changed files with 12 additions and 2 deletions

View File

@ -23,6 +23,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
@ -151,7 +152,17 @@ public abstract class AbstractHlsDownload2 extends AbstractDownload {
}
protected void execute(SegmentDownload segmentDownload) {
downloadExecutor.submit(segmentDownload);
CompletableFuture.supplyAsync(() -> downloadExecutor.submit(segmentDownload), downloadExecutor)
.whenComplete((result, executor) -> {
try {
segmentDownloadFinished(result.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
}
private void handleHttpException(HttpException e) throws IOException {
@ -343,7 +354,6 @@ public abstract class AbstractHlsDownload2 extends AbstractDownload {
OutputStream targetStream = getSegmentOutputStream(prefix, tmp.getName());
SegmentDownload segmentDownload = new SegmentDownload(model, playlist, segmentUrl, client, targetStream);
execute(segmentDownload);
segmentDownloadFinished(segmentDownload);
}
}
}