Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle PubSub better with a queue and waiting for requests #652

Merged
merged 1 commit into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/main/java/me/kavin/piped/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import rocks.kavin.reqwest4j.ReqwestUtils;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -102,15 +104,39 @@ public void run() {

Collections.shuffle(channelIds);

channelIds.stream()
.parallel()
.forEach(id -> Multithreading.runAsyncLimitedPubSub(() -> {
var queue = new ConcurrentLinkedQueue<>(channelIds);

System.out.println("PubSub: queue size - " + queue.size() + " channels");

for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
new Thread(() -> {

Object o = new Object();

String channelId;
while ((channelId = queue.poll()) != null) {
try {
PubSubHelper.subscribePubSub(id);
CompletableFuture<?> future = PubSubHelper.subscribePubSub(channelId);

if (future == null)
continue;

future.whenComplete((resp, throwable) -> {
synchronized (o) {
o.notify();
}
});

synchronized (o) {
o.wait();
}

} catch (Exception e) {
ExceptionHandler.handle(e);
}
}));
}
}, "PubSub-" + i).start();
}

} catch (Exception e) {
e.printStackTrace();
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/me/kavin/piped/utils/PubSubHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
import okio.Buffer;
import org.hibernate.StatelessSession;
import rocks.kavin.reqwest4j.ReqwestUtils;
import rocks.kavin.reqwest4j.Response;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class PubSubHelper {
public static void subscribePubSub(String channelId) throws IOException {

@Nullable
public static CompletableFuture<Response> subscribePubSub(String channelId) throws IOException {

if (!ChannelHelpers.isValidId(channelId))
return;
return null;

PubSub pubsub = DatabaseHelper.getPubSubFromId(channelId);

Expand Down Expand Up @@ -44,16 +49,21 @@ public static void subscribePubSub(String channelId) throws IOException {
var buffer = new Buffer();
formBuilder.build().writeTo(buffer);

ReqwestUtils.fetch(Constants.PUBSUB_HUB_URL, "POST", buffer.readByteArray(), Map.of())
.thenAccept(resp -> {
if (resp.status() != 202)
var completableFuture = ReqwestUtils.fetch(Constants.PUBSUB_HUB_URL, "POST", buffer.readByteArray(), Map.of());

completableFuture
.whenComplete((resp, e) -> {
if (e != null) {
ExceptionHandler.handle((Exception) e);
return;
}
if (resp != null && resp.status() != 202)
System.out.println("Failed to subscribe: " + resp.status() + "\n" + new String(resp.body()));
})
.exceptionally(e -> {
ExceptionHandler.handle((Exception) e);
return null;
});

return completableFuture;
}
return null;
}

public static void updatePubSub(String channelId) {
Expand Down