Make GET /dumps' background thread use the Vert.x scheduler instead of being a standalone thread
This commit is contained in:
parent
43b59dcf2c
commit
66b2fc5951
@ -4,7 +4,7 @@ import com.google.common.collect.ImmutableList;
|
|||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public final class MaxMindResult {
|
public final class MaxMindResult {
|
||||||
@ -29,7 +29,7 @@ public final class MaxMindResult {
|
|||||||
this.country = new MaxMindCountry(legacy.getJsonObject("country"));
|
this.country = new MaxMindCountry(legacy.getJsonObject("country"));
|
||||||
this.registeredCountry = new MaxMindRegisteredCountry(legacy.getJsonObject("registered_country"));
|
this.registeredCountry = new MaxMindRegisteredCountry(legacy.getJsonObject("registered_country"));
|
||||||
|
|
||||||
List<MaxMindSubdivision> subdivisions = new ArrayList<>();
|
List<MaxMindSubdivision> subdivisions = new LinkedList<>();
|
||||||
|
|
||||||
for (Object subdivision : legacy.getJsonArray("subdivisions")) {
|
for (Object subdivision : legacy.getJsonArray("subdivisions")) {
|
||||||
subdivisions.add(new MaxMindSubdivision((JsonObject) subdivision));
|
subdivisions.add(new MaxMindSubdivision((JsonObject) subdivision));
|
||||||
|
@ -14,7 +14,7 @@ import org.bson.Document;
|
|||||||
import org.bson.types.ObjectId;
|
import org.bson.types.ObjectId;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@ -34,11 +34,11 @@ public final class AuditLogEntry {
|
|||||||
@Getter private Map<String, Object> metadata;
|
@Getter private Map<String, Object> metadata;
|
||||||
|
|
||||||
public static void findPaginated(Document query, int skip, int pageSize, SingleResultCallback<List<AuditLogEntry>> callback) {
|
public static void findPaginated(Document query, int skip, int pageSize, SingleResultCallback<List<AuditLogEntry>> callback) {
|
||||||
auditLogCollection.find().sort(new Document("performedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback);
|
auditLogCollection.find(query).sort(new Document("performedAt", -1)).skip(skip).limit(pageSize).into(new LinkedList<>(), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void find(Document query, SingleResultCallback<List<AuditLogEntry>> callback) {
|
public static void find(Document query, SingleResultCallback<List<AuditLogEntry>> callback) {
|
||||||
auditLogCollection.find(query).into(new ArrayList<>(), callback);
|
auditLogCollection.find(query).into(new LinkedList<>(), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AuditLogEntry() {} // For Jackson
|
public AuditLogEntry() {} // For Jackson
|
||||||
|
@ -12,7 +12,7 @@ import net.frozenorb.apiv3.util.MaxMindUtils;
|
|||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@ -26,7 +26,7 @@ public final class IpIntel {
|
|||||||
@Getter private MaxMindResult result;
|
@Getter private MaxMindResult result;
|
||||||
|
|
||||||
public static void findAll(SingleResultCallback<List<IpIntel>> callback) {
|
public static void findAll(SingleResultCallback<List<IpIntel>> callback) {
|
||||||
ipIntelCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback);
|
ipIntelCollection.find().sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void findById(String id, SingleResultCallback<IpIntel> callback) {
|
public static void findById(String id, SingleResultCallback<IpIntel> callback) {
|
||||||
|
@ -14,7 +14,7 @@ import org.bson.Document;
|
|||||||
import org.bson.types.ObjectId;
|
import org.bson.types.ObjectId;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ public final class IpLogEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void findAll(SingleResultCallback<List<IpLogEntry>> callback) {
|
public static void findAll(SingleResultCallback<List<IpLogEntry>> callback) {
|
||||||
ipLogCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback);
|
ipLogCollection.find().sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void findById(String id, SingleResultCallback<IpLogEntry> callback) {
|
public static void findById(String id, SingleResultCallback<IpLogEntry> callback) {
|
||||||
@ -68,7 +68,7 @@ public final class IpLogEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void findByUser(UUID user, SingleResultCallback<List<IpLogEntry>> callback) {
|
public static void findByUser(UUID user, SingleResultCallback<List<IpLogEntry>> callback) {
|
||||||
ipLogCollection.find(new Document("user", user)).sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback);
|
ipLogCollection.find(new Document("user", user)).sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void findByUserAndIp(User user, String userIp, SingleResultCallback<IpLogEntry> callback) {
|
public static void findByUserAndIp(User user, String userIp, SingleResultCallback<IpLogEntry> callback) {
|
||||||
|
@ -12,7 +12,7 @@ import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
|||||||
import net.frozenorb.apiv3.util.SyncUtils;
|
import net.frozenorb.apiv3.util.SyncUtils;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ public final class NotificationTemplate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void findAll(SingleResultCallback<List<NotificationTemplate>> callback) {
|
public static void findAll(SingleResultCallback<List<NotificationTemplate>> callback) {
|
||||||
notificationTemplatesCollection.find().into(new ArrayList<>(), callback);
|
notificationTemplatesCollection.find().into(new LinkedList<>(), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void findById(String id, SingleResultCallback<NotificationTemplate> callback) {
|
public static void findById(String id, SingleResultCallback<NotificationTemplate> callback) {
|
||||||
|
@ -12,8 +12,8 @@ import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
|||||||
import net.frozenorb.apiv3.util.SyncUtils;
|
import net.frozenorb.apiv3.util.SyncUtils;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@ -58,7 +58,7 @@ public final class Rank {
|
|||||||
private static void updateCacheIfNeeded() {
|
private static void updateCacheIfNeeded() {
|
||||||
if (rankCache == null || (System.currentTimeMillis() - rankCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
if (rankCache == null || (System.currentTimeMillis() - rankCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
||||||
Map<String, Rank> working = new HashMap<>();
|
Map<String, Rank> working = new HashMap<>();
|
||||||
List<Rank> workingAlt = new ArrayList<>();
|
List<Rank> workingAlt = new LinkedList<>();
|
||||||
List<Rank> allRanks = SyncUtils.blockMulti(ranksCollection.find());
|
List<Rank> allRanks = SyncUtils.blockMulti(ranksCollection.find());
|
||||||
allRanks.sort((a, b) -> Ints.compare(a.getWeight(), b.getWeight()));
|
allRanks.sort((a, b) -> Ints.compare(a.getWeight(), b.getWeight()));
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ public final class Server {
|
|||||||
private static void updateCacheIfNeeded() {
|
private static void updateCacheIfNeeded() {
|
||||||
if (serverCache == null || (System.currentTimeMillis() - serverCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
if (serverCache == null || (System.currentTimeMillis() - serverCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
||||||
Map<String, Server> working = new HashMap<>();
|
Map<String, Server> working = new HashMap<>();
|
||||||
List<Server> workingAlt = new ArrayList<>();
|
List<Server> workingAlt = new LinkedList<>();
|
||||||
|
|
||||||
for (Server server : SyncUtils.blockMulti(serversCollection.find())) {
|
for (Server server : SyncUtils.blockMulti(serversCollection.find())) {
|
||||||
working.put(server.getId(), server);
|
working.put(server.getId(), server);
|
||||||
|
@ -57,7 +57,7 @@ public final class ServerGroup {
|
|||||||
private static void updateCacheIfNeeded() {
|
private static void updateCacheIfNeeded() {
|
||||||
if (serverGroupCache == null || (System.currentTimeMillis() - serverGroupCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
if (serverGroupCache == null || (System.currentTimeMillis() - serverGroupCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
|
||||||
Map<String, ServerGroup> working = new HashMap<>();
|
Map<String, ServerGroup> working = new HashMap<>();
|
||||||
List<ServerGroup> workingAlt = new ArrayList<>();
|
List<ServerGroup> workingAlt = new LinkedList<>();
|
||||||
|
|
||||||
for (ServerGroup serverGroup : SyncUtils.blockMulti(serverGroupsCollection.find())) {
|
for (ServerGroup serverGroup : SyncUtils.blockMulti(serverGroupsCollection.find())) {
|
||||||
working.put(serverGroup.getId(), serverGroup);
|
working.put(serverGroup.getId(), serverGroup);
|
||||||
|
@ -99,7 +99,7 @@ public final class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void findByIdGrouped(Iterable<UUID> search, SingleResultCallback<Map<UUID, User>> callback) {
|
public static void findByIdGrouped(Iterable<UUID> search, SingleResultCallback<Map<UUID, User>> callback) {
|
||||||
usersCollection.find(new Document("_id", new Document("$in", search))).into(new ArrayList<>(), (users, error) -> {
|
usersCollection.find(new Document("_id", new Document("$in", search))).into(new LinkedList<>(), (users, error) -> {
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
callback.onResult(null, error);
|
callback.onResult(null, error);
|
||||||
} else {
|
} else {
|
||||||
|
@ -13,76 +13,66 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
public final class GETDumps implements Handler<RoutingContext> {
|
public final class GETDumps implements Handler<RoutingContext> {
|
||||||
|
|
||||||
private List<UUID> banCache = new ArrayList<>();
|
private List<UUID> banCache = new LinkedList<>();
|
||||||
private List<UUID> blacklistCache = new ArrayList<>();
|
private List<UUID> blacklistCache = new LinkedList<>();
|
||||||
private Map<String, List<UUID>> grantCache = new HashMap<>();
|
private Map<String, List<UUID>> grantCache = new HashMap<>();
|
||||||
|
|
||||||
public GETDumps() {
|
public GETDumps() {
|
||||||
// TODO: Use vertx scheduler
|
APIv3.getVertxInstance().setPeriodic(TimeUnit.MINUTES.toMillis(5), (id) -> {
|
||||||
Thread dumpUpdater = new Thread() {
|
Punishment.findByType(ImmutableSet.of(
|
||||||
|
Punishment.PunishmentType.BAN,
|
||||||
@Override
|
Punishment.PunishmentType.BLACKLIST
|
||||||
public void run() {
|
), (punishments, error) -> {
|
||||||
int tick = 0;
|
if (error != null) {
|
||||||
|
error.printStackTrace();
|
||||||
while (true) {
|
return;
|
||||||
if (tick == 0 || tick % 2 != 0) {
|
|
||||||
List<UUID> banCache = new ArrayList<>();
|
|
||||||
List<UUID> blacklistCache = new ArrayList<>();
|
|
||||||
|
|
||||||
Punishment.findByTypeSync(ImmutableSet.of(
|
|
||||||
Punishment.PunishmentType.BAN,
|
|
||||||
Punishment.PunishmentType.BLACKLIST
|
|
||||||
)).forEach((punishment) -> {
|
|
||||||
if (!punishment.isActive()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (punishment.getType() == Punishment.PunishmentType.BAN) {
|
|
||||||
banCache.add(punishment.getUser());
|
|
||||||
} else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
|
|
||||||
blacklistCache.add(punishment.getUser());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
GETDumps.this.banCache = banCache;
|
|
||||||
GETDumps.this.blacklistCache = blacklistCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tick == 0 || tick % 2 == 0) {
|
|
||||||
Map<String, List<UUID>> grantCache = new HashMap<>();
|
|
||||||
|
|
||||||
Grant.findAllSync().forEach((grant) -> {
|
|
||||||
if (grant.isActive()) {
|
|
||||||
List<UUID> users = grantCache.get(grant.getRank());
|
|
||||||
|
|
||||||
if (users == null) {
|
|
||||||
users = new ArrayList<>();
|
|
||||||
grantCache.put(grant.getRank(), users);
|
|
||||||
}
|
|
||||||
|
|
||||||
users.add(grant.getUser());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
GETDumps.this.grantCache = grantCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(TimeUnit.MINUTES.toMillis(3));
|
|
||||||
} catch (InterruptedException ex) {
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
tick++;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
};
|
List<UUID> banCache = new LinkedList<>();
|
||||||
|
List<UUID> blacklistCache = new LinkedList<>();
|
||||||
|
|
||||||
dumpUpdater.setName("APIv3 - Dump Updater");
|
for (Punishment punishment : punishments) {
|
||||||
dumpUpdater.setDaemon(true);
|
if (!punishment.isActive()) {
|
||||||
dumpUpdater.start();
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (punishment.getType() == Punishment.PunishmentType.BAN) {
|
||||||
|
banCache.add(punishment.getUser());
|
||||||
|
} else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
|
||||||
|
blacklistCache.add(punishment.getUser());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GETDumps.this.banCache = banCache;
|
||||||
|
GETDumps.this.blacklistCache = blacklistCache;
|
||||||
|
});
|
||||||
|
|
||||||
|
Grant.findAll((grants, error) -> {
|
||||||
|
if (error != null) {
|
||||||
|
error.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, List<UUID>> grantCache = new HashMap<>();
|
||||||
|
|
||||||
|
for (Grant grant : grants) {
|
||||||
|
if (!grant.isActive()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<UUID> users = grantCache.get(grant.getRank());
|
||||||
|
|
||||||
|
if (users == null) {
|
||||||
|
users = new LinkedList<>();
|
||||||
|
grantCache.put(grant.getRank(), users);
|
||||||
|
}
|
||||||
|
|
||||||
|
users.add(grant.getUser());
|
||||||
|
}
|
||||||
|
|
||||||
|
GETDumps.this.grantCache = grantCache;
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handle(RoutingContext ctx) {
|
public void handle(RoutingContext ctx) {
|
||||||
@ -96,7 +86,7 @@ public final class GETDumps implements Handler<RoutingContext> {
|
|||||||
APIv3.respondJson(ctx, blacklistCache);
|
APIv3.respondJson(ctx, blacklistCache);
|
||||||
return;
|
return;
|
||||||
case "accessdeniable": // Lowercase d because we convert to lowercase above
|
case "accessdeniable": // Lowercase d because we convert to lowercase above
|
||||||
List<UUID> result = new ArrayList<>();
|
List<UUID> result = new LinkedList<>();
|
||||||
|
|
||||||
result.addAll(banCache);
|
result.addAll(banCache);
|
||||||
result.addAll(blacklistCache);
|
result.addAll(blacklistCache);
|
||||||
|
@ -4,7 +4,7 @@ import com.mongodb.async.client.MongoIterable;
|
|||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
@ -20,7 +20,7 @@ public class SyncUtils {
|
|||||||
public static <T> List<T> blockMulti(MongoIterable<T> mongoIterable) {
|
public static <T> List<T> blockMulti(MongoIterable<T> mongoIterable) {
|
||||||
BlockingCallback<List<T>> callback = new BlockingCallback<>();
|
BlockingCallback<List<T>> callback = new BlockingCallback<>();
|
||||||
|
|
||||||
mongoIterable.into(new ArrayList<>(), callback);
|
mongoIterable.into(new LinkedList<>(), callback);
|
||||||
return callback.get();
|
return callback.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user