Skip to content

Commit

Permalink
Add support CLIENT KILL [MAXAGE] (#2782)
Browse files Browse the repository at this point in the history
* Add support CLIENT KILL [MAXAGE]

* polish

* address review changes

* format code
  • Loading branch information
dengliming committed May 14, 2024
1 parent 5031999 commit cc9d240
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/io/lettuce/core/KillArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ private enum Type {

private String username;

private Long maxAge;

/**
* Builder entry points for {@link KillArgs}.
*/
Expand Down Expand Up @@ -160,6 +162,17 @@ public static KillArgs user(String username) {
return new KillArgs().user(username);
}

/**
* Creates new {@link KillArgs} setting {@literal MAXAGE}.
*
* @return new {@link KillArgs} with {@literal MAXAGE} set.
* @see KillArgs#maxAge(Long)
* @since 7.0
*/
public static KillArgs maxAge(Long maxAge) {
return new KillArgs().maxAge(maxAge);
}

}

/**
Expand Down Expand Up @@ -241,6 +254,21 @@ public KillArgs type(Type type) {
return this;
}

/**
* Closes all the connections that are older than the specified age, in seconds.
*
* @param maxAge must not be {@code null}.
* @return {@code this} {@link KillArgs}.
* @since 7.0
*/
public KillArgs maxAge(Long maxAge) {

LettuceAssert.notNull(maxAge, "MaxAge must not be null");

this.maxAge = maxAge;
return this;
}

/**
* Closes all the connections that are authenticated with the specified ACL {@code username}.
*
Expand Down Expand Up @@ -282,6 +310,10 @@ public <K, V> void build(CommandArgs<K, V> args) {
if (username != null) {
args.add("USER").add(username);
}

if (maxAge != null) {
args.add("MAXAGE").add(maxAge);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import javax.inject.Inject;

import io.lettuce.test.condition.RedisConditions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -186,6 +187,24 @@ void clientKillUser() {
redis.aclDeluser("test_kill");
}

@Test
void clientKillMaxAge() throws InterruptedException {
// can not find other new command to use `@EnabledOnCommand` for now, so check the version
assumeTrue(RedisConditions.of(redis).hasVersionGreaterOrEqualsTo("8.0"));

RedisCommands<String, String> connection2 = client.connect().sync();
long inactiveId = connection2.clientId();
long maxAge = 2L;
// sleep for maxAge * 2 seconds, to be sure
TimeUnit.SECONDS.sleep(maxAge * 2);
RedisCommands<String, String> connection3 = client.connect().sync();
long activeId = connection3.clientId();
assertThat(redis.clientKill(KillArgs.Builder.maxAge(maxAge))).isGreaterThan(0);

assertThat(redis.clientList(ClientListArgs.Builder.ids(inactiveId))).isBlank();
assertThat(redis.clientList(ClientListArgs.Builder.ids(activeId))).isNotBlank();
}

@Test
void clientId() {
assertThat(redis.clientId()).isNotNull();
Expand Down

0 comments on commit cc9d240

Please sign in to comment.