Skip to content

Commit ed46d62

Browse files
committed
feat: implement rate limit solution
This commit slows down the processing of event queues for the sole purpose of preventing Discord API endpoint saturation as well as to avoid the global Discord API Gateway rate limiter. Moreover, it removes a few 'static' keywords that should not be there.
1 parent c638d7e commit ed46d62

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

application/src/main/java/org/togetherjava/tjbot/features/dynamicvc/DynamicVoiceListener.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent;
88
import org.apache.commons.lang3.tuple.Pair;
99
import org.jetbrains.annotations.NotNull;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1012

1113
import org.togetherjava.tjbot.config.Config;
1214
import org.togetherjava.tjbot.features.VoiceReceiverAdapter;
@@ -19,6 +21,8 @@
1921
import java.util.Optional;
2022
import java.util.Queue;
2123
import java.util.concurrent.CompletableFuture;
24+
import java.util.concurrent.Executor;
25+
import java.util.concurrent.TimeUnit;
2226
import java.util.concurrent.atomic.AtomicBoolean;
2327
import java.util.function.Predicate;
2428
import java.util.regex.Matcher;
@@ -41,14 +45,22 @@
4145
*/
4246
public class DynamicVoiceListener extends VoiceReceiverAdapter {
4347

48+
private final Logger logger = LoggerFactory.getLogger(DynamicVoiceListener.class);
49+
4450
private final Map<String, Predicate<String>> channelPredicates = new HashMap<>();
4551
private static final Pattern channelTopicPattern = Pattern.compile("(\\s+\\d+)$");
4652

4753
/** Map of event queues for each channel topic. */
48-
private static final Map<String, Queue<GuildVoiceUpdateEvent>> eventQueues = new HashMap<>();
54+
private final Map<String, Queue<GuildVoiceUpdateEvent>> eventQueues = new HashMap<>();
4955

5056
/** Map to track if an event queue is currently being processed for each channel topic. */
51-
private static final Map<String, AtomicBoolean> activeQueuesMap = new HashMap<>();
57+
private final Map<String, AtomicBoolean> activeQueuesMap = new HashMap<>();
58+
59+
/** Boolean to track if events from all queues should be handled at a slower rate. */
60+
private final AtomicBoolean slowmode = new AtomicBoolean(false);
61+
private final Executor eventQueueExecutor =
62+
CompletableFuture.delayedExecutor(1L, TimeUnit.SECONDS);
63+
private static final int SLOWMODE_THRESHOLD = 5;
5264

5365
/**
5466
* Initializes a new {@link DynamicVoiceListener} with the specified configuration.
@@ -85,11 +97,19 @@ private void insertEventToQueue(GuildVoiceUpdateEvent event, String channelTopic
8597
}
8698

8799
eventQueue.add(event);
100+
slowmode.set(eventQueue.size() >= SLOWMODE_THRESHOLD);
88101

89102
if (activeQueuesMap.get(channelTopic).get()) {
90103
return;
91104
}
92105

106+
if (slowmode.get()) {
107+
logger.info("Running with slowmode");
108+
CompletableFuture.runAsync(() -> processEventFromQueue(channelTopic),
109+
eventQueueExecutor);
110+
return;
111+
}
112+
93113
processEventFromQueue(channelTopic);
94114
}
95115

0 commit comments

Comments
 (0)