Add rate limit for verbose chat output

This commit is contained in:
Luck committed 2026-02-21 09:29:14 +00:00
1 parent 6292d2d3e6
commit b25c335b7b
3 files changed
+60 -1

No files matched your search

@@ -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<String> EXPORT_LOG = msg -> prefixed(text()
// "&3EXPORT &3&l> &f{}"
.append(translatable("luckperms.logs.export-prefix", DARK_AQUA))
@@ -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<VerboseEvent> results = new ArrayList<>(DATA_TRUNCATION / 10);
// a list of timestamps when a notification message was sent, used for rate limiting
private final List<Long> 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;
@@ -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