Skip to content

Commit 5fde650

Browse files
committed
When using shouldSkipBlockingWait, do not grow the seen lock cache infinitely
1 parent f620e0a commit 5fde650

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/main/java/com/amazonaws/services/dynamodbv2/AmazonDynamoDBLockClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.ByteBuffer;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
22+
import java.util.Collections;
2223
import java.util.HashMap;
2324
import java.util.HashSet;
2425
import java.util.Iterator;
@@ -234,6 +235,8 @@ public class AmazonDynamoDBLockClient implements Runnable, Closeable {
234235
private final String ownerName;
235236
private final ConcurrentHashMap<String, LockItem> locks;
236237
private final ConcurrentHashMap<String, LockItem> notMyLocks = new ConcurrentHashMap<>();
238+
private final long maxCacheEntries = 100000;
239+
private final List<String> notMyLocksList = Collections.synchronizedList(new ArrayList<>());
237240
private final ConcurrentHashMap<String, Thread> sessionMonitors;
238241
private final Optional<Thread> backgroundThread;
239242
private final Function<String, ThreadFactory> namedThreadCreator;
@@ -486,6 +489,12 @@ public LockItem acquireLock(final AcquireLockOptions options) throws LockNotGran
486489
}
487490
} else {
488491
notMyLocks.put(id, existingLock.get());
492+
// Ensure we don't grow the cache too large.
493+
notMyLocksList.add(id);
494+
if (notMyLocks.size() > maxCacheEntries) {
495+
String oldest = notMyLocksList.remove(0);
496+
notMyLocks.remove(oldest);
497+
}
489498
}
490499

491500
/*

0 commit comments

Comments
 (0)