From 27df9db855dc157d32c65c3c2894a587847ace2f Mon Sep 17 00:00:00 2001 From: Bernie <114138479+InforBG@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:09:12 +0800 Subject: [PATCH] To support Virtual Thread, replace synchronized with ReentrantLock. --- .../caching/cache/SecretCacheObject.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/amazonaws/secretsmanager/caching/cache/SecretCacheObject.java b/src/main/java/com/amazonaws/secretsmanager/caching/cache/SecretCacheObject.java index b1eee46..b926b33 100644 --- a/src/main/java/com/amazonaws/secretsmanager/caching/cache/SecretCacheObject.java +++ b/src/main/java/com/amazonaws/secretsmanager/caching/cache/SecretCacheObject.java @@ -14,6 +14,7 @@ package com.amazonaws.secretsmanager.caching.cache; import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.locks.ReentrantLock; import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; @@ -48,7 +49,7 @@ public abstract class SecretCacheObject { protected final String secretId; /** A private object to synchronize access to certain methods. */ - protected final Object lock = new Object(); + private final ReentrantLock lock = new ReentrantLock(); /** The AWS Secrets Manager client to use for requesting secrets. */ protected final SecretsManagerClient client; @@ -229,9 +230,12 @@ public boolean refreshNow() throws InterruptedException { Thread.sleep(sleep); // Perform the requested refresh - synchronized (lock) { + lock.lock(); + try { refresh(); return (null == this.exception); + } finally { + lock.unlock(); } } @@ -241,13 +245,16 @@ public boolean refreshNow() throws InterruptedException { * @return The cached GetSecretValue result. */ public GetSecretValueResponse getSecretValue() { - synchronized (lock) { + lock.lock(); + try { refresh(); if (null == this.data) { if (null != this.exception) { throw this.exception; } } return this.getSecretValue(this.getResult()); + } finally { + lock.unlock(); } }