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

Commit 6b982e9

Browse files
committed
refactor: replace synchronized collections with ConcurrentHashMap and ConcurrentLinkedQueue
1 parent d2d84e0 commit 6b982e9

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/netty/network/ConnectionImpl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,6 @@ class ConnectionImpl(
712712
handleDisconnection()
713713
}
714714

715-
716715
this.averageSentPackets = lerp(
717716
0.75f, this.sentPackets.toFloat(),
718717
this.averageSentPackets

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/netty/network/RespondingPacketSendHandler.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import dev.slne.surf.cloud.api.common.netty.packet.ResponseNettyPacket
66
import dev.slne.surf.cloud.api.common.util.netty.UnifiedReadOnlyChannelHandler
77
import dev.slne.surf.surfapi.core.api.util.logger
88
import dev.slne.surf.surfapi.core.api.util.mutableObject2ObjectMapOf
9-
import dev.slne.surf.surfapi.core.api.util.synchronize
109
import io.netty.channel.ChannelHandlerContext
1110
import io.netty.channel.ChannelPromise
1211
import kotlinx.coroutines.CompletableDeferred
@@ -16,7 +15,7 @@ import java.util.*
1615
class RespondingPacketSendHandler : UnifiedReadOnlyChannelHandler<NettyPacket>() {
1716
private val log = logger()
1817
private val respondingPackets =
19-
mutableObject2ObjectMapOf<UUID, CompletableDeferred<ResponseNettyPacket>>().synchronize()
18+
mutableObject2ObjectMapOf<UUID, CompletableDeferred<ResponseNettyPacket>>()
2019

2120
@Suppress("DEPRECATION")
2221
override fun handleRead(
@@ -49,7 +48,7 @@ class RespondingPacketSendHandler : UnifiedReadOnlyChannelHandler<NettyPacket>()
4948
}
5049
}
5150

52-
@Suppress("DEPRECATION")
51+
@Suppress("UNCHECKED_CAST")
5352
override fun handleWrite(
5453
ctx: ChannelHandlerContext,
5554
msg: NettyPacket,
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package dev.slne.surf.cloud.core.common.netty.network
22

3-
import dev.slne.surf.surfapi.core.api.util.mutableObjectListOf
4-
import dev.slne.surf.surfapi.core.api.util.synchronize
5-
import kotlinx.coroutines.sync.Mutex
6-
import kotlinx.coroutines.sync.withLock
3+
import dev.slne.surf.surfapi.core.api.util.logger
4+
import java.util.concurrent.ConcurrentLinkedQueue
75

86
interface TickablePacketListener : PacketListener {
97

@@ -14,22 +12,31 @@ interface TickablePacketListener : PacketListener {
1412
}
1513

1614
abstract class CommonTickablePacketListener : TickablePacketListener {
17-
private val schedules = mutableObjectListOf<suspend () -> Unit>().synchronize()
18-
private val schedulesMutex = Mutex()
15+
companion object {
16+
private val log = logger()
17+
}
18+
19+
private val schedules = ConcurrentLinkedQueue<suspend () -> Unit>()
1920

2021
override suspend fun tick() {
21-
schedulesMutex.withLock {
22-
schedules.forEach { it() }
23-
schedules.clear()
22+
while (true) {
23+
val task = schedules.poll() ?: break
24+
25+
try {
26+
task()
27+
} catch (e: Exception) {
28+
log.atWarning()
29+
.withCause(e)
30+
.log("Error while executing scheduled task")
31+
}
2432
}
33+
2534
tick0()
2635
}
2736

2837
protected abstract suspend fun tick0()
2938

30-
suspend fun schedule(function: suspend () -> Unit) {
31-
schedulesMutex.withLock {
32-
schedules.add(function)
33-
}
39+
fun schedule(function: suspend () -> Unit) {
40+
schedules.add(function)
3441
}
3542
}

surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/plugin/coroutine/impl/CoroutineManagerImpl.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package dev.slne.surf.cloud.standalone.plugin.coroutine.impl
22

33
import com.google.auto.service.AutoService
4-
import dev.slne.surf.surfapi.core.api.util.mutableObject2ObjectMapOf
5-
import dev.slne.surf.surfapi.core.api.util.synchronize
64
import dev.slne.surf.cloud.api.server.plugin.StandalonePlugin
75
import dev.slne.surf.cloud.api.server.plugin.coroutine.CoroutineManager
86
import dev.slne.surf.cloud.api.server.plugin.coroutine.CoroutineSession
7+
import java.util.concurrent.ConcurrentHashMap
98

109
@AutoService(CoroutineManager::class)
1110
class CoroutineManagerImpl : CoroutineManager {
12-
private val items =
13-
mutableObject2ObjectMapOf<StandalonePlugin, CoroutineSessionImpl>().synchronize()
11+
private val items = ConcurrentHashMap<StandalonePlugin, CoroutineSessionImpl>()
1412

1513
override fun getCoroutineSession(plugin: StandalonePlugin): CoroutineSession {
1614
val session = items[plugin]
@@ -20,9 +18,10 @@ class CoroutineManagerImpl : CoroutineManager {
2018
}
2119

2220
override fun setupCoroutineSession(plugin: StandalonePlugin) {
23-
check(plugin !in items) { "Coroutine session for plugin ${plugin.meta.name} is already initialized" }
24-
items[plugin] = CoroutineSessionImpl(plugin)
25-
21+
items.compute(plugin) { _, value ->
22+
check(value == null) { "Coroutine session for plugin ${plugin.meta.name} is already initialized" }
23+
CoroutineSessionImpl(plugin)
24+
}
2625
}
2726

2827
override fun disable(plugin: StandalonePlugin) {

surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/plugin/entrypoint/classloader/ByteCodeModifyingURLClassloader.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package dev.slne.surf.cloud.standalone.plugin.entrypoint.classloader
22

3-
import dev.slne.surf.surfapi.core.api.util.mutableObject2ObjectMapOf
4-
import dev.slne.surf.surfapi.core.api.util.synchronize
53
import java.io.IOException
64
import java.io.UncheckedIOException
75
import java.net.JarURLConnection
@@ -10,6 +8,7 @@ import java.net.URL
108
import java.net.URLClassLoader
119
import java.security.CodeSigner
1210
import java.security.CodeSource
11+
import java.util.concurrent.ConcurrentHashMap
1312
import java.util.jar.Attributes
1413
import java.util.jar.Manifest
1514

@@ -26,7 +25,7 @@ class ByteCodeModifyingURLClassloader(
2625
}
2726
}
2827

29-
private val manifestCache = mutableObject2ObjectMapOf<String, Any>().synchronize()
28+
private val manifestCache = ConcurrentHashMap<String, Any>()
3029

3130
override fun findClass(name: String): Class<*> {
3231
val path = name.replace('.', '/') + ".class"
@@ -43,7 +42,7 @@ class ByteCodeModifyingURLClassloader(
4342
val lastDot = name.lastIndexOf('.')
4443

4544
if (lastDot != -1) {
46-
val pkgName = name.substring(0, lastDot)
45+
val pkgName = name.take(lastDot)
4746

4847
// Check if package already loaded.
4948
val manifest = url.manifest()

0 commit comments

Comments
 (0)