Skip to content

Commit 709894a

Browse files
committed
Replace synchronized usage with ReentrantLock
Closes gh-702
1 parent 5d87845 commit 709894a

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

spring-vault-core/src/main/java/org/springframework/vault/authentication/LifecycleAwareSessionManager.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.time.Duration;
1919
import java.util.Map;
2020
import java.util.Optional;
21+
import java.util.concurrent.locks.ReentrantLock;
2122

2223
import org.springframework.beans.factory.DisposableBean;
2324
import org.springframework.http.HttpEntity;
@@ -81,7 +82,7 @@ public class LifecycleAwareSessionManager extends LifecycleAwareSessionManagerSu
8182
*/
8283
private final RestOperations restOperations;
8384

84-
private final Object lock = new Object();
85+
private final ReentrantLock lock = new ReentrantLock();
8586

8687
/**
8788
* The token state: Contains the currently valid token that identifies the Vault
@@ -254,12 +255,15 @@ public VaultToken getSessionToken() {
254255

255256
if (!getToken().isPresent()) {
256257

257-
synchronized (this.lock) {
258-
258+
this.lock.lock();
259+
try {
259260
if (!getToken().isPresent()) {
260261
doGetSessionToken();
261262
}
262263
}
264+
finally {
265+
this.lock.unlock();
266+
}
263267
}
264268

265269
return getToken().map(TokenWrapper::getToken)

spring-vault-core/src/main/java/org/springframework/vault/authentication/SimpleSessionManager.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.vault.authentication;
1717

1818
import java.util.Optional;
19+
import java.util.concurrent.locks.ReentrantLock;
1920

2021
import org.springframework.util.Assert;
2122
import org.springframework.vault.support.VaultToken;
@@ -34,7 +35,7 @@ public class SimpleSessionManager implements SessionManager {
3435

3536
private final ClientAuthentication clientAuthentication;
3637

37-
private final Object lock = new Object();
38+
private final ReentrantLock lock = new ReentrantLock();
3839

3940
private volatile Optional<VaultToken> token = Optional.empty();
4041

@@ -53,11 +54,16 @@ public SimpleSessionManager(ClientAuthentication clientAuthentication) {
5354
public VaultToken getSessionToken() {
5455

5556
if (!this.token.isPresent()) {
56-
synchronized (this.lock) {
57+
58+
this.lock.lock();
59+
try {
5760
if (!this.token.isPresent()) {
5861
this.token = Optional.of(this.clientAuthentication.login());
5962
}
6063
}
64+
finally {
65+
this.lock.unlock();
66+
}
6167
}
6268

6369
return this.token.orElseThrow(() -> new IllegalStateException("Cannot obtain VaultToken"));

spring-vault-core/src/main/java/org/springframework/vault/core/env/VaultPropertySource.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.LinkedHashMap;
1919
import java.util.Map;
2020
import java.util.Set;
21+
import java.util.concurrent.locks.ReentrantLock;
2122

2223
import org.apache.commons.logging.Log;
2324
import org.apache.commons.logging.LogFactory;
@@ -60,7 +61,7 @@ public class VaultPropertySource extends EnumerablePropertySource<VaultOperation
6061

6162
private final boolean ignoreSecretNotFound;
6263

63-
private final Object lock = new Object();
64+
private final ReentrantLock lock = new ReentrantLock();
6465

6566
/**
6667
* Create a new {@link VaultPropertySource} given a {@link VaultTemplate} and
@@ -141,7 +142,9 @@ public VaultPropertySource(String name, VaultOperations vaultOperations, String
141142
*/
142143
protected void loadProperties() {
143144

144-
synchronized (this.lock) {
145+
this.lock.lock();
146+
147+
try {
145148
if (logger.isDebugEnabled()) {
146149
logger.debug(String.format("Fetching properties from Vault at %s", this.path));
147150
}
@@ -176,6 +179,9 @@ protected void loadProperties() {
176179
this.properties.putAll(doTransformProperties(properties));
177180
}
178181
}
182+
finally {
183+
this.lock.unlock();
184+
}
179185
}
180186

181187
@Override

0 commit comments

Comments
 (0)