Skip to content
Snippets Groups Projects
Commit bc59ddaa authored by Clemens Klein-Robbenhaar's avatar Clemens Klein-Robbenhaar
Browse files

XWIKI-15927 - Recursivity of getEvents() could lead to stack overflow

- also stop fetching results after ten seconds.
  nobody will wait for that long, and it might stop
  creating more load on the server which is seemingly
  very slow already
parent 75e1a7d4
No related branches found
No related tags found
No related merge requests found
......@@ -61,6 +61,11 @@ public class DefaultParametrizedNotificationManager implements ParametrizedNotif
*/
private static final int MAX_BATCH_SIZE = 1280;
/**
* the maximal time in milliseconds to spend fetching a single batch.
*/
private static final long MAX_EVENT_FETCHING_TIME_MILLIS = 10000L;
@Inject
private EventStream eventStream;
......@@ -100,16 +105,17 @@ public List<CompositeEvent> getEvents(NotificationParameters parameters)
if (Boolean.TRUE.equals(parameters.onlyUnread) && !parameters.filters.contains(eventReadEmailFilter)) {
parameters.filters.add(eventReadEmailFilter);
}
return getEvents(new ArrayList<>(), parameters);
return getEventsInternal(parameters);
}
private List<CompositeEvent> getEvents(List<CompositeEvent> results, NotificationParameters parameters)
throws NotificationException
private List<CompositeEvent> getEventsInternal(NotificationParameters parameters) throws NotificationException
{
// Because the user might not be able to see all notifications because of the rights, we take from the database
// more events than expected and we will filter afterwards.
int batchSize = parameters.expectedCount * 2;
int offset = 0;
long deadline = System.currentTimeMillis() + MAX_EVENT_FETCHING_TIME_MILLIS;
List<CompositeEvent> results = new ArrayList<>();
try {
boolean done = false;
......@@ -126,8 +132,8 @@ private List<CompositeEvent> getEvents(List<CompositeEvent> results, Notificatio
done = addMatchingEventsToResults(batch, parameters, results);
if (!done) {
if (batch.size() < batchSize) {
// there are no more results to expect. stop.
if (batch.size() < batchSize || System.currentTimeMillis() >= deadline) {
// there are no more results to expect, or we needed too much time. stop.
done = true;
} else {
// grab a larger batch size next time to get more possible results
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment