Skip to content

Commit

Permalink
Improve the performance of updating the last update time for clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ismartcoding committed Aug 25, 2023
1 parent ed7bfe0 commit 889926f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/com/ismartcoding/plain/db/DSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ data class DSession(
var token: String = ""
}


data class SessionClientTsUpdate(
@ColumnInfo(name = "client_id")
var clientId: String,
@ColumnInfo(name = "updated_at")
val updatedAt: Instant = Clock.System.now(),
)

@Dao
interface SessionDao {
@Query("SELECT * FROM sessions ORDER BY updated_at DESC")
Expand All @@ -43,8 +51,8 @@ interface SessionDao {
@Update
fun update(vararg item: DSession)

@Query("UPDATE sessions SET updated_at=:updatedAt WHERE client_id=:clientId")
fun updateTs(clientId: String, updatedAt: Instant)
@Update(entity = DSession::class)
fun updateTs(items: List<SessionClientTsUpdate>)

@Query("DELETE FROM sessions WHERE client_id=:clientId")
fun delete(clientId: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.ismartcoding.plain.TempData
import com.ismartcoding.plain.data.preference.HttpPortPreference
import com.ismartcoding.plain.data.preference.HttpsPortPreference
import com.ismartcoding.plain.data.preference.PasswordPreference
import com.ismartcoding.plain.db.AppDatabase
import com.ismartcoding.plain.db.SessionClientTsUpdate
import com.ismartcoding.plain.features.ConfirmToAcceptLoginEvent
import com.ismartcoding.plain.web.websocket.WebSocketSession
import io.ktor.server.application.Application
Expand Down Expand Up @@ -106,18 +108,19 @@ object HttpServerManager {
}

fun clientTsInterval() {
val duration = 5000L
Timer().scheduleAtFixedRate(object : TimerTask() {
override fun run() {
val now = System.currentTimeMillis()
clientRequestTs.forEach { (clientId, ts) ->
if (ts + 5000 > now) {
coIO {
SessionList.updateTsAsync(clientId, Instant.fromEpochMilliseconds(ts))
}
val updates = clientRequestTs.filter { it.value + duration > now }
.map { SessionClientTsUpdate(it.key, Instant.fromEpochMilliseconds(it.value)) }
if (updates.isNotEmpty()) {
coIO {
AppDatabase.instance.sessionDao().updateTs(updates)
}
}
}
}, 0, 5000)
}, 0, duration)
}

suspend fun respondTokenAsync(event: ConfirmToAcceptLoginEvent, clientIp: String) {
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/com/ismartcoding/plain/web/SessionList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.ismartcoding.plain.web
import com.ismartcoding.plain.db.AppDatabase
import com.ismartcoding.plain.db.DSession
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant

object SessionList {
fun getItemsAsync(): List<DSession> {
Expand All @@ -30,10 +29,6 @@ object SessionList {
}
}

fun updateTsAsync(clientId: String, updatedAt: Instant) {
AppDatabase.instance.sessionDao().updateTs(clientId, updatedAt)
}

fun deleteAsync(clientId: String) {
AppDatabase.instance.sessionDao().delete(clientId)
}
Expand Down

0 comments on commit 889926f

Please sign in to comment.