Skip to content

Commit

Permalink
Merge pull request #9 from GiganticMinecraft/add-log
Browse files Browse the repository at this point in the history
処理接続から切断までの処理の流れにログを追加する
  • Loading branch information
kory33 authored Dec 11, 2022
2 parents bcd5886 + 2f6efeb commit 9eb3173
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# IntelliJ IDEA
.idea/
*.iml
.bsp/

# SBT
/project/target
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.3.13
sbt.version = 1.6.1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package click.seichi.bungeesemaphore

import java.util.concurrent.Executors

import akka.actor.ActorSystem
import cats.effect.{ContextShift, IO, SyncIO, Timer}
import click.seichi.bungeesemaphore.application.configuration.Configuration
Expand All @@ -15,6 +14,7 @@ import click.seichi.generic.concurrent.synchronization.barrier.IndexedSwitchable
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.plugin.Plugin

import java.util.logging.Logger
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext}

Expand All @@ -26,6 +26,7 @@ class BungeeSemaphorePlugin extends Plugin {
implicit val _contextShift: ContextShift[IO] = IO.contextShift(_executionContext)
implicit val _effectEnvironment: EffectEnvironment = JulLoggerEffectEnvironment(getLogger)
implicit val _timer: Timer[IO] = IO.timer(_executionContext)
implicit val logger: Logger = getLogger

implicit val _configuration: Configuration = {
new PluginConfiguration[SyncIO](getDataFolder).getConfiguration.unsafeRunSync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package click.seichi.bungeesemaphore.application

import cats.effect.Sync
import cats.implicits._
import click.seichi.bungeesemaphore.application.configuration.Configuration
import click.seichi.bungeesemaphore.domain.{PlayerName, ServerName}

import java.util.logging.Logger

object EmitGlobalLock {
def of[F[_]: HasGlobalPlayerDataSaveLock: Sync](playerName: PlayerName, disconnectionSource: ServerName)
(implicit configuration: Configuration): F[Unit] = {
(implicit configuration: Configuration, logger: Logger): F[Unit] = {
if (configuration.emitsSaveSignalOnDisconnect(disconnectionSource)) {
HasGlobalPlayerDataSaveLock[F].lock(playerName)
HasGlobalPlayerDataSaveLock[F].lock(playerName) >> Sync[F].delay {
logger.info(s"Globally locked $playerName's data-save lock")
}
} else {
Sync[F].unit
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import simulacrum.typeclass
/**
* Await for an availability of the lock on given `playerName`.
*
* This action is cancellable.
* This action is cancellable when next case.
* - When it is found that data save has failed.
*/
def awaitLockAvailability(playerName: PlayerName): F[Unit]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import simulacrum.typeclass
* An action to semantically block until the player with given `playerName` disconnects from the proxy server.
*
* This action is cancellable.
*
* This action is cancelled when the data save has been reported failed.
*/
def awaitDisconnectedState(playerName: PlayerName): F[Unit]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import click.seichi.bungeesemaphore.domain.{PlayerName, ServerName}
import net.md_5.bungee.api.config.ServerInfo
import net.md_5.bungee.api.connection.ProxiedPlayer

import java.util.logging.Logger

object AwaitDataSaveConfirmation {

import cats.implicits._

def of[F[_] : Sync : HasGlobalPlayerDataSaveLock](player: ProxiedPlayer, targetServer: ServerInfo)
(implicit configuration: Configuration): F[Unit] = {
(implicit configuration: Configuration, logger: Logger): F[Unit] = {
if (configuration.shouldAwaitForSaveSignal(ServerName(targetServer.getName))) {
HasGlobalPlayerDataSaveLock[F]
.awaitLockAvailability(PlayerName(player.getName))
.orElse {
Sync[F].delay {
player.disconnect(configuration.errorMessages.downstreamCouldNotSaveData)
logger.info(s"${player.getName}'s save-lock has been cleared, but data was not saved successfully.")
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import net.md_5.bungee.api.plugin.Listener
import net.md_5.bungee.event.{EventHandler, EventPriority}

import java.util.concurrent.ConcurrentHashMap
import java.util.logging.Logger
import scala.collection.mutable

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ import scala.collection.mutable
*/
class SemaphoringServerSwitcher[
F[_]: ConcurrentEffect: HasGlobalPlayerDataSaveLock: HasPlayerConnectionLock: Timer
](implicit configuration: Configuration, effectEnvironment: EffectEnvironment, proxy: ProxyServer)
](implicit configuration: Configuration, effectEnvironment: EffectEnvironment, proxy: ProxyServer, logger: Logger)
extends Listener {

import cats.implicits._
Expand Down Expand Up @@ -93,6 +94,8 @@ class SemaphoringServerSwitcher[
val targetServer = event.getTarget
val playerName = PlayerName(player.getName)

logger.info(s"${player.getName} requested a connection to ${targetServer.getName}")

// If we get a server switch command from a player
// that tries to go to the same server the player is already connected (e.g. sending `/server lobby` in lobby),
// the ServerConnectEvent is not called hence the connection hangs.
Expand All @@ -109,14 +112,18 @@ class SemaphoringServerSwitcher[
case originalServer =>
ConnectionModifications.letConnectionLinger[F](player) >>
EmitGlobalLock.of[F](playerName, ServerName(originalServer.getInfo.getName)) >>
ConnectionModifications.disconnectFromServer(player)
ConnectionModifications.disconnectFromServer(player) >> Sync[F].delay {
logger.info(s"Disconnected $playerName from current server. Holding connection for further actions...")
}
}

val reconnectToTarget = Sync[F].delay {
// prevent this listener from reacting again
playersBeingConnectedToNewServer.add(playerName)

player.connect(targetServer)

logger.info(s"Connected $playerName to the original destination server")
}

event.setCancelled(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import click.seichi.bungeesemaphore.domain.PlayerName
import click.seichi.generic.concurrent.synchronization.barrier.IndexedSwitchableBarrier
import redis.RedisClient

import java.util.logging.Logger
import scala.concurrent.duration.{Duration, FiniteDuration}

object LocalLockRedisBridge {
Expand Down Expand Up @@ -44,7 +45,8 @@ object LocalLockRedisBridge {
(implicit configuration: Configuration,
actorSystem: ActorSystem,
effectEnvironment: EffectEnvironment,
publishingContext: ContextShift[IO]): F[HasGlobalPlayerDataSaveLock[F]] = {
publishingContext: ContextShift[IO],
logger: Logger): F[HasGlobalPlayerDataSaveLock[F]] = {

val pxMillis = configuration.saveLockTimeout match {
case _: Duration.Infinite => None
Expand Down Expand Up @@ -90,6 +92,10 @@ object LocalLockRedisBridge {
localLock(playerName).await,
requestLocalPromise.get
)

_ <- Sync[F].delay {
logger.info(s"${playerName.value}'s save-lock has been successfully cleared")
}
} yield ()
}
}
Expand Down

0 comments on commit 9eb3173

Please sign in to comment.