Persist first Lobby admission before queuing requests
This commit is contained in:
parent
426c6729c3
commit
22ec139d4a
5 changed files with 36 additions and 15 deletions
|
|
@ -1,6 +1,6 @@
|
|||
plugins { java }
|
||||
group = "com.dunerealms"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
|
|
|||
|
|
@ -50,6 +50,14 @@ final class AccountJournal {
|
|||
}
|
||||
}
|
||||
|
||||
Entry admit(UUID player, String name) throws IOException {
|
||||
Entry entry = read(player);
|
||||
if (entry != null) return entry;
|
||||
entry = new Entry(player, UUID.randomUUID(), name, Phase.PENDING);
|
||||
write(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
void write(Entry entry) throws IOException {
|
||||
if (!validName(entry.name())) throw new IOException("Invalid verified player name");
|
||||
Path tmp = Files.createTempFile(folder, ".receipt-", ".tmp", permissions("rw-------"));
|
||||
|
|
|
|||
|
|
@ -104,29 +104,31 @@ public final class DuneLobbyAccount extends JavaPlugin implements Listener {
|
|||
private void attempt(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
String liveName = player.getName();
|
||||
if (!running || !AccountJournal.validName(liveName) || finished.contains(uuid) || !active.add(uuid)) return;
|
||||
String admittedName = firstAdmissions.get(uuid);
|
||||
if (admittedName != null) {
|
||||
try {
|
||||
executor.execute(() -> process(player, uuid, liveName, admittedName));
|
||||
journal.admit(uuid, admittedName);
|
||||
firstAdmissions.remove(uuid);
|
||||
} catch (IOException unavailable) {
|
||||
return; // Retry the local receipt before admitting any network work.
|
||||
}
|
||||
}
|
||||
if (!running || !AccountJournal.validName(liveName) || finished.contains(uuid) || !active.add(uuid)) return;
|
||||
try {
|
||||
executor.execute(() -> process(player, uuid, liveName));
|
||||
} catch (RejectedExecutionException full) {
|
||||
active.remove(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
private void process(Player player, UUID uuid, String liveName, String admittedName) {
|
||||
private void process(Player player, UUID uuid, String liveName) {
|
||||
boolean terminal = false;
|
||||
boolean recorded = false;
|
||||
try {
|
||||
AccountJournal.Entry entry = journal.read(uuid);
|
||||
if (entry == null) {
|
||||
if (admittedName == null) {
|
||||
terminal = true;
|
||||
return;
|
||||
}
|
||||
entry = new AccountJournal.Entry(uuid, UUID.randomUUID(), admittedName, PENDING);
|
||||
journal.write(entry);
|
||||
}
|
||||
recorded = true;
|
||||
if (entry.phase() == DONE || entry.phase() == HELD) {
|
||||
terminal = true;
|
||||
return;
|
||||
|
|
@ -137,10 +139,9 @@ public final class DuneLobbyAccount extends JavaPlugin implements Listener {
|
|||
} catch (IOException | TimeoutException | ExecutionException unavailable) {
|
||||
// An uncertain response is retried using the same durable request on the next timer or join.
|
||||
} finally {
|
||||
boolean complete = terminal, hasReceipt = recorded;
|
||||
boolean complete = terminal;
|
||||
onMain(() -> {
|
||||
active.remove(uuid);
|
||||
if (hasReceipt) firstAdmissions.remove(uuid);
|
||||
if (complete) finished.add(uuid);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
name: DuneLobbyAccount
|
||||
version: 1.0.0
|
||||
version: 1.0.1
|
||||
main: com.dunerealms.lobbyaccount.DuneLobbyAccount
|
||||
api-version: '1.21.8'
|
||||
description: Provisions ordinary DuneRealms accounts from authenticated first Lobby joins
|
||||
|
|
|
|||
|
|
@ -32,6 +32,18 @@ class LobbyAccountContractTest {
|
|||
assertThrows(IOException.class, () -> restarted.write(new AccountJournal.Entry(request.player(), request.request(), "x\ny", PENDING)));
|
||||
}
|
||||
|
||||
@Test void aPendingFirstJoinCanBeRecoveredBeforeAnyWorkerRan(@TempDir Path temp) throws Exception {
|
||||
UUID player = request.player();
|
||||
var first = new AccountJournal(temp.resolve("receipts"));
|
||||
var admitted = first.admit(player, request.name());
|
||||
var restarted = new AccountJournal(temp.resolve("receipts"));
|
||||
assertEquals(admitted, restarted.read(player));
|
||||
assertEquals(PENDING, admitted.phase());
|
||||
assertEquals(admitted, restarted.admit(player, "Renamed"));
|
||||
restarted.write(admitted.advance(ACK));
|
||||
assertEquals(admitted.advance(ACK), first.admit(player, request.name()));
|
||||
}
|
||||
|
||||
@Test void requestAndAcknowledgementHaveExactWireFields() {
|
||||
JsonObject provision = LobbyAccountApi.request(request, false);
|
||||
assertEquals(Set.of("action", "schema", "requestId", "uuid", "name"), provision.keySet());
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff