Fix LBRY timeouts.

This commit is contained in:
FireMaskterK 2021-11-09 08:03:21 +00:00
parent ced7b0ee9e
commit a7b02e9566
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD

View File

@ -116,7 +116,7 @@ public class ResponseHelper {
ExceptionUtils.rethrow(e); ExceptionUtils.rethrow(e);
} }
return null; return null;
}); }, Multithreading.getCachedExecutor());
CompletableFuture<String> futureLbryId = CompletableFuture.supplyAsync(() -> { CompletableFuture<String> futureLbryId = CompletableFuture.supplyAsync(() -> {
try { try {
@ -125,16 +125,20 @@ public class ResponseHelper {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);
} }
return null; return null;
}); }, Multithreading.getCachedExecutor());
CompletableFuture<String> futureLBRY = CompletableFuture.supplyAsync(() -> { CompletableFuture<String> futureLBRY = CompletableFuture.supplyAsync(() -> {
try { try {
return getLBRYStreamURL(futureLbryId); String lbryId = null;
lbryId = futureLbryId.completeOnTimeout(null, 2, TimeUnit.SECONDS).get();
return getLBRYStreamURL(lbryId);
} catch (Exception e) { } catch (Exception e) {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);
} }
return null; return null;
}); }, Multithreading.getCachedExecutor());
final List<Subtitle> subtitles = new ObjectArrayList<>(); final List<Subtitle> subtitles = new ObjectArrayList<>();
final List<ChapterSegment> chapters = new ObjectArrayList<>(); final List<ChapterSegment> chapters = new ObjectArrayList<>();
@ -157,7 +161,7 @@ public class ResponseHelper {
String lbryURL = null; String lbryURL = null;
try { try {
lbryURL = futureLBRY.get(3, TimeUnit.SECONDS); lbryURL = futureLBRY.completeOnTimeout(null, 3, TimeUnit.SECONDS).get();
} catch (Exception e) { } catch (Exception e) {
// ignored // ignored
} }
@ -849,23 +853,32 @@ public class ResponseHelper {
if (user != null) { if (user != null) {
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
if (override) try {
user.setSubscribed(Arrays.asList(channelIds)); s.getTransaction().begin();
else if (override)
s.createNativeQuery("delete from users_subscribed where subscriber = :id")
.setParameter("id", user.getId()).executeUpdate();
for (String channelId : channelIds) for (String channelId : channelIds)
if (!user.getSubscribed().contains(channelId)) if (override || !user.getSubscribed().contains(channelId))
user.getSubscribed().add(channelId); s.createNativeQuery("insert into users_subscribed (subscriber, channel) values (?,?)")
.setParameter(1, user.getId()).setParameter(2, channelId).executeUpdate();
if (channelIds.length > 0) { if (channelIds.length > 0) {
s.update(user); s.getTransaction().commit();
s.beginTransaction().commit(); }
s.close();
} catch (Exception e) {
e.printStackTrace();
} }
s.close();
}); });
for (String channelId : channelIds) { for (String channelId : channelIds) {
if (channelId.length() > 0)
break;
Multithreading.runAsyncLimited(() -> { Multithreading.runAsyncLimited(() -> {
try { try {
@ -970,17 +983,10 @@ public class ResponseHelper {
.getJSONObject("data").getJSONObject("videos").optString(videoId); .getJSONObject("data").getJSONObject("videos").optString(videoId);
} }
private static final String getLBRYStreamURL(CompletableFuture<String> futureLbryId) private static final String getLBRYStreamURL(String lbryId)
throws IOException, InterruptedException, ExecutionException { throws IOException, InterruptedException, ExecutionException {
String lbryId = ""; if (lbryId != null && !lbryId.isEmpty())
try {
lbryId = futureLbryId.get(2, TimeUnit.SECONDS);
} catch (Exception e) {
// ignored
}
if (!lbryId.isEmpty())
return new JSONObject( return new JSONObject(
Constants.h2client.send( Constants.h2client.send(
HttpRequest.newBuilder(URI.create("https://api.lbry.tv/api/v1/proxy?m=get")) HttpRequest.newBuilder(URI.create("https://api.lbry.tv/api/v1/proxy?m=get"))