From eea884fb823cf2cf70e6b2d02a78874dd261bd37 Mon Sep 17 00:00:00 2001 From: Jeff Trent Date: Fri, 6 Oct 2023 11:55:03 -0400 Subject: [PATCH] review comments --- .../common/tls/spi/TlsManagerCache.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java b/common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java index 9bc8acc9d66..b1b374a3f7b 100644 --- a/common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java +++ b/common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java @@ -16,24 +16,40 @@ package io.helidon.common.tls.spi; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Function; import io.helidon.common.tls.TlsManager; class TlsManagerCache { - private static final ConcurrentHashMap CACHE = new ConcurrentHashMap<>(); + private static final ReentrantLock LOCK = new ReentrantLock(); + private static final Map CACHE = new HashMap<>(); private TlsManagerCache() { } - @SuppressWarnings("unchecked") static TlsManager getOrCreate(T configBean, Function creator) { Objects.requireNonNull(configBean); Objects.requireNonNull(creator); - return CACHE.computeIfAbsent(configBean, (Function) creator); + LOCK.lock(); + try { + TlsManager manager = CACHE.get(configBean); + if (manager != null) { + return manager; + } + + manager = creator.apply(configBean); + Object existing = CACHE.put(configBean, manager); + assert (existing == null); + + return manager; + } finally { + LOCK.unlock(); + } } }