From 1c8e8b190e5f257de61a34285c5677ce48a0811e Mon Sep 17 00:00:00 2001 From: FireMaskterK <20838718+FireMasterK@users.noreply.github.com> Date: Tue, 9 Nov 2021 08:06:13 +0000 Subject: [PATCH] Fix LBRY timeouts. --- .../me/kavin/piped/utils/ResponseHelper.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index b5d75da..cbb44bb 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -116,7 +116,7 @@ public class ResponseHelper { ExceptionUtils.rethrow(e); } return null; - }); + }, Multithreading.getCachedExecutor()); CompletableFuture futureLbryId = CompletableFuture.supplyAsync(() -> { try { @@ -125,16 +125,20 @@ public class ResponseHelper { ExceptionHandler.handle(e); } return null; - }); + }, Multithreading.getCachedExecutor()); CompletableFuture futureLBRY = CompletableFuture.supplyAsync(() -> { try { - return getLBRYStreamURL(futureLbryId); + String lbryId = null; + + lbryId = futureLbryId.completeOnTimeout(null, 2, TimeUnit.SECONDS).get(); + + return getLBRYStreamURL(lbryId); } catch (Exception e) { ExceptionHandler.handle(e); } return null; - }); + }, Multithreading.getCachedExecutor()); final List subtitles = new ObjectArrayList<>(); final List chapters = new ObjectArrayList<>(); @@ -157,7 +161,7 @@ public class ResponseHelper { String lbryURL = null; try { - lbryURL = futureLBRY.get(3, TimeUnit.SECONDS); + lbryURL = futureLBRY.completeOnTimeout(null, 3, TimeUnit.SECONDS).get(); } catch (Exception e) { // ignored } @@ -970,17 +974,10 @@ public class ResponseHelper { .getJSONObject("data").getJSONObject("videos").optString(videoId); } - private static final String getLBRYStreamURL(CompletableFuture futureLbryId) + private static final String getLBRYStreamURL(String lbryId) throws IOException, InterruptedException, ExecutionException { - String lbryId = ""; - try { - lbryId = futureLbryId.get(2, TimeUnit.SECONDS); - } catch (Exception e) { - // ignored - } - - if (!lbryId.isEmpty()) + if (lbryId != null && !lbryId.isEmpty()) return new JSONObject( Constants.h2client.send( HttpRequest.newBuilder(URI.create("https://api.lbry.tv/api/v1/proxy?m=get"))