Skip to content

Commit

Permalink
Merge pull request #585 from lichess-org/keep-alive-rata-race
Browse files Browse the repository at this point in the history
fix concurrent writes to mutable keepalive set
  • Loading branch information
ornicar authored Aug 10, 2024
2 parents d566ee9 + c515a6b commit f611a09
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/main/scala/KeepAlive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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))

0 comments on commit f611a09

Please sign in to comment.