|
@@ -30,6 +30,8 @@ import java.net.URI
|
|
|
import java.time.LocalDateTime
|
|
|
import java.time.format.DateTimeFormatter
|
|
|
import java.util.*
|
|
|
+import java.util.concurrent.ConcurrentHashMap
|
|
|
+import java.util.concurrent.ConcurrentMap
|
|
|
import kotlin.system.exitProcess
|
|
|
import kotlin.system.measureTimeMillis
|
|
|
|
|
@@ -74,7 +76,8 @@ fun main(args: Array<String>) {
|
|
|
class Client(
|
|
|
val number: String,
|
|
|
var connectAt: String,
|
|
|
- var lastRequest: String
|
|
|
+ var lastRequest: String,
|
|
|
+ val sessionId: String?
|
|
|
) : Serializable {
|
|
|
var reqSuccess = 0
|
|
|
var reqFailed = 0
|
|
@@ -83,7 +86,7 @@ class Client(
|
|
|
|
|
|
object ClientHolder {
|
|
|
private val mutexSucReq = Mutex()
|
|
|
- private val clientMap: MutableMap<String, Client> = mutableMapOf()
|
|
|
+ private val clientMap: ConcurrentMap<String, Client> by lazy { ConcurrentHashMap() }
|
|
|
private suspend fun massiveRun(action: suspend () -> Unit) {
|
|
|
measureTimeMillis {
|
|
|
coroutineScope { // sc
|
|
@@ -94,11 +97,17 @@ object ClientHolder {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ fun removeBySessionId(sessionId: String?) {
|
|
|
+ clientMap.entries.filter { it.value.sessionId == sessionId }.forEach {
|
|
|
+ clientMap.remove(it.key)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
fun put(key: String, client: Client) {
|
|
|
- clientMap[key] = client
|
|
|
+ clientMap.getOrPut(key) { client }
|
|
|
}
|
|
|
|
|
|
- fun get() = clientMap.toMap()
|
|
|
+ fun get() = clientMap
|
|
|
|
|
|
fun addFailedRequest(clNum: String) {
|
|
|
runBlocking {
|
|
@@ -183,6 +192,7 @@ class WebSocketEventListener {
|
|
|
@EventListener
|
|
|
fun onDisconnect(event: SessionDisconnectEvent) {
|
|
|
AppLog.write(this.javaClass).info("disconnect with session id ${event.sessionId}")
|
|
|
+ ClientHolder.removeBySessionId(event.sessionId)
|
|
|
}
|
|
|
|
|
|
@EventListener
|
|
@@ -201,7 +211,8 @@ class WebSocketEventListener {
|
|
|
clNum, Client(
|
|
|
clNum,
|
|
|
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),
|
|
|
- LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
|
|
|
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")),
|
|
|
+ sessionId
|
|
|
)
|
|
|
)
|
|
|
|