Feature - RSemaphore.trySetPermits() method with ttl parameter added.

pull/5775/head
Nikita Koksharov 10 months ago
parent 6482246358
commit 071276829b

@ -506,13 +506,13 @@ public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
@Override
public RFuture<Boolean> trySetPermitsAsync(int permits) {
RFuture<Boolean> future = commandExecutor.syncedEvalWithRetry(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local value = redis.call('get', KEYS[1]); " +
"local value = redis.call('get', KEYS[1]); " +
"if (value == false) then "
+ "redis.call('set', KEYS[1], ARGV[1]); "
+ "redis.call(ARGV[2], KEYS[2], ARGV[1]); "
+ "return 1;"
+ "end;"
+ "return 0;",
+ "redis.call('set', KEYS[1], ARGV[1]); "
+ "redis.call(ARGV[2], KEYS[2], ARGV[1]); "
+ "return 1;"
+ "end;"
+ "return 0;",
Arrays.asList(getRawName(), getChannelName()),
permits, getSubscribeService().getPublishCommand());
@ -528,6 +528,36 @@ public class RedissonSemaphore extends RedissonExpirable implements RSemaphore {
return future;
}
@Override
public boolean trySetPermits(int permits, Duration timeToLive) {
return get(trySetPermitsAsync(permits, timeToLive));
}
@Override
public RFuture<Boolean> trySetPermitsAsync(int permits, Duration timeToLive) {
RFuture<Boolean> future = commandExecutor.syncedEvalWithRetry(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local value = redis.call('get', KEYS[1]); " +
"if (value == false) then "
+ "redis.call('set', KEYS[1], ARGV[1], 'px', ARGV[3]); "
+ "redis.call(ARGV[2], KEYS[2], ARGV[1]); "
+ "return 1;"
+ "end;"
+ "return 0;",
Arrays.asList(getRawName(), getChannelName()),
permits, getSubscribeService().getPublishCommand(), timeToLive.toMillis());
if (LOGGER.isDebugEnabled()) {
future.thenAccept(r -> {
if (r) {
LOGGER.debug("permits set, permits: {}, name: {}", permits, getName());
} else {
LOGGER.debug("unable to set permits, permits: {}, name: {}", permits, getName());
}
});
}
return future;
}
@Override
public void addPermits(int permits) {
get(addPermitsAsync(permits));

@ -142,12 +142,22 @@ public interface RSemaphore extends RExpirable, RSemaphoreAsync {
/**
* Tries to set number of permits.
*
* @param permits - number of permits
* @param permits number of permits
* @return <code>true</code> if permits has been set successfully,
* otherwise <code>false</code> if permits were already set.
*/
boolean trySetPermits(int permits);
/**
* Tries to set number of permits with defined time to live.
*
* @param timeToLive time to live
* @param permits number of permits
* @return <code>true</code> if permits has been set successfully,
* otherwise <code>false</code> if permits were already set.
*/
boolean trySetPermits(int permits, Duration timeToLive);
/**
* Increases or decreases the number of available permits by defined value.
*

@ -83,11 +83,20 @@ public interface RSemaphoreAsync extends RExpirableAsync {
/**
* Tries to set number of permits.
*
* @param permits - number of permits
* @param permits number of permits
* @return <code>true</code> if permits has been set successfully, otherwise <code>false</code>.
*/
RFuture<Boolean> trySetPermitsAsync(int permits);
/**
* Tries to set number of permits with defined time to live.
*
* @param timeToLive time to live
* @param permits number of permits
* @return <code>true</code> if permits has been set successfully, otherwise <code>false</code>.
*/
RFuture<Boolean> trySetPermitsAsync(int permits, Duration timeToLive);
/**
* Use {@link #tryAcquireAsync(Duration)} instead
*

@ -85,11 +85,20 @@ public interface RSemaphoreReactive extends RExpirableReactive {
/**
* Tries to set number of permits.
*
* @param permits - number of permits
* @param permits number of permits
* @return <code>true</code> if permits has been set successfully, otherwise <code>false</code>.
*/
Mono<Boolean> trySetPermits(int permits);
/**
* Tries to set number of permits with defined time to live.
*
* @param timeToLive time to live
* @param permits number of permits
* @return <code>true</code> if permits has been set successfully, otherwise <code>false</code>.
*/
Mono<Boolean> trySetPermits(int permits, Duration timeToLive);
/**
* Use {@link #tryAcquire(Duration)} instead
*

@ -91,6 +91,15 @@ public interface RSemaphoreRx extends RExpirableRx {
*/
Single<Boolean> trySetPermits(int permits);
/**
* Tries to set number of permits with defined time to live.
*
* @param timeToLive time to live
* @param permits number of permits
* @return <code>true</code> if permits has been set successfully, otherwise <code>false</code>.
*/
Single<Boolean> trySetPermits(int permits, Duration timeToLive);
/**
* Use {@link #tryAcquire(Duration)} instead
*

@ -58,12 +58,21 @@ public class RedissonSemaphoreTest extends BaseConcurrentTest {
}
@Test
public void testTrySetPermits() {
public void testTrySetPermits() throws InterruptedException {
RSemaphore s = redisson.getSemaphore("test");
assertThat(s.trySetPermits(10)).isTrue();
assertThat(s.availablePermits()).isEqualTo(10);
assertThat(s.trySetPermits(15)).isFalse();
assertThat(s.availablePermits()).isEqualTo(10);
s.delete();
assertThat(s.isExists()).isFalse();
assertThat(s.trySetPermits(1, Duration.ofSeconds(2))).isTrue();
Thread.sleep(1000);
assertThat(s.availablePermits()).isEqualTo(1);
Thread.sleep(1000);
assertThat(s.availablePermits()).isZero();
assertThat(s.isExists()).isFalse();
}
@Test

Loading…
Cancel
Save