Skip to content

Commit

Permalink
Add batch processing of events from queue for improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
taldekar committed Jan 21, 2025
1 parent 2c3a4ca commit e8d7fb7
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package software.aws.toolkits.eclipse.amazonq.broker;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
Expand Down Expand Up @@ -63,6 +65,7 @@ public static final class OrderedThreadPoolExecutor {
private final BlockingQueue<Runnable> scheduledJobsQueue;
private final ThreadPoolExecutor executor;
private final int eventQueueCapacity;
private final static int BATCH_PROCESSING_QUEUE_SIZE = 250;

OrderedThreadPoolExecutor(final int coreThreadCount, final int maxThreadCount, final int jobQueueCapacity,
final int eventQueueCapacity, final int keepAliveTime, final TimeUnit keepAliveTimeUnit) {
Expand Down Expand Up @@ -116,15 +119,20 @@ private <T, R> void processQueuedEvents(final String interestId, final Class<R>
return;
}

List<R> batchedEvents = new ArrayList<>(BATCH_PROCESSING_QUEUE_SIZE);

while (!eventQueue.isEmpty()) {
R newEvent = eventQueue.poll();
if (newEvent != null) {
eventQueue.drainTo(batchedEvents);

for (R newEvent : batchedEvents) {
try {
eventCallback.callWith(newEvent);
} catch (Exception e) {
e.printStackTrace();
}
}

batchedEvents.clear();
}
} finally {
jobStatus.set(false);
Expand Down

0 comments on commit e8d7fb7

Please sign in to comment.