From e61e7f44b9ef0d6e76e059165bd2047722a57230 Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Sun, 18 Feb 2024 09:22:45 +0100 Subject: [PATCH] (blacklist) Delay startup of blacklist To help services start faster, the blacklist will no longer block until it's loaded. If such a behavior is desirable, a method was added to explicitly wait for the data. --- .../nu/marginalia/db/DomainBlacklist.java | 1 + .../nu/marginalia/db/DomainBlacklistImpl.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/code/common/db/src/main/java/nu/marginalia/db/DomainBlacklist.java b/code/common/db/src/main/java/nu/marginalia/db/DomainBlacklist.java index b8562e88..eb169d46 100644 --- a/code/common/db/src/main/java/nu/marginalia/db/DomainBlacklist.java +++ b/code/common/db/src/main/java/nu/marginalia/db/DomainBlacklist.java @@ -9,4 +9,5 @@ public interface DomainBlacklist { default TIntHashSet getSpamDomains() { return new TIntHashSet(); } + void waitUntilLoaded() throws InterruptedException; } diff --git a/code/common/db/src/main/java/nu/marginalia/db/DomainBlacklistImpl.java b/code/common/db/src/main/java/nu/marginalia/db/DomainBlacklistImpl.java index 662b5344..a625554f 100644 --- a/code/common/db/src/main/java/nu/marginalia/db/DomainBlacklistImpl.java +++ b/code/common/db/src/main/java/nu/marginalia/db/DomainBlacklistImpl.java @@ -12,11 +12,13 @@ import java.util.concurrent.TimeUnit; @Singleton public class DomainBlacklistImpl implements DomainBlacklist { - private volatile TIntHashSet spamDomainSet = new TIntHashSet(); - private final HikariDataSource dataSource; - private final Logger logger = LoggerFactory.getLogger(getClass()); private final boolean blacklistDisabled = Boolean.getBoolean("blacklist.disable"); + private final HikariDataSource dataSource; + private final Logger logger = LoggerFactory.getLogger(getClass()); + + + private volatile TIntHashSet spamDomainSet = new TIntHashSet(); private volatile boolean isLoaded = false; @Inject @@ -63,20 +65,25 @@ public class DomainBlacklistImpl implements DomainBlacklist { } } + + /** Block until the blacklist has been loaded */ - public boolean waitUntilLoaded() throws InterruptedException { + @Override + public void waitUntilLoaded() throws InterruptedException { + if (blacklistDisabled) + return; + if (!isLoaded) { + logger.info("Waiting for blacklist to be loaded"); synchronized (this) { while (!isLoaded) { wait(5000); } } + logger.info("Blacklist loaded, size = {}", spamDomainSet.size()); } - - return true; } - public TIntHashSet getSpamDomains() { final TIntHashSet result = new TIntHashSet(1_000_000);