Stephan Schröder
03/12/2024, 9:31 AMgetOrPut
, so
data object ProxyMode {
private val certificateCache: MutableMap<String, BCECPublicKey> = mutableMapOf()
private val lock = Mutex()
...
suspend fun getRequest(...): Pair<HttpClient, RequestData> {
...
val serverEncPublicKey: BCECPublicKey = lock.withLock { certificateCache.getOrPut(host) {
loadCertificate("https://$host/Certificate").bcecPublicKey
} }
...
}
}
but I wonder if a way exists where the locking is part of the collection class but which still locks the coroutine and not the thread.Aleksei Tirman [JB]
03/12/2024, 1:48 PMCLOVIS
03/12/2024, 2:36 PMgetOrPut
call won't be atomic.Stephan Schröder
03/12/2024, 10:00 PMgetOrPut
wouldn't be atomic but computeIfAbsent
would, but unfortunately that one would block the thread and not the coroutine 🤷♂️