Hardened EOSEAccountFast against concurrent access so callers no longer iterate over live mutable maps

This commit is contained in:
davotoula
2025-11-05 18:55:58 +01:00
parent c07202944d
commit a82d6565fa

View File

@@ -125,12 +125,14 @@ class EOSEAccountFast<T : Any>(
cacheSize: Int = 20,
) {
private val users: LruCache<T, EOSERelayList> = LruCache<T, EOSERelayList>(cacheSize)
private val lock = Any()
fun addOrUpdate(
user: T,
relayUrl: NormalizedRelayUrl,
time: Long,
) {
synchronized(lock) {
val relayList = users[user]
if (relayList == null) {
val newList = EOSERelayList()
@@ -141,20 +143,28 @@ class EOSEAccountFast<T : Any>(
relayList.addOrUpdate(relayUrl, time)
}
}
}
fun removeEveryoneBut(list: Set<T>) {
synchronized(lock) {
users.snapshot().forEach {
if (it.key !in list) {
users.remove(it.key)
}
}
}
fun removeDataFor(user: T) {
users.remove(user)
}
fun since(key: T) = users[key]?.relayList
fun removeDataFor(user: T) {
synchronized(lock) {
users.remove(user)
}
}
fun since(key: T): SincePerRelayMap? =
synchronized(lock) {
users[key]?.relayList?.toMutableMap()
}
fun newEose(
user: T,