Skip to content

Commit 740abb1

Browse files
authored
[Logs 6] Reduce amount of locking on every created log event (#4380)
* Add Log feature to Java SDK * Rate limit for log items * Add options for logs * Add batch processor for logs * Use a separate ExecutorService for log batching * Reduce locking when log event is created
1 parent defba33 commit 740abb1

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

sentry/src/main/java/io/sentry/logger/LoggerBatchProcessor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class LoggerBatchProcessor implements ILoggerBatchProcessor {
2828
private volatile @Nullable Future<?> scheduledFlush;
2929
private static final @NotNull AutoClosableReentrantLock scheduleLock =
3030
new AutoClosableReentrantLock();
31+
private volatile boolean hasScheduled = false;
3132

3233
public LoggerBatchProcessor(
3334
final @NotNull SentryOptions options, final @NotNull ISentryClient client) {
@@ -58,12 +59,16 @@ public void close(final boolean isRestarting) {
5859
}
5960

6061
private void maybeSchedule(boolean forceSchedule, boolean immediately) {
62+
if (hasScheduled && !forceSchedule) {
63+
return;
64+
}
6165
try (final @NotNull ISentryLifecycleToken ignored = scheduleLock.acquire()) {
6266
final @Nullable Future<?> latestScheduledFlush = scheduledFlush;
6367
if (forceSchedule
6468
|| latestScheduledFlush == null
6569
|| latestScheduledFlush.isDone()
6670
|| latestScheduledFlush.isCancelled()) {
71+
hasScheduled = true;
6772
final int flushAfterMs = immediately ? 0 : FLUSH_AFTER_MS;
6873
scheduledFlush = executorService.schedule(new BatchRunnable(), flushAfterMs);
6974
}
@@ -75,6 +80,8 @@ private void flush() {
7580
try (final @NotNull ISentryLifecycleToken ignored = scheduleLock.acquire()) {
7681
if (!queue.isEmpty()) {
7782
maybeSchedule(true, false);
83+
} else {
84+
hasScheduled = false;
7885
}
7986
}
8087
}

0 commit comments

Comments
 (0)