Skip to content

Commit

Permalink
fix concurrent writes to mutable keepalive set
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Aug 10, 2024
1 parent d566ee9 commit a249a95
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main/scala/KeepAlive.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lila.ws

import java.util.concurrent.atomic.AtomicReference
import org.apache.pekko.actor.typed.Scheduler

import ipc.*
Expand Down Expand Up @@ -32,11 +33,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))

0 comments on commit a249a95

Please sign in to comment.