|
19 | 19 | import java.nio.ByteBuffer; |
20 | 20 | import java.util.ArrayList; |
21 | 21 | import java.util.Collection; |
| 22 | +import java.util.Collections; |
22 | 23 | import java.util.HashMap; |
23 | 24 | import java.util.HashSet; |
24 | 25 | import java.util.Iterator; |
@@ -234,6 +235,8 @@ public class AmazonDynamoDBLockClient implements Runnable, Closeable { |
234 | 235 | private final String ownerName; |
235 | 236 | private final ConcurrentHashMap<String, LockItem> locks; |
236 | 237 | private final ConcurrentHashMap<String, LockItem> notMyLocks = new ConcurrentHashMap<>(); |
| 238 | + private final long maxCacheEntries = 100000; |
| 239 | + private final List<String> notMyLocksList = Collections.synchronizedList(new ArrayList<>()); |
237 | 240 | private final ConcurrentHashMap<String, Thread> sessionMonitors; |
238 | 241 | private final Optional<Thread> backgroundThread; |
239 | 242 | private final Function<String, ThreadFactory> namedThreadCreator; |
@@ -486,6 +489,12 @@ public LockItem acquireLock(final AcquireLockOptions options) throws LockNotGran |
486 | 489 | } |
487 | 490 | } else { |
488 | 491 | 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 | + } |
489 | 498 | } |
490 | 499 |
|
491 | 500 | /* |
|
0 commit comments