cleaner socket member quit code

pull/4797/head
Thibault Duplessis 2018-12-16 17:43:05 +08:00
parent 3b4f455b84
commit 5d32199416
10 changed files with 33 additions and 38 deletions

View File

@ -39,8 +39,6 @@ private final class ChallengeSocket(
prependEventsSince(version, enumerator, member),
member
)
case Quit(uid) => quit(uid)
}
override protected def broom: Unit = {

View File

@ -20,14 +20,14 @@ trait Trouper extends lila.common.Tellable {
// implement async behaviour here
protected val process: Receive
private[this] var alive = true
protected var isAlive = true
def stop(): Unit = {
alive = false
isAlive = false
}
def !(msg: Any): Unit =
if (alive && stateRef.getAndUpdate(
if (isAlive && stateRef.getAndUpdate(
new UnaryOperator[State] {
override def apply(state: State): State = Some(state.fold(Queue.empty[Any])(_ enqueue msg))
}

View File

@ -151,8 +151,7 @@ private[lobby] final class LobbySocket(
private def withActiveMemberByUidString(uid: String)(f: Member => Unit): Unit =
if (!idleUids(uid)) members get uid foreach f
override def quit(uid: Socket.Uid): Unit = {
super.quit(uid)
override protected def afterQuit(uid: Socket.Uid, member: Member) = {
idleUids -= uid.value
hookSubscriberUids -= uid.value
}

View File

@ -16,8 +16,8 @@ import lila.game.{ Game, GameRepo, Event }
import lila.hub.actorApi.Deploy
import lila.hub.actorApi.game.ChangeFeatured
import lila.hub.actorApi.round.{ IsOnGame, TourStanding }
import lila.hub.actorApi.tv.{ Select => TvSelect }
import lila.hub.actorApi.simul.GetHostIds
import lila.hub.actorApi.tv.{ Select => TvSelect }
import lila.hub.Trouper
import lila.socket._
import lila.socket.actorApi.{ Connected => _, _ }
@ -254,13 +254,9 @@ private[round] final class RoundSocket(
}
}
override def quit(uid: Socket.Uid) =
if (members contains uid.value) {
super.quit(uid)
notifyCrowd
}
override protected def afterQuit(uid: Socket.Uid, member: Member) = notifyCrowd
def notifyCrowd: Unit = {
def notifyCrowd: Unit = if (isAlive) {
if (!delayedCrowdNotification) {
delayedCrowdNotification = true
system.scheduler.scheduleOnce(1 second)(this ! NotifyCrowd)

View File

@ -78,10 +78,6 @@ private[simul] final class Socket(
member
)
case Quit(uid) =>
quit(uid)
notifyCrowd
case NotifyCrowd =>
delayedCrowdNotification = false
showSpectators(lightUser)(members.values) foreach { notifyAll("crowd", _) }
@ -95,6 +91,8 @@ private[simul] final class Socket(
if (members.nonEmpty) keepMeAlive()
}
override protected def afterQuit(uid: lila.socket.Socket.Uid, member: Member) = notifyCrowd
def notifyCrowd: Unit =
if (!delayedCrowdNotification) {
delayedCrowdNotification = true

View File

@ -45,8 +45,7 @@ private[site] final class Socket(
lila.mon.socket.count.site(members.size)
}
override def quit(uid: Socket.Uid): Unit = {
members get uid.value foreach { flags.remove(uid, _) }
super.quit(uid)
override protected def afterQuit(uid: Socket.Uid, member: Member) = {
flags.remove(uid, member)
}
}

View File

@ -51,7 +51,7 @@ abstract class SocketTrouper[M <: SocketMember](
case Broom => broom
// when a member quits
case Quit(uid) => quit(uid)
case Quit(uid) => withMember(uid) { quit(uid, _) }
case Resync(uid) => resync(uid)
@ -89,16 +89,24 @@ abstract class SocketTrouper[M <: SocketMember](
protected def ejectUidString(uid: String): Unit = eject(Socket.Uid(uid))
protected def eject(uid: Socket.Uid): Unit = withMember(uid) { member =>
// actively boot a member, if it exists
// this function is called when a member joins,
// to prevent duplicate UID
private final def eject(uid: Socket.Uid): Unit = withMember(uid) { member =>
member.end
quit(uid)
quit(uid, member)
}
protected def quit(uid: Socket.Uid): Unit = withMember(uid) { member =>
// when a member quits, voluntarily or not
// at this point we know the member exists
private final def quit(uid: Socket.Uid, member: M): Unit = {
members -= uid.value
lilaBus.publish(SocketLeave(uid, member), 'socketLeave)
afterQuit(uid, member)
}
protected def afterQuit(uid: Socket.Uid, member: M): Unit = {}
protected def onDeploy(d: Deploy): Unit =
notifyAll(makeMessage(d.key))

View File

@ -9,8 +9,8 @@ case class Connected[M <: SocketMember](
)
case class BotConnected(color: chess.Color, v: Boolean)
case object Broom
case class Quit(uid: Socket.Uid)
private[socket] case object Broom
private[socket] case class Quit(uid: Socket.Uid)
case class SocketEnter[M <: SocketMember](uid: Socket.Uid, member: M)
case class SocketLeave[M <: SocketMember](uid: Socket.Uid, member: M)

View File

@ -207,12 +207,6 @@ final class StudySocket(
)
userId foreach sendStudyDoor(true)
case Quit(uid) => withMember(uid) { member =>
quit(uid)
member.userId foreach sendStudyDoor(false)
notifyCrowd
}
case NotifyCrowd =>
delayedCrowdNotification = false
val json =
@ -239,6 +233,11 @@ final class StudySocket(
if (members.nonEmpty) keepMeAlive()
}
override protected def afterQuit(uid: Uid, member: Member) = {
member.userId foreach sendStudyDoor(false)
notifyCrowd
}
private def notifyCrowd: Unit =
if (!delayedCrowdNotification) {
delayedCrowdNotification = true

View File

@ -11,7 +11,7 @@ import actorApi._
import lila.chat.Chat
import lila.hub.Trouper
import lila.socket.actorApi.{ Connected => _, _ }
import lila.socket.{ SocketTrouper, History, Historical }
import lila.socket.{ SocketTrouper, History, Historical, Socket }
private[tournament] final class TournamentSocket(
system: ActorSystem,
@ -75,10 +75,6 @@ private[tournament] final class TournamentSocket(
member
)
case Quit(uid) =>
quit(uid)
notifyCrowd
case NotifyCrowd =>
delayedCrowdNotification = false
showSpectators(lightUser)(members.values) foreach {
@ -98,6 +94,8 @@ private[tournament] final class TournamentSocket(
if (members.nonEmpty) keepMeAlive()
}
override protected def afterQuit(uid: Socket.Uid, member: Member) = notifyCrowd
private def notifyCrowd: Unit =
if (!delayedCrowdNotification) {
delayedCrowdNotification = true