Replace synchronized usage with ReentrantLock

Closes gh-702
This commit is contained in:
Mark Paluch
2022-05-23 15:29:15 +02:00
parent 65ce272dd3
commit 2e4aec67da
3 changed files with 23 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.vault.authentication;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.locks.ReentrantLock;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.http.HttpEntity;
@@ -81,7 +82,7 @@ public class LifecycleAwareSessionManager extends LifecycleAwareSessionManagerSu
*/
private final RestOperations restOperations;
private final Object lock = new Object();
private final ReentrantLock lock = new ReentrantLock();
/**
* The token state: Contains the currently valid token that identifies the Vault
@@ -254,12 +255,15 @@ public class LifecycleAwareSessionManager extends LifecycleAwareSessionManagerSu
if (!getToken().isPresent()) {
synchronized (this.lock) {
this.lock.lock();
try {
if (!getToken().isPresent()) {
doGetSessionToken();
}
}
finally {
this.lock.unlock();
}
}
return getToken().map(TokenWrapper::getToken)

View File

@@ -16,6 +16,7 @@
package org.springframework.vault.authentication;
import java.util.Optional;
import java.util.concurrent.locks.ReentrantLock;
import org.springframework.util.Assert;
import org.springframework.vault.support.VaultToken;
@@ -34,7 +35,7 @@ public class SimpleSessionManager implements SessionManager {
private final ClientAuthentication clientAuthentication;
private final Object lock = new Object();
private final ReentrantLock lock = new ReentrantLock();
private volatile Optional<VaultToken> token = Optional.empty();
@@ -53,11 +54,16 @@ public class SimpleSessionManager implements SessionManager {
public VaultToken getSessionToken() {
if (!this.token.isPresent()) {
synchronized (this.lock) {
this.lock.lock();
try {
if (!this.token.isPresent()) {
this.token = Optional.of(this.clientAuthentication.login());
}
}
finally {
this.lock.unlock();
}
}
return this.token.orElseThrow(() -> new IllegalStateException("Cannot obtain VaultToken"));

View File

@@ -18,6 +18,7 @@ package org.springframework.vault.core.env;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -60,7 +61,7 @@ public class VaultPropertySource extends EnumerablePropertySource<VaultOperation
private final boolean ignoreSecretNotFound;
private final Object lock = new Object();
private final ReentrantLock lock = new ReentrantLock();
/**
* Create a new {@link VaultPropertySource} given a {@link VaultTemplate} and
@@ -141,7 +142,9 @@ public class VaultPropertySource extends EnumerablePropertySource<VaultOperation
*/
protected void loadProperties() {
synchronized (this.lock) {
this.lock.lock();
try {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Fetching properties from Vault at %s", this.path));
}
@@ -176,6 +179,9 @@ public class VaultPropertySource extends EnumerablePropertySource<VaultOperation
this.properties.putAll(doTransformProperties(properties));
}
}
finally {
this.lock.unlock();
}
}
@Override