mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 14:38:49 +00:00
usermanager cleanup
This commit is contained in:
5 files changed
+139
-135
No files matched your search
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* CarbonChat
|
||||||
|
*
|
||||||
|
* Copyright (c) 2023 Josua Parks (Vicarious)
|
||||||
|
* Contributors
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package net.draycia.carbon.common.users;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||||
|
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
|
|
||||||
|
@DefaultQualifier(NonNull.class)
|
||||||
|
public abstract class CachingUserManager<C extends CarbonPlayer> implements UserManagerInternal<C> {
|
||||||
|
|
||||||
|
protected final Logger logger;
|
||||||
|
protected final ExecutorService executor;
|
||||||
|
protected final ReentrantLock cacheLock;
|
||||||
|
protected final Map<UUID, CompletableFuture<C>> cache;
|
||||||
|
|
||||||
|
protected CachingUserManager(final Logger logger, final ExecutorService executorService) {
|
||||||
|
this.logger = logger;
|
||||||
|
this.executor = executorService;
|
||||||
|
this.cacheLock = new ReentrantLock();
|
||||||
|
this.cache = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdown() {
|
||||||
|
this.cacheLock.lock();
|
||||||
|
try {
|
||||||
|
final Map<UUID, CompletableFuture<Void>> collect = List.copyOf(this.cache.keySet()).stream()
|
||||||
|
.collect(Collectors.toMap(Function.identity(), this::loggedOut));
|
||||||
|
for (final Map.Entry<UUID, CompletableFuture<Void>> entry : collect.entrySet()) {
|
||||||
|
try {
|
||||||
|
entry.getValue().join();
|
||||||
|
} catch (final Exception ex) {
|
||||||
|
this.logger.warn("Exception saving data for player with uuid " + entry.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ConcurrentUtil.shutdownExecutor(this.executor, TimeUnit.MILLISECONDS, 500);
|
||||||
|
} finally {
|
||||||
|
this.cacheLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Void> loggedOut(final UUID uuid) {
|
||||||
|
this.cacheLock.lock();
|
||||||
|
try {
|
||||||
|
final @Nullable CompletableFuture<C> remove = this.cache.remove(uuid);
|
||||||
|
if (remove != null && remove.isDone()) { // don't need to save if it never finished loading
|
||||||
|
final @Nullable C join = remove.join();
|
||||||
|
if (join != null) {
|
||||||
|
return this.save(join);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return CompletableFuture.completedFuture(null);
|
||||||
|
} finally {
|
||||||
|
this.cacheLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't keep failed requests, so they can be retried on the next request
|
||||||
|
// The caller is expected to handle the error
|
||||||
|
protected void attachPostLoad(final UUID uuid, final CompletableFuture<CarbonPlayerCommon> future) {
|
||||||
|
future.whenComplete((result, thr) -> {
|
||||||
|
if (result == null || thr != null) {
|
||||||
|
this.cacheLock.lock();
|
||||||
|
try {
|
||||||
|
this.cache.remove(uuid);
|
||||||
|
} finally {
|
||||||
|
this.cacheLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -19,17 +19,12 @@
|
|||||||
*/
|
*/
|
||||||
package net.draycia.carbon.common.users.db;
|
package net.draycia.carbon.common.users.db;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import net.draycia.carbon.common.users.CachingUserManager;
|
||||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
import net.draycia.carbon.common.users.ProfileResolver;
|
||||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@@ -40,24 +35,21 @@ import org.jdbi.v3.core.statement.PreparedBatch;
|
|||||||
import org.jdbi.v3.core.statement.Update;
|
import org.jdbi.v3.core.statement.Update;
|
||||||
|
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public abstract class DatabaseUserManager implements UserManagerInternal<CarbonPlayerCommon> {
|
public abstract class DatabaseUserManager extends CachingUserManager<CarbonPlayerCommon> {
|
||||||
|
|
||||||
protected final Jdbi jdbi;
|
protected final Jdbi jdbi;
|
||||||
|
|
||||||
protected final Map<UUID, CarbonPlayerCommon> userCache = Collections.synchronizedMap(new HashMap<>());
|
|
||||||
protected final QueriesLocator locator;
|
protected final QueriesLocator locator;
|
||||||
protected final ExecutorService executor;
|
protected final ProfileResolver profileResolver;
|
||||||
private final Logger logger;
|
|
||||||
|
|
||||||
protected DatabaseUserManager(final Jdbi jdbi, final QueriesLocator locator, final Logger logger) {
|
protected DatabaseUserManager(final Jdbi jdbi, final QueriesLocator locator, final Logger logger, final ProfileResolver profileResolver) {
|
||||||
|
super(logger, Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "DatabaseUserManager")));
|
||||||
this.jdbi = jdbi;
|
this.jdbi = jdbi;
|
||||||
this.locator = locator;
|
this.locator = locator;
|
||||||
this.executor = Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "DatabaseUserManager"));
|
this.profileResolver = profileResolver;
|
||||||
this.logger = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
final public CompletableFuture<Void> save(final CarbonPlayerCommon player) {
|
public final CompletableFuture<Void> save(final CarbonPlayerCommon player) {
|
||||||
return CompletableFuture.runAsync(() -> this.jdbi.withHandle(handle -> {
|
return CompletableFuture.runAsync(() -> this.jdbi.withHandle(handle -> {
|
||||||
if (!this.modifiedPlayerObject(player)) {
|
if (!this.modifiedPlayerObject(player)) {
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
@@ -98,16 +90,4 @@ public abstract class DatabaseUserManager implements UserManagerInternal<CarbonP
|
|||||||
|
|
||||||
abstract protected Update bindPlayerArguments(final Update update, final CarbonPlayerCommon player);
|
abstract protected Update bindPlayerArguments(final Update update, final CarbonPlayerCommon player);
|
||||||
|
|
||||||
@Override
|
|
||||||
public void shutdown() {
|
|
||||||
for (final UUID id : List.copyOf(this.userCache.keySet())) {
|
|
||||||
try {
|
|
||||||
this.loggedOut(id).join();
|
|
||||||
} catch (final Exception ex) {
|
|
||||||
this.logger.warn("Exception saving data for player with uuid " + id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ConcurrentUtil.shutdownExecutor(this.executor, TimeUnit.MILLISECONDS, 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+8
-29
@@ -21,8 +21,6 @@ package net.draycia.carbon.common.users.db.mysql;
|
|||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -54,12 +52,8 @@ import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
|||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public final class MySQLUserManager extends DatabaseUserManager {
|
public final class MySQLUserManager extends DatabaseUserManager {
|
||||||
|
|
||||||
private final ProfileResolver profileResolver;
|
|
||||||
private final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache = new HashMap<>();
|
|
||||||
|
|
||||||
private MySQLUserManager(final Jdbi jdbi, final Logger logger, final ProfileResolver profileResolver) {
|
private MySQLUserManager(final Jdbi jdbi, final Logger logger, final ProfileResolver profileResolver) {
|
||||||
super(jdbi, new QueriesLocator(DBType.MYSQL), logger);
|
super(jdbi, new QueriesLocator(DBType.MYSQL), logger, profileResolver);
|
||||||
this.profileResolver = profileResolver;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MySQLUserManager manager(
|
public static MySQLUserManager manager(
|
||||||
@@ -107,10 +101,11 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
||||||
|
this.cacheLock.lock();
|
||||||
|
try {
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
return this.cache.computeIfAbsent(uuid, $ -> {
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
||||||
return this.userCache.computeIfAbsent(uuid, key -> {
|
|
||||||
return this.jdbi.withHandle(handle -> {
|
return this.jdbi.withHandle(handle -> {
|
||||||
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||||
.bind("id", uuid)
|
.bind("id", uuid)
|
||||||
@@ -145,21 +140,15 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
});
|
});
|
||||||
return carbonPlayerCommon.get();
|
return carbonPlayerCommon.get();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}, this.executor);
|
}, this.executor);
|
||||||
|
|
||||||
// Don't keep failed requests so they can be retried on the next request
|
this.attachPostLoad(uuid, future);
|
||||||
// The caller is expected to handle the error
|
|
||||||
future.whenComplete((result, $$$) -> {
|
|
||||||
if (result == null) {
|
|
||||||
synchronized (this) {
|
|
||||||
this.cache.remove(uuid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return future;
|
return future;
|
||||||
});
|
});
|
||||||
|
} finally {
|
||||||
|
this.cacheLock.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -176,14 +165,4 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
.bind("spying", player.spying());
|
.bind("spying", player.spying());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletableFuture<Void> loggedOut(final UUID uuid) {
|
|
||||||
final CompletableFuture<@Nullable CarbonPlayerCommon> remove = this.cache.remove(uuid);
|
|
||||||
final @Nullable CarbonPlayerCommon join = remove.join();
|
|
||||||
if (remove.isDone() && join != null) { // don't need to save if it never finished loading
|
|
||||||
return this.save(join);
|
|
||||||
}
|
|
||||||
return CompletableFuture.completedFuture(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+8
-33
@@ -21,8 +21,6 @@ package net.draycia.carbon.common.users.db.postgresql;
|
|||||||
|
|
||||||
import com.zaxxer.hikari.HikariConfig;
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -55,12 +53,8 @@ import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
|||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public final class PostgreSQLUserManager extends DatabaseUserManager {
|
public final class PostgreSQLUserManager extends DatabaseUserManager {
|
||||||
|
|
||||||
private final ProfileResolver profileResolver;
|
|
||||||
private final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache = new HashMap<>();
|
|
||||||
|
|
||||||
private PostgreSQLUserManager(final Jdbi jdbi, final Logger logger, final ProfileResolver profileResolver) {
|
private PostgreSQLUserManager(final Jdbi jdbi, final Logger logger, final ProfileResolver profileResolver) {
|
||||||
super(jdbi, new QueriesLocator(DBType.POSTGRESQL), logger);
|
super(jdbi, new QueriesLocator(DBType.POSTGRESQL), logger, profileResolver);
|
||||||
this.profileResolver = profileResolver;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PostgreSQLUserManager manager(
|
public static PostgreSQLUserManager manager(
|
||||||
@@ -104,15 +98,11 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
||||||
|
this.cacheLock.lock();
|
||||||
|
try {
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
return this.cache.computeIfAbsent(uuid, $ -> {
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
||||||
final @Nullable CarbonPlayerCommon cachedPlayer = this.userCache.get(uuid);
|
|
||||||
|
|
||||||
if (cachedPlayer != null) {
|
|
||||||
return cachedPlayer;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.jdbi.withHandle(handle -> {
|
return this.jdbi.withHandle(handle -> {
|
||||||
try {
|
try {
|
||||||
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||||
@@ -153,18 +143,13 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
});
|
});
|
||||||
}, this.executor);
|
}, this.executor);
|
||||||
|
|
||||||
// Don't keep failed requests so they can be retried on the next request
|
this.attachPostLoad(uuid, future);
|
||||||
// The caller is expected to handle the error
|
|
||||||
future.whenComplete((result, $$$) -> {
|
|
||||||
if (result == null) {
|
|
||||||
synchronized (this) {
|
|
||||||
this.cache.remove(uuid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return future;
|
return future;
|
||||||
});
|
});
|
||||||
|
} finally {
|
||||||
|
this.cacheLock.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -181,14 +166,4 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
.bind("spying", player.spying());
|
.bind("spying", player.spying());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletableFuture<Void> loggedOut(final UUID uuid) {
|
|
||||||
final CompletableFuture<@Nullable CarbonPlayerCommon> remove = this.cache.remove(uuid);
|
|
||||||
final @Nullable CarbonPlayerCommon join = remove.join();
|
|
||||||
if (remove.isDone() && join != null) { // don't need to save if it never finished loading
|
|
||||||
return this.save(join);
|
|
||||||
}
|
|
||||||
return CompletableFuture.completedFuture(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -27,22 +27,17 @@ import java.io.IOException;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import net.draycia.carbon.api.CarbonChatProvider;
|
import net.draycia.carbon.api.CarbonChatProvider;
|
||||||
import net.draycia.carbon.api.channels.ChatChannel;
|
import net.draycia.carbon.api.channels.ChatChannel;
|
||||||
import net.draycia.carbon.common.ForCarbon;
|
import net.draycia.carbon.common.ForCarbon;
|
||||||
import net.draycia.carbon.common.serialisation.gson.ChatChannelSerializerGson;
|
import net.draycia.carbon.common.serialisation.gson.ChatChannelSerializerGson;
|
||||||
import net.draycia.carbon.common.serialisation.gson.UUIDSerializerGson;
|
import net.draycia.carbon.common.serialisation.gson.UUIDSerializerGson;
|
||||||
|
import net.draycia.carbon.common.users.CachingUserManager;
|
||||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||||
import net.draycia.carbon.common.users.ProfileResolver;
|
import net.draycia.carbon.common.users.ProfileResolver;
|
||||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
|
||||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@@ -51,15 +46,12 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
|||||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
|
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
public class JSONUserManager implements UserManagerInternal<CarbonPlayerCommon> {
|
public class JSONUserManager extends CachingUserManager<CarbonPlayerCommon> {
|
||||||
|
|
||||||
private static final String EMPTY_USER_UUID = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6";
|
private static final String EMPTY_USER_UUID = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6";
|
||||||
private final Logger logger;
|
|
||||||
private final Gson serializer;
|
private final Gson serializer;
|
||||||
private final Path userDirectory;
|
private final Path userDirectory;
|
||||||
private final ProfileResolver profileResolver;
|
private final ProfileResolver profileResolver;
|
||||||
private final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache = new HashMap<>();
|
|
||||||
private final ExecutorService executor;
|
|
||||||
private final String emptyUserJson;
|
private final String emptyUserJson;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@@ -69,7 +61,7 @@ public class JSONUserManager implements UserManagerInternal<CarbonPlayerCommon>
|
|||||||
final Logger logger,
|
final Logger logger,
|
||||||
final ProfileResolver profileResolver
|
final ProfileResolver profileResolver
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
this.logger = logger;
|
super(logger, Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "JSONUserManager")));
|
||||||
this.userDirectory = dataDirectory.resolve("users");
|
this.userDirectory = dataDirectory.resolve("users");
|
||||||
this.profileResolver = profileResolver;
|
this.profileResolver = profileResolver;
|
||||||
|
|
||||||
@@ -83,12 +75,12 @@ public class JSONUserManager implements UserManagerInternal<CarbonPlayerCommon>
|
|||||||
.create();
|
.create();
|
||||||
|
|
||||||
this.emptyUserJson = this.serializer.toJson(new CarbonPlayerCommon("username", UUID.fromString(EMPTY_USER_UUID)));
|
this.emptyUserJson = this.serializer.toJson(new CarbonPlayerCommon("username", UUID.fromString(EMPTY_USER_UUID)));
|
||||||
|
|
||||||
this.executor = Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "JSONUserManager"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
||||||
|
this.cacheLock.lock();
|
||||||
|
try {
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
return this.cache.computeIfAbsent(uuid, $ -> {
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
||||||
final Path userFile = this.userFile(uuid);
|
final Path userFile = this.userFile(uuid);
|
||||||
@@ -117,18 +109,13 @@ public class JSONUserManager implements UserManagerInternal<CarbonPlayerCommon>
|
|||||||
return player;
|
return player;
|
||||||
}, this.executor);
|
}, this.executor);
|
||||||
|
|
||||||
// Don't keep failed requests so they can be retried on the next request
|
this.attachPostLoad(uuid, future);
|
||||||
// The caller is expected to handle the error
|
|
||||||
future.whenComplete((result, $$$) -> {
|
|
||||||
if (result == null) {
|
|
||||||
synchronized (this) {
|
|
||||||
this.cache.remove(uuid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return future;
|
return future;
|
||||||
});
|
});
|
||||||
|
} finally {
|
||||||
|
this.cacheLock.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path userFile(final UUID id) {
|
private Path userFile(final UUID id) {
|
||||||
@@ -164,26 +151,4 @@ public class JSONUserManager implements UserManagerInternal<CarbonPlayerCommon>
|
|||||||
}, this.executor);
|
}, this.executor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void shutdown() {
|
|
||||||
for (final UUID id : List.copyOf(this.cache.keySet())) {
|
|
||||||
try {
|
|
||||||
this.loggedOut(id).join();
|
|
||||||
} catch (final Exception ex) {
|
|
||||||
this.logger.warn("Exception saving data for player with uuid " + id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ConcurrentUtil.shutdownExecutor(this.executor, TimeUnit.MILLISECONDS, 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized CompletableFuture<Void> loggedOut(final UUID uuid) {
|
|
||||||
final CompletableFuture<@Nullable CarbonPlayerCommon> remove = this.cache.remove(uuid);
|
|
||||||
final @Nullable CarbonPlayerCommon join = remove.join();
|
|
||||||
if (remove.isDone() && join != null) { // don't need to save if it never finished loading
|
|
||||||
return this.save(join);
|
|
||||||
}
|
|
||||||
return CompletableFuture.completedFuture(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user