com.github.tomitakussaari.mysqlcluscon.ServerBlackList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of myscluscon-driver Show documentation
Show all versions of myscluscon-driver Show documentation
JDBC Driver for always connecting to valid server in your Mariadb/Mysql/Galera cluster
The newest version!
package com.github.tomitakussaari.mysqlcluscon;
import lombok.RequiredArgsConstructor;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@RequiredArgsConstructor
class ServerBlackList {
private static final long defaultBlackListTimeInMs = 2 * 60 * 1000;
private final Map serversAndBlackListTimes = new ConcurrentHashMap<>();
private final Supplier nowSupplier;
private final long blackListTimeInMs;
ServerBlackList() {
this(System::currentTimeMillis, defaultBlackListTimeInMs);
}
void blackList(String server) {
serversAndBlackListTimes.put(server, nowSupplier.get());
}
List withoutBlackListed(List allServers) {
purgeOldEntries();
return allServers.stream()
.filter(server -> !serversAndBlackListTimes.containsKey(server))
.collect(Collectors.toList());
}
private void purgeOldEntries() {
Set> blackListEntries = serversAndBlackListTimes.entrySet();
blackListEntries.stream()
.filter(entry -> entry.getValue() + blackListTimeInMs < nowSupplier.get())
.forEach(blackListEntries::remove);
}
Set blackListed() {
purgeOldEntries();
return serversAndBlackListTimes.keySet();
}
}