(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.
This commit is contained in:
Viktor Lofgren 2024-02-18 09:22:45 +01:00
parent f9b6ac03c6
commit e61e7f44b9
2 changed files with 15 additions and 7 deletions

View File

@ -9,4 +9,5 @@ public interface DomainBlacklist {
default TIntHashSet getSpamDomains() {
return new TIntHashSet();
}
void waitUntilLoaded() throws InterruptedException;
}

View File

@ -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);