Skip to content

Commit 97550ad

Browse files
committed
fix: safely publish table projections across threads
1 parent 0ade9e1 commit 97550ad

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

  • modules/mahjong-application/src/main/java/top/ellan/mahjong/application

‎modules/mahjong-application/src/main/java/top/ellan/mahjong/application/TableActor.java‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public final class TableActor implements AutoCloseable {
5757
private final AtomicBoolean scheduled = new AtomicBoolean();
5858
private final AtomicBoolean closed = new AtomicBoolean();
5959
private final AtomicReference<TableActorSnapshot> publishedSnapshot = new AtomicReference<>();
60+
private final AtomicReference<TableProjection> publishedProjection = new AtomicReference<>();
6061
private final CompletableFuture<Void> shutdownComplete = new CompletableFuture<>();
6162
private final Map<UUID, AuthorizedAction> actionCatalog = new HashMap<>();
6263
private TableAggregate aggregate;
6364
private RuleState ruleState;
64-
private TableProjection latestProjection;
6565
private OutboxHealth outboxHealth;
6666
private long lastEventSequence;
6767
private long acceptedActions;
@@ -130,7 +130,7 @@ public TableActorSnapshot snapshot() {
130130
}
131131

132132
public Optional<TableProjection> latestProjection() {
133-
return Optional.ofNullable(latestProjection);
133+
return Optional.ofNullable(publishedProjection.get());
134134
}
135135

136136
private void scheduleDrain() {
@@ -542,15 +542,16 @@ private void installFrame(RuleFrame frame) {
542542
}
543543
authorizedByPlayer.put(player, List.copyOf(authorized));
544544
});
545-
latestProjection =
545+
TableProjection projection =
546546
new TableProjection(
547547
aggregate.tableId(),
548548
aggregate.revision(),
549549
aggregate.lifecycle(),
550550
frame.publicView(),
551551
frame.privateViews(),
552552
authorizedByPlayer);
553-
publishProjection(latestProjection);
553+
publishedProjection.set(projection);
554+
publishProjection(projection);
554555
}
555556

556557
private void handleOutboxHealth(OutboxHealth health) {
@@ -565,18 +566,20 @@ private void handleOutboxHealth(OutboxHealth health) {
565566
}
566567

567568
private void republishLifecycle() {
568-
if (latestProjection == null) {
569+
TableProjection current = publishedProjection.get();
570+
if (current == null) {
569571
return;
570572
}
571-
latestProjection =
573+
TableProjection updated =
572574
new TableProjection(
573-
latestProjection.tableId(),
574-
latestProjection.revision(),
575+
current.tableId(),
576+
current.revision(),
575577
aggregate.lifecycle(),
576-
latestProjection.publicView(),
577-
latestProjection.privateViews(),
578-
latestProjection.authorizedActions());
579-
publishProjection(latestProjection);
578+
current.publicView(),
579+
current.privateViews(),
580+
current.authorizedActions());
581+
publishedProjection.set(updated);
582+
publishProjection(updated);
580583
}
581584

582585
private void publishProjection(TableProjection projection) {

0 commit comments

Comments
 (0)