From ce27c309d3c9453d8d4c9f2ac4559695aeb688ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Tue, 2 May 2023 13:00:33 +0200 Subject: [PATCH] Remove timeout for rate-limit pool (#2458) * Remove timeout for rate-limit pool * Improve shutdown logic --- .../requests/SequentialRestRateLimiter.java | 42 ++++++++++--------- .../utils/config/ThreadingConfig.java | 18 ++------ 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/requests/SequentialRestRateLimiter.java b/src/main/java/net/dv8tion/jda/api/requests/SequentialRestRateLimiter.java index 8e7b4082af..8d21fc8d67 100644 --- a/src/main/java/net/dv8tion/jda/api/requests/SequentialRestRateLimiter.java +++ b/src/main/java/net/dv8tion/jda/api/requests/SequentialRestRateLimiter.java @@ -113,18 +113,13 @@ public void stop(boolean shutdown, @Nonnull Runnable callback) shutdownHandle.thenRun(callback); if (!doShutdown) { - int size = buckets.size(); - int average = (int) Math.ceil( - buckets.values().stream() - .map(Bucket::getRequests) - .mapToInt(Collection::size) - .average().orElse(0) - ); - - if (size > 0 && average > 0) - log.info("Waiting for {} bucket(s) to finish. Average queue size of {} requests", size, average); - else if (size == 0) - doShutdown = true; + int count = buckets.values().stream() + .mapToInt(bucket -> bucket.getRequests().size()) + .sum(); + + if (count > 0) + log.info("Waiting for {} requests to finish.", count); + doShutdown = count == 0; } } if (doShutdown && !isShutdown) @@ -184,19 +179,26 @@ private void cleanup() bucket.requests.removeIf(Work::isSkipped); // Remove cancelled requests // Check if the bucket is empty - if (bucket.isUninit() && bucket.requests.isEmpty()) - entries.remove(); // remove uninit if requests are empty - // If the requests of the bucket are drained and the reset is expired the bucket has no valuable information - else if (bucket.requests.isEmpty() && bucket.reset <= getNow()) - entries.remove(); - // Remove empty buckets when the rate limiter is stopped - else if (bucket.requests.isEmpty() && isStopped) - entries.remove(); + if (bucket.requests.isEmpty()) + { + // remove uninit if requests are empty + if (bucket.isUninit()) + entries.remove(); + // If the requests of the bucket are drained and the reset is expired the bucket has no valuable information + else if (bucket.reset <= getNow()) + entries.remove(); + // Remove empty buckets when the rate limiter is stopped + else if (isStopped) + entries.remove(); + } } + // Log how many buckets were removed size -= buckets.size(); if (size > 0) log.debug("Removed {} expired buckets", size); + else if (isStopped && !isShutdown) + shutdown(); }); } diff --git a/src/main/java/net/dv8tion/jda/internal/utils/config/ThreadingConfig.java b/src/main/java/net/dv8tion/jda/internal/utils/config/ThreadingConfig.java index 3ee6855e51..1bc7057e73 100644 --- a/src/main/java/net/dv8tion/jda/internal/utils/config/ThreadingConfig.java +++ b/src/main/java/net/dv8tion/jda/internal/utils/config/ThreadingConfig.java @@ -20,7 +20,10 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.concurrent.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.function.Supplier; public class ThreadingConfig @@ -97,19 +100,6 @@ public void shutdown() eventPool.shutdown(); if (shutdownAudioPool && audioPool != null) audioPool.shutdown(); - if (shutdownRateLimitPool) - { - if (rateLimitPool instanceof ScheduledThreadPoolExecutor) - { - ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) rateLimitPool; - executor.setKeepAliveTime(5L, TimeUnit.SECONDS); - executor.allowCoreThreadTimeOut(true); - } - else - { - rateLimitPool.shutdown(); - } - } } public void shutdownRequester()