configurable filtering expression evaluation timeout

This commit is contained in:
Athou
2024-08-17 23:26:42 +02:00
parent 3627ee369d
commit ede7834cb8
3 changed files with 18 additions and 2 deletions

View File

@@ -167,6 +167,12 @@ public interface CommaFeedConfiguration {
*/ */
@WithDefault("0") @WithDefault("0")
Duration userInactivityPeriod(); Duration userInactivityPeriod();
/**
* Duration after which the evaluation of a filtering expresion to mark an entry as read is considered to have timed out.
*/
@WithDefault("500ms")
Duration filteringExpressionEvaluationTimeout();
} }
interface Database { interface Database {

View File

@@ -23,6 +23,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.model.FeedEntry; import com.commafeed.backend.model.FeedEntry;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
@@ -35,6 +36,7 @@ public class FeedEntryFilteringService {
private static final JexlEngine ENGINE = initEngine(); private static final JexlEngine ENGINE = initEngine();
private final ExecutorService executor = Executors.newCachedThreadPool(); private final ExecutorService executor = Executors.newCachedThreadPool();
private final CommaFeedConfiguration config;
private static JexlEngine initEngine() { private static JexlEngine initEngine() {
// classloader that prevents object creation // classloader that prevents object creation
@@ -96,8 +98,9 @@ public class FeedEntryFilteringService {
Future<Object> future = executor.submit(callable); Future<Object> future = executor.submit(callable);
Object result; Object result;
try { try {
result = future.get(500, TimeUnit.MILLISECONDS); result = future.get(config.feedRefresh().filteringExpressionEvaluationTimeout().toMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new FeedEntryFilterException("interrupted while evaluating expression " + filter, e); throw new FeedEntryFilterException("interrupted while evaluating expression " + filter, e);
} catch (ExecutionException e) { } catch (ExecutionException e) {
throw new FeedEntryFilterException("Exception while evaluating expression " + filter, e); throw new FeedEntryFilterException("Exception while evaluating expression " + filter, e);

View File

@@ -1,9 +1,13 @@
package com.commafeed.backend.service; package com.commafeed.backend.service;
import java.time.Duration;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.model.FeedEntry; import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntryContent; import com.commafeed.backend.model.FeedEntryContent;
import com.commafeed.backend.service.FeedEntryFilteringService.FeedEntryFilterException; import com.commafeed.backend.service.FeedEntryFilteringService.FeedEntryFilterException;
@@ -16,7 +20,10 @@ class FeedEntryFilteringServiceTest {
@BeforeEach @BeforeEach
public void init() { public void init() {
service = new FeedEntryFilteringService(); CommaFeedConfiguration config = Mockito.mock(CommaFeedConfiguration.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(config.feedRefresh().filteringExpressionEvaluationTimeout()).thenReturn(Duration.ofSeconds(2));
service = new FeedEntryFilteringService(config);
entry = new FeedEntry(); entry = new FeedEntry();
entry.setUrl("https://github.com/Athou/commafeed"); entry.setUrl("https://github.com/Athou/commafeed");