Skip to content
Snippets Groups Projects
Commit c8be608f authored by Clément Pit-Claudel's avatar Clément Pit-Claudel
Browse files

server: Synchronize accesses to the `sessions` map

This is mostly a stopgap measure until we merge !25.
parent c8aea705
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,7 @@ import scala.util.{Success, Failure, Try}
/** A collection of websockets organized by app Id and client */
private[web] final class WebSocketsCollection():
// When the user joins the app, the projection of the current state is sent to them
private def onClientConnect(instanceId: InstanceId, userId: UserId): Unit =
private def onClientConnect(instanceId: InstanceId, userId: UserId): Unit = sessions.synchronized:
val instance = WebServer.apps(instanceId)
val newInstance = instance.copy(connectedUsersCount = instance.connectedUsersCount + 1)
WebServer.apps(instanceId) = newInstance
......@@ -16,7 +16,7 @@ private[web] final class WebSocketsCollection():
EventResponse.Wire.encode(Success(List(Action.Render(js))))
println(f"[${newInstance.instance.appInfo.id}][$instanceId] client \"$userId\" connected")
private def onClientDisconnect(instanceId: InstanceId, userId: UserId): Unit =
private def onClientDisconnect(instanceId: InstanceId, userId: UserId): Unit = sessions.synchronized:
val instance = WebServer.apps(instanceId)
val newInstance = instance.copy(connectedUsersCount = instance.connectedUsersCount - 1)
WebServer.apps(instanceId) = newInstance
......@@ -31,12 +31,12 @@ private[web] final class WebSocketsCollection():
mutable.Map()
/** Initialize an empty session list for the given app instance */
def initializeApp(instanceId: InstanceId) =
def initializeApp(instanceId: InstanceId) = sessions.synchronized:
require(!sessions.contains(instanceId))
sessions(instanceId) = Seq.empty
def connect(instanceId: String, userId: String)
(implicit cc: castor.Context, log: cask.Logger): cask.WebsocketResult =
(implicit cc: castor.Context, log: cask.Logger): cask.WebsocketResult = sessions.synchronized:
if WebServer.apps.contains(instanceId) then
cask.WsHandler: channel =>
sessions(instanceId) = sessions(instanceId) :+ (userId, channel)
......@@ -54,7 +54,7 @@ private[web] final class WebSocketsCollection():
cask.Response(f"Unknown instance id $instanceId", 400)
/** Enumerates clients connected to [[instanceId]]. */
def connectedClients(instanceId: InstanceId): Seq[UserId] =
def connectedClients(instanceId: InstanceId): Seq[UserId] = sessions.synchronized:
sessions.get(instanceId).map(_.map(_._1).distinct).getOrElse(Seq())
/** Sends a message to a specific client. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment