|
|
|
@ -28,6 +28,7 @@ import org.redisson.pubsub.SemaphorePubSub;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
@ -282,14 +283,19 @@ public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedException {
|
|
|
|
|
LOGGER.debug("trying to acquire, permits: {}, waitTime: {}, unit: {}, name: {}", permits, waitTime, unit, getName());
|
|
|
|
|
public boolean tryAcquire(Duration waitTime) throws InterruptedException {
|
|
|
|
|
return tryAcquire(1, waitTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean tryAcquire(int permits, Duration waitTime) throws InterruptedException {
|
|
|
|
|
LOGGER.debug("trying to acquire, permits: {}, waitTime: {}, name: {}", permits, waitTime, getName());
|
|
|
|
|
|
|
|
|
|
long time = unit.toMillis(waitTime);
|
|
|
|
|
long time = waitTime.toMillis();
|
|
|
|
|
long current = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
if (tryAcquire(permits)) {
|
|
|
|
|
LOGGER.debug("acquired, permits: {}, waitTime: {}, unit: {}, name: {}", permits, waitTime, unit, getName());
|
|
|
|
|
LOGGER.debug("acquired, permits: {}, waitTime: {}, name: {}", permits, waitTime, getName());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -322,7 +328,7 @@ public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
|
|
|
|
|
while (true) {
|
|
|
|
|
current = System.currentTimeMillis();
|
|
|
|
|
if (tryAcquire(permits)) {
|
|
|
|
|
LOGGER.debug("acquired, permits: {}, wait-time: {}, unit: {}, name: {}", permits, waitTime, unit, getName());
|
|
|
|
|
LOGGER.debug("acquired, permits: {}, wait-time: {}, name: {}", permits, waitTime, getName());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -347,13 +353,18 @@ public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
|
|
|
|
|
} finally {
|
|
|
|
|
unsubscribe(entry);
|
|
|
|
|
}
|
|
|
|
|
// return get(tryAcquireAsync(permits, waitTime, unit));
|
|
|
|
|
// return get(tryAcquireAsync(permits, waitTime));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public RFuture<Boolean> tryAcquireAsync(int permits, long waitTime, TimeUnit unit) {
|
|
|
|
|
public RFuture<Boolean> tryAcquireAsync(Duration waitTime) {
|
|
|
|
|
return tryAcquireAsync(1, waitTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public RFuture<Boolean> tryAcquireAsync(int permits, Duration waitTime) {
|
|
|
|
|
CompletableFuture<Boolean> result = new CompletableFuture<>();
|
|
|
|
|
AtomicLong time = new AtomicLong(unit.toMillis(waitTime));
|
|
|
|
|
AtomicLong time = new AtomicLong(waitTime.toMillis());
|
|
|
|
|
long curr = System.currentTimeMillis();
|
|
|
|
|
RFuture<Boolean> tryAcquireFuture = tryAcquireAsync(permits);
|
|
|
|
|
tryAcquireFuture.whenComplete((res, e) -> {
|
|
|
|
@ -401,6 +412,16 @@ public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
|
|
|
|
|
return new CompletableFutureWrapper<>(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedException {
|
|
|
|
|
return tryAcquire(permits, Duration.ofMillis(unit.toMillis(waitTime)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public RFuture<Boolean> tryAcquireAsync(int permits, long waitTime, TimeUnit unit) {
|
|
|
|
|
return tryAcquireAsync(permits, Duration.ofMillis(unit.toMillis(waitTime)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CompletableFuture<RedissonLockEntry> subscribe() {
|
|
|
|
|
return semaphorePubSub.subscribe(getRawName(), getChannelName());
|
|
|
|
|
}
|
|
|
|
|