-
-
Notifications
You must be signed in to change notification settings - Fork 476
perf: Schedule rate-limit notifications on shared executor (JAVA-653) #5814
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,12 +23,13 @@ | |
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Date; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Timer; | ||
| import java.util.TimerTask; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.RejectedExecutionException; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
|
|
@@ -42,8 +43,9 @@ public final class RateLimiter implements Closeable { | |
| private final @NotNull Map<DataCategory, @NotNull Date> sentryRetryAfterLimit = | ||
| new ConcurrentHashMap<>(); | ||
| private final @NotNull List<IRateLimitObserver> rateLimitObservers = new CopyOnWriteArrayList<>(); | ||
| private @Nullable Timer timer = null; | ||
| private final @NotNull AutoClosableReentrantLock timerLock = new AutoClosableReentrantLock(); | ||
| private final @NotNull List<Future<?>> notifyObserversFutures = new ArrayList<>(); | ||
| private final @NotNull AutoClosableReentrantLock notifyFuturesLock = | ||
| new AutoClosableReentrantLock(); | ||
|
|
||
| public RateLimiter( | ||
| final @NotNull ICurrentDateProvider currentDateProvider, | ||
|
|
@@ -278,11 +280,11 @@ public void updateRetryAfterLimits( | |
| continue; | ||
| } | ||
|
|
||
| applyRetryAfterOnlyIfLonger(dataCategory, date); | ||
| applyRetryAfterOnlyIfLonger(dataCategory, date, retryAfterMillis); | ||
| } | ||
| } else { | ||
| // if categories are empty, we should apply to "all" categories. | ||
| applyRetryAfterOnlyIfLonger(DataCategory.All, date); | ||
| applyRetryAfterOnlyIfLonger(DataCategory.All, date, retryAfterMillis); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -291,7 +293,7 @@ public void updateRetryAfterLimits( | |
| final long retryAfterMillis = parseRetryAfterOrDefault(retryAfterHeader); | ||
| // we dont care if Date is UTC as we just add the relative seconds | ||
| final Date date = new Date(currentDateProvider.getCurrentTimeMillis() + retryAfterMillis); | ||
| applyRetryAfterOnlyIfLonger(DataCategory.All, date); | ||
| applyRetryAfterOnlyIfLonger(DataCategory.All, date, retryAfterMillis); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -300,10 +302,11 @@ public void updateRetryAfterLimits( | |
| * | ||
| * @param dataCategory the DataCategory | ||
| * @param date the Date to be applied | ||
| * @param delayMillis the millis until the rate limit is lifted | ||
| */ | ||
| @SuppressWarnings({"JdkObsolete", "JavaUtilDate"}) | ||
| private void applyRetryAfterOnlyIfLonger( | ||
| final @NotNull DataCategory dataCategory, final @NotNull Date date) { | ||
| final @NotNull DataCategory dataCategory, final @NotNull Date date, final long delayMillis) { | ||
| final Date oldDate = sentryRetryAfterLimit.get(dataCategory); | ||
|
|
||
| // only overwrite its previous date if the limit is even longer | ||
|
|
@@ -312,19 +315,25 @@ private void applyRetryAfterOnlyIfLonger( | |
|
|
||
| notifyRateLimitObservers(); | ||
|
|
||
| try (final @NotNull ISentryLifecycleToken ignored = timerLock.acquire()) { | ||
| if (timer == null) { | ||
| timer = new Timer(true); | ||
| // notify observers again once the rate limit is lifted, using the shared timer executor | ||
| // instead of a dedicated Timer thread | ||
| try (final @NotNull ISentryLifecycleToken ignored = notifyFuturesLock.acquire()) { | ||
| final @NotNull Iterator<Future<?>> iterator = notifyObserversFutures.iterator(); | ||
| while (iterator.hasNext()) { | ||
| if (iterator.next().isDone()) { | ||
| iterator.remove(); | ||
| } | ||
| } | ||
| try { | ||
| notifyObserversFutures.add( | ||
| options | ||
| .getTimerExecutorService() | ||
| .schedule(this::notifyRateLimitObservers, delayMillis)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shutdown stalls on pending rate-limit tasksMedium Severity Rate-limit lift notifications are now scheduled on the shared timer executor, often with long delays. Additional Locations (1)Reviewed by Cursor Bugbot for commit 472e0fb. Configure here. |
||
| } catch (RejectedExecutionException e) { | ||
| options | ||
| .getLogger() | ||
| .log(SentryLevel.WARNING, "Failed to schedule rate limit lifted notification.", e); | ||
| } | ||
|
|
||
| timer.schedule( | ||
| new TimerTask() { | ||
| @Override | ||
| public void run() { | ||
| notifyRateLimitObservers(); | ||
| } | ||
| }, | ||
| date); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -364,11 +373,11 @@ public void removeRateLimitObserver(@NotNull final IRateLimitObserver observer) | |
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| try (final @NotNull ISentryLifecycleToken ignored = timerLock.acquire()) { | ||
| if (timer != null) { | ||
| timer.cancel(); | ||
| timer = null; | ||
| try (final @NotNull ISentryLifecycleToken ignored = notifyFuturesLock.acquire()) { | ||
| for (Future<?> future : notifyObserversFutures) { | ||
| future.cancel(false); | ||
| } | ||
| notifyObserversFutures.clear(); | ||
| } | ||
| rateLimitObservers.clear(); | ||
| } | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: unsure if we need to protect this with checking forinstanceof NoOpSentryExecutorServiceto avoid allocating FutureTasks for nothing (isDonealso returnsfalsewhen no-op, so pruning wouldn't do anything), but we're not really doing that anywhere else I believe, so I'm fine with keeping it as-is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it is a good point but also quite an edge case. Since we aren't doing it anywhere else, I don't think we should do it here.