This is an automated email from the ASF dual-hosted git repository. michaelo pushed a commit to branch MRESOLVER-184 in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
commit 3971fa930d12831f28d11707e05b7e28b4ec19d1 Author: Michael Osipov <[email protected]> AuthorDate: Sun May 16 14:52:26 2021 +0200 [MRESOLVER-184] Destroy Redisson semaphores if not used anymore --- .../RedissonSemaphoreNamedLockFactory.java | 27 ++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/maven-resolver-named-locks-redisson/src/main/java/org/eclipse/aether/named/redisson/RedissonSemaphoreNamedLockFactory.java b/maven-resolver-named-locks-redisson/src/main/java/org/eclipse/aether/named/redisson/RedissonSemaphoreNamedLockFactory.java index bef84cc..852a050 100644 --- a/maven-resolver-named-locks-redisson/src/main/java/org/eclipse/aether/named/redisson/RedissonSemaphoreNamedLockFactory.java +++ b/maven-resolver-named-locks-redisson/src/main/java/org/eclipse/aether/named/redisson/RedissonSemaphoreNamedLockFactory.java @@ -23,8 +23,12 @@ import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock; import org.eclipse.aether.named.support.NamedLockSupport; import org.redisson.api.RSemaphore; +import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; /** @@ -37,12 +41,31 @@ public class RedissonSemaphoreNamedLockFactory { public static final String NAME = "semaphore-redisson"; + private final ConcurrentMap<String, RSemaphore> semaphores; + + @Inject + public RedissonSemaphoreNamedLockFactory() + { + super(); + this.semaphores = new ConcurrentHashMap<>(); + } + @Override protected NamedLockSupport createLock( final String name ) { + RSemaphore semaphore = semaphores.computeIfAbsent( + name, k -> redissonClient.getSemaphore( NAME_PREFIX + k ) ); + return new AdaptedSemaphoreNamedLock( - name, this, new RedissonSemaphore( redissonClient.getSemaphore( NAME_PREFIX + name ) ) - ); + name, this, new RedissonSemaphore( semaphore ) + ); + } + + @Override + protected void destroyLock( NamedLockSupport lock ) + { + RSemaphore semaphore = semaphores.remove( lock.name() ); + semaphore.delete(); } private static final class RedissonSemaphore implements AdaptedSemaphoreNamedLock.AdaptedSemaphore
