Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit e581884

Browse files
committed
refactor: standardize task scopes and improve synchronization logic
- Renamed `BeforeStartTaskScope` to `SynchronizeTasksScope` for clarity. - Replaced `Dispatchers.IO` with `Dispatchers.Default` in task scope implementation for better task scheduling. - Updated synchronization task executions to use `fireAndForget` for reduced overhead. - Added new synchronization tasks: `SetVelocitySecretTask` and `SynchronizeServersTask` for modularity and reusability.
1 parent 539fc88 commit e581884

File tree

8 files changed

+50
-24
lines changed

8 files changed

+50
-24
lines changed

surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/ClientSynchronizingPacketListenerImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import dev.slne.surf.cloud.core.client.server.ClientCloudServerImpl
99
import dev.slne.surf.cloud.core.client.server.ClientProxyCloudServerImpl
1010
import dev.slne.surf.cloud.core.client.server.serverManagerImpl
1111
import dev.slne.surf.cloud.core.client.sync.SyncRegistryImpl
12-
import dev.slne.surf.cloud.core.common.coroutines.BeforeStartTaskScope
1312
import dev.slne.surf.cloud.core.common.coroutines.PacketHandlerScope
13+
import dev.slne.surf.cloud.core.common.coroutines.SynchronizeTasksScope
1414
import dev.slne.surf.cloud.core.common.netty.network.ConnectionImpl
1515
import dev.slne.surf.cloud.core.common.netty.network.protocol.common.ClientboundSetVelocitySecretPacket
1616
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.*
@@ -44,7 +44,7 @@ class ClientSynchronizingPacketListenerImpl(
4444
fun startSynchronizing() {
4545
statusUpdater.switchState(AbstractStatusUpdater.State.SYNCHRONIZING)
4646

47-
BeforeStartTaskScope.launch {
47+
SynchronizeTasksScope.launch {
4848
CloudSynchronizeTaskManager.executeTasks(client)
4949

5050
statusUpdater.switchState(AbstractStatusUpdater.State.SYNCHRONIZE_WAIT_FOR_SERVER)

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/coroutines/scopes.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.slne.surf.cloud.core.common.coroutines
22

33
import dev.slne.surf.cloud.api.common.util.threadFactory
4-
import dev.slne.surf.cloud.core.common.coroutines.BeforeStartTaskScope.unnamedTask
4+
import dev.slne.surf.cloud.core.common.coroutines.SynchronizeTasksScope.unnamedTask
55
import dev.slne.surf.surfapi.core.api.util.logger
66
import dev.slne.surf.surfapi.core.api.util.mutableObjectListOf
77
import kotlinx.coroutines.*
@@ -172,14 +172,14 @@ object PunishmentCacheRefreshScope : BaseScope(
172172
name = "punishment-cache-refresh"
173173
)
174174

175-
object BeforeStartTaskScope : BaseScope(
176-
dispatcher = Dispatchers.IO,
177-
name = "before-start-task",
175+
object SynchronizeTasksScope : BaseScope(
176+
dispatcher = Dispatchers.Default,
177+
name = "synchronize-tasks",
178178
coroutineExceptionHandler = CoroutineExceptionHandler { context, throwable ->
179179
val task = context[TaskName] ?: unnamedTask
180180
log.atWarning()
181181
.withCause(throwable)
182-
.log("Unhandled exception in before start task: $task")
182+
.log("Unhandled exception in synchronize task: $task")
183183
}
184184
) {
185185
@JvmStatic

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/plugin/task/CloudSynchronizeTaskManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dev.slne.surf.cloud.core.common.plugin.task
22

33
import dev.slne.surf.cloud.api.common.netty.NettyClient
44
import dev.slne.surf.cloud.api.common.plugin.spring.task.CloudInitialSynchronizeTask
5-
import dev.slne.surf.cloud.core.common.coroutines.BeforeStartTaskScope
5+
import dev.slne.surf.cloud.core.common.coroutines.SynchronizeTasksScope
66
import dev.slne.surf.surfapi.core.api.util.logger
77
import kotlinx.coroutines.launch
88
import org.springframework.core.annotation.AnnotationAwareOrderComparator
@@ -35,7 +35,7 @@ object CloudSynchronizeTaskManager {
3535
.log("Executing initial synchronize task: ${task.name} (${position + 1}/${tasks.size})")
3636

3737
val duration = measureTime {
38-
BeforeStartTaskScope.launch(BeforeStartTaskScope.TaskName(task.name, position)) {
38+
SynchronizeTasksScope.launch(SynchronizeTasksScope.TaskName(task.name, position)) {
3939
task.execute(client)
4040
}.join()
4141
}

surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/netty/server/network/ServerSynchronizingPacketListenerImpl.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ import dev.slne.surf.cloud.api.common.netty.network.ConnectionProtocol
44
import dev.slne.surf.cloud.api.common.netty.packet.NettyPacket
55
import dev.slne.surf.cloud.api.common.netty.packet.NettyPacketInfo
66
import dev.slne.surf.cloud.api.common.player.CloudPlayerManager
7-
import dev.slne.surf.cloud.core.common.coroutines.BeforeStartTaskScope
87
import dev.slne.surf.cloud.core.common.coroutines.PacketHandlerScope
8+
import dev.slne.surf.cloud.core.common.coroutines.SynchronizeTasksScope
99
import dev.slne.surf.cloud.core.common.netty.network.ConnectionImpl
10-
import dev.slne.surf.cloud.core.common.netty.network.protocol.common.ClientboundSetVelocitySecretPacket
11-
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.*
10+
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.RunningProtocols
11+
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.ServerboundCreateOfflineCloudPlayerIfNotExistsPacket
12+
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.SyncSetDeltaPacket
13+
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.SyncValueChangePacket
1214
import dev.slne.surf.cloud.core.common.netty.network.protocol.synchronizing.ClientboundSynchronizeFinishPacket
1315
import dev.slne.surf.cloud.core.common.netty.network.protocol.synchronizing.FinishSynchronizingPacket
1416
import dev.slne.surf.cloud.core.common.netty.network.protocol.synchronizing.ServerSynchronizingPacketListener
1517
import dev.slne.surf.cloud.core.common.netty.network.protocol.synchronizing.ServerboundSynchronizeFinishAcknowledgedPacket
1618
import dev.slne.surf.cloud.core.common.netty.registry.listener.NettyListenerRegistry
1719
import dev.slne.surf.cloud.core.common.plugin.task.CloudSynchronizeTaskManager
1820
import dev.slne.surf.cloud.standalone.netty.server.NettyServerImpl
19-
import dev.slne.surf.cloud.standalone.netty.server.ProxySecretHolder
2021
import dev.slne.surf.cloud.standalone.netty.server.ServerClientImpl
22+
import dev.slne.surf.cloud.standalone.netty.server.network.config.SetVelocitySecretTask
2123
import dev.slne.surf.cloud.standalone.netty.server.network.config.SynchronizeRegistriesTask
24+
import dev.slne.surf.cloud.standalone.netty.server.network.config.SynchronizeServersTask
2225
import dev.slne.surf.cloud.standalone.netty.server.network.config.SynchronizeUserTask
23-
import dev.slne.surf.cloud.standalone.server.serverManagerImpl
2426
import dev.slne.surf.cloud.standalone.sync.SyncRegistryImpl
2527
import dev.slne.surf.surfapi.core.api.util.logger
2628
import kotlinx.coroutines.launch
@@ -52,10 +54,9 @@ class ServerSynchronizingPacketListenerImpl(
5254
check(state == State.START) { "Cannot start synchronizing from state $state" }
5355
state = State.SYNCHRONIZING
5456

55-
BeforeStartTaskScope.launch {
56-
send(ClientboundSetVelocitySecretPacket(ProxySecretHolder.currentSecret()))
57-
send(ClientboundBatchUpdateServer(serverManagerImpl.retrieveAllServers()))
58-
57+
SynchronizeTasksScope.launch {
58+
SetVelocitySecretTask.execute(client)
59+
SynchronizeServersTask.execute(client)
5960
SynchronizeRegistriesTask.execute(client)
6061
SynchronizeUserTask.execute(client)
6162
CloudSynchronizeTaskManager.executeTasks(client)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.slne.surf.cloud.standalone.netty.server.network.config
2+
3+
import dev.slne.surf.cloud.api.common.netty.NettyClient
4+
import dev.slne.surf.cloud.api.common.plugin.spring.task.CloudInitialSynchronizeTask
5+
import dev.slne.surf.cloud.core.common.netty.network.protocol.common.ClientboundSetVelocitySecretPacket
6+
import dev.slne.surf.cloud.standalone.netty.server.ProxySecretHolder
7+
8+
object SetVelocitySecretTask: CloudInitialSynchronizeTask {
9+
override suspend fun execute(client: NettyClient) {
10+
client.fireAndForget(ClientboundSetVelocitySecretPacket(ProxySecretHolder.currentSecret()))
11+
}
12+
}

surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/netty/server/network/config/SynchronizeRegistriesTask.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import dev.slne.surf.cloud.standalone.sync.SyncRegistryImpl
99
object SynchronizeRegistriesTask : CloudInitialSynchronizeTask {
1010

1111
override suspend fun execute(client: NettyClient) {
12-
client.connection.send(ClientboundBatchSyncValuePacket(SyncRegistryImpl.instance.prepareBatchSyncValues()))
13-
client.connection.send(ClientboundBatchSyncSetPacket(SyncRegistryImpl.instance.prepareBatchSyncSets()))
12+
client.fireAndForget(ClientboundBatchSyncValuePacket(SyncRegistryImpl.instance.prepareBatchSyncValues()))
13+
client.fireAndForget(ClientboundBatchSyncSetPacket(SyncRegistryImpl.instance.prepareBatchSyncSets()))
1414
}
1515
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.slne.surf.cloud.standalone.netty.server.network.config
2+
3+
import dev.slne.surf.cloud.api.common.netty.NettyClient
4+
import dev.slne.surf.cloud.api.common.plugin.spring.task.CloudInitialSynchronizeTask
5+
import dev.slne.surf.cloud.core.common.netty.network.protocol.running.ClientboundBatchUpdateServer
6+
import dev.slne.surf.cloud.standalone.server.serverManagerImpl
7+
8+
object SynchronizeServersTask : CloudInitialSynchronizeTask {
9+
override suspend fun execute(client: NettyClient) {
10+
client.fireAndForget(ClientboundBatchUpdateServer(serverManagerImpl.retrieveAllServers()))
11+
}
12+
}

surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/netty/server/network/config/SynchronizeUserTask.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ object SynchronizeUserTask : CloudInitialSynchronizeTask {
102102
while (iterator.hasNext()) {
103103
val uuid = iterator.next()
104104
val player = players.find { it.uuid == uuid } ?: run {
105-
log.atWarning().log(
106-
"Unable to find player with UUID '%s' in hydration list. Maybe the player already disconnected?",
107-
uuid
108-
)
105+
log.atWarning()
106+
.log(
107+
"Unable to find player with UUID '%s' in hydration list. Maybe the player already disconnected?",
108+
uuid
109+
)
109110
continue
110111
}
111112

0 commit comments

Comments
 (0)