Add system property to disable public key caching (#7438)

This commit is contained in:
Rick Parker
2023-07-21 08:56:22 +01:00
committed by GitHub
parent 7d1d2297e7
commit 6ec8855c6e

View File

@ -7,6 +7,8 @@ import java.security.PublicKey
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
object PublicKeyCache { object PublicKeyCache {
private val DISABLE = java.lang.Boolean.getBoolean("net.corda.core.pubkeycache.disable")
private val collectedWeakPubKeys = ReferenceQueue<PublicKey>() private val collectedWeakPubKeys = ReferenceQueue<PublicKey>()
private class WeakPubKey(key: PublicKey, val bytes: ByteSequence? = null) : WeakReference<PublicKey>(key, collectedWeakPubKeys) { private class WeakPubKey(key: PublicKey, val bytes: ByteSequence? = null) : WeakReference<PublicKey>(key, collectedWeakPubKeys) {
@ -36,15 +38,18 @@ object PublicKeyCache {
} }
fun bytesForCachedPublicKey(key: PublicKey): ByteSequence? { fun bytesForCachedPublicKey(key: PublicKey): ByteSequence? {
if (DISABLE) return null
val weakPubKey = WeakPubKey(key) val weakPubKey = WeakPubKey(key)
return pubKeyToBytes[weakPubKey] return pubKeyToBytes[weakPubKey]
} }
fun publicKeyForCachedBytes(bytes: ByteSequence): PublicKey? { fun publicKeyForCachedBytes(bytes: ByteSequence): PublicKey? {
if (DISABLE) return null
return bytesToPubKey[bytes]?.get() return bytesToPubKey[bytes]?.get()
} }
fun cachePublicKey(key: PublicKey): PublicKey { fun cachePublicKey(key: PublicKey): PublicKey {
if (DISABLE) return key
reapCollectedWeakPubKeys() reapCollectedWeakPubKeys()
val weakPubKey = WeakPubKey(key, ByteSequence.of(key.encoded)) val weakPubKey = WeakPubKey(key, ByteSequence.of(key.encoded))
pubKeyToBytes.putIfAbsent(weakPubKey, weakPubKey.bytes!!) pubKeyToBytes.putIfAbsent(weakPubKey, weakPubKey.bytes!!)