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