From c515a6b83f15f5fb97228ed316484859895c745c Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Sat, 10 Aug 2024 15:24:12 +0200 Subject: [PATCH] fix concurrent writes to mutable keepalive set --- src/main/scala/KeepAlive.scala | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/scala/KeepAlive.scala b/src/main/scala/KeepAlive.scala index 3c3df924..cba9b8d1 100644 --- a/src/main/scala/KeepAlive.scala +++ b/src/main/scala/KeepAlive.scala @@ -2,6 +2,8 @@ package lila.ws import org.apache.pekko.actor.typed.Scheduler +import java.util.concurrent.atomic.AtomicReference + import ipc.* final class KeepAlive(lila: Lila, scheduler: Scheduler)(using Executor): @@ -32,11 +34,8 @@ object KeepAlive: final class AliveRooms: - private val rooms = collection.mutable.Set[RoomId]() + private val rooms: AtomicReference[Set[RoomId]] = AtomicReference(Set.empty) - def apply(roomId: RoomId) = rooms += roomId + def apply(roomId: RoomId): Unit = rooms.getAndUpdate(_ + roomId) - def getAndClear: LilaIn.KeepAlives = - val ret = LilaIn.KeepAlives(rooms.toSet) - rooms.clear() - ret + def getAndClear: LilaIn.KeepAlives = LilaIn.KeepAlives(rooms.getAndUpdate(_ => Set.empty))