diff --git a/common/src/main/java/me/lucko/luckperms/common/locale/Message.java b/common/src/main/java/me/lucko/luckperms/common/locale/Message.java index b56d95128..158417e86 100644 --- a/common/src/main/java/me/lucko/luckperms/common/locale/Message.java +++ b/common/src/main/java/me/lucko/luckperms/common/locale/Message.java @@ -454,6 +454,22 @@ public interface Message { .append(text(processor, DARK_GREEN)) .build(); + Args0 VERBOSE_NOTIFICATION_RATE_LIMITED = () -> prefixed(text() + // "&3VB &3&l> &cNotification rate limit exceeded. Some events are not being shown. Use &7/lp verbose upload &cto see the full output." + .append(translatable("luckperms.logs.verbose-prefix", DARK_AQUA)) + .append(space()) + .append(text('>', DARK_AQUA, BOLD)) + .append(space()) + .append(translatable() + .key("luckperms.logs.verbose.rate-limit-exceeded") + .color(RED) + .args(text("/lp verbose upload", GRAY) + .clickEvent(ClickEvent.runCommand("/lp verbose upload")) + ) + .append(FULL_STOP) + ) + ); + Args1 EXPORT_LOG = msg -> prefixed(text() // "&3EXPORT &3&l> &f{}" .append(translatable("luckperms.logs.export-prefix", DARK_AQUA)) diff --git a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java index 2cdc92e77..c5e24e0a4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java +++ b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java @@ -73,11 +73,15 @@ public class VerboseListener { .withZone(ZoneId.systemDefault()); // how much data should we store before stopping. - private static final int DATA_TRUNCATION = 10000; + private static final int DATA_TRUNCATION = 10_000; // how many lines should we include in each stack trace send as a chat message private static final int STACK_TRUNCATION_CHAT = 15; // how many lines should we include in each stack trace in the web output private static final int STACK_TRUNCATION_WEB = 40; + // rate limit for notifications: 50 notifications per second for players, 100 for console + private static final int NOTIFICATION_RATE_LIMIT_MAX_EVENTS_PLAYER = 50; + private static final int NOTIFICATION_RATE_LIMIT_MAX_EVENTS_CONSOLE = 100; + private static final long NOTIFICATION_RATE_LIMIT_WINDOW_MS = 1000; private static final StackTracePrinter FILTERING_PRINTER = StackTracePrinter.builder() .ignoreClassStartingWith("me.lucko.luckperms.") @@ -116,6 +120,10 @@ public class VerboseListener { private final AtomicInteger matchedCounter = new AtomicInteger(0); // the events which passed the filter, up to a max size of #DATA_TRUNCATION private final List results = new ArrayList<>(DATA_TRUNCATION / 10); + // a list of timestamps when a notification message was sent, used for rate limiting + private final List notificationTimestamps = new ArrayList<>(); + // track if we've already warned about rate limiting in the current window + private boolean notificationRateLimitWarningShown = false; public VerboseListener(Sender notifiedSender, VerboseFilter filter, boolean notify) { this.notifiedSender = notifiedSender; @@ -152,6 +160,10 @@ public class VerboseListener { } private void sendNotification(VerboseEvent event) { + if (!checkNotificationRateLimit()) { + return; + } + // form a text component from the check trace Component component; if (event instanceof PermissionCheckEvent) { @@ -227,6 +239,36 @@ public class VerboseListener { this.notifiedSender.sendMessage(component.hoverEvent(hoverEvent)); } + /** + * Check if we should send a notification based on rate limiting. + * + * @return true if the notification should be sent, false if rate limited + */ + private boolean checkNotificationRateLimit() { + long now = System.currentTimeMillis(); + int maxEvents = this.notifiedSender.isConsole() ? NOTIFICATION_RATE_LIMIT_MAX_EVENTS_CONSOLE : NOTIFICATION_RATE_LIMIT_MAX_EVENTS_PLAYER; + + // remove timestamps outside the current window + this.notificationTimestamps.removeIf(timestamp -> (now - timestamp) > NOTIFICATION_RATE_LIMIT_WINDOW_MS); + + if (this.notificationTimestamps.size() >= maxEvents) { + if (!this.notificationRateLimitWarningShown) { + Message.VERBOSE_NOTIFICATION_RATE_LIMITED.send(this.notifiedSender); + this.notificationRateLimitWarningShown = true; + } + return false; + } + + this.notificationTimestamps.add(now); + + // reset the warning flag if we're back under the limit + if (this.notificationTimestamps.size() < maxEvents) { + this.notificationRateLimitWarningShown = false; + } + + return true; + } + private static boolean shouldFilterStackTrace(VerboseEvent event) { if (event instanceof PermissionCheckEvent) { PermissionCheckEvent permissionEvent = (PermissionCheckEvent) event; diff --git a/common/src/main/resources/luckperms_en.properties b/common/src/main/resources/luckperms_en.properties index 33cfa7d13..bc5117f7b 100644 --- a/common/src/main/resources/luckperms_en.properties +++ b/common/src/main/resources/luckperms_en.properties @@ -8,6 +8,7 @@ luckperms.logs.verbose.hover.thread-key=Thread luckperms.logs.verbose.hover.trace-key=Trace luckperms.logs.verbose.hover.processor-key=Processor luckperms.logs.verbose.hover.overflow=and {0} more +luckperms.logs.verbose.rate-limit-exceeded=Notification rate limit exceeded. Some events are not being shown. Use {0} to see the full output luckperms.logs.export-prefix=EXPORT luckperms.commandsystem.available-commands=Use {0} to view available commands luckperms.commandsystem.command-not-recognised=Command not recognised