mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-11 11:57:01 +01:00
Hardened EOSEAccountFast against concurrent access so callers no longer iterate over live mutable maps
This commit is contained in:
@@ -125,36 +125,46 @@ 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,
|
||||||
) {
|
) {
|
||||||
val relayList = users[user]
|
synchronized(lock) {
|
||||||
if (relayList == null) {
|
val relayList = users[user]
|
||||||
val newList = EOSERelayList()
|
if (relayList == null) {
|
||||||
users.put(user, newList)
|
val newList = EOSERelayList()
|
||||||
|
users.put(user, newList)
|
||||||
|
|
||||||
newList.addOrUpdate(relayUrl, time)
|
newList.addOrUpdate(relayUrl, time)
|
||||||
} else {
|
} else {
|
||||||
relayList.addOrUpdate(relayUrl, time)
|
relayList.addOrUpdate(relayUrl, time)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeEveryoneBut(list: Set<T>) {
|
fun removeEveryoneBut(list: Set<T>) {
|
||||||
users.snapshot().forEach {
|
synchronized(lock) {
|
||||||
if (it.key !in list) {
|
users.snapshot().forEach {
|
||||||
users.remove(it.key)
|
if (it.key !in list) {
|
||||||
|
users.remove(it.key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeDataFor(user: T) {
|
fun removeDataFor(user: T) {
|
||||||
users.remove(user)
|
synchronized(lock) {
|
||||||
|
users.remove(user)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun since(key: T) = users[key]?.relayList
|
fun since(key: T): SincePerRelayMap? =
|
||||||
|
synchronized(lock) {
|
||||||
|
users[key]?.relayList?.toMutableMap()
|
||||||
|
}
|
||||||
|
|
||||||
fun newEose(
|
fun newEose(
|
||||||
user: T,
|
user: T,
|
||||||
|
|||||||
Reference in New Issue
Block a user