Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support CLIENT KILL [MAXAGE] #2782

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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