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

Support the MAXAGE option for CLIENT KILL #3754

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public static enum Keyword implements Rawable {
DELETE, LIBRARYNAME, WITHCODE, DESCRIPTION, GETKEYS, GETKEYSANDFLAGS, DOCS, FILTERBY, DUMP,
MODULE, ACLCAT, PATTERN, DOCTOR, LATEST, HISTORY, USAGE, SAMPLES, PURGE, STATS, LOADEX, CONFIG, ARGS, RANK,
NOW, VERSION, ADDR, SKIPME, USER, LADDR,
CHANNELS, NUMPAT, NUMSUB, SHARDCHANNELS, SHARDNUMSUB, NOVALUES;
CHANNELS, NUMPAT, NUMSUB, SHARDCHANNELS, SHARDNUMSUB, NOVALUES, MAXAGE;

private final byte[] raw;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public interface ClientCommands {
String clientKill(String ip, int port);

/**
* Close a given client connection.
* Close client connections based on certain selection parameters.
*
* @param params Connection info will be closed
* @return Close success return OK
* @param params Parameters defining what client connections to close.
* @return The number of client connections that were closed.
*/
long clientKill(ClientKillParams params);

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/params/ClientKillParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public ClientKillParams laddr(String ip, int port) {
return addParam(Keyword.LADDR, ip + ':' + port);
}

/**
* Kill clients older than {@code maxAge} seconds.
*
* @param maxAge Clients older than this number of seconds will be killed.
* @return The {@code ClientKillParams} instance, for call chaining.
*/
public ClientKillParams maxAge(long maxAge) {
return addParam(Keyword.MAXAGE, maxAge);
}

@Override
public void addParams(CommandArguments args) {
params.forEach(kv -> args.add(kv.getKey()).add(kv.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,36 @@ public void killAddrIpPort() {

@Test
public void killUser() {
Jedis client2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
client.aclSetUser("test_kill", "on", "+acl", ">password1");
try {
client2.auth("test_kill", "password1");
assertEquals(1, jedis.clientKill(new ClientKillParams().user("test_kill")));
assertDisconnected(client2);
} finally {
jedis.aclDelUser("test_kill");
try (Jedis client2 = new Jedis(hnp.getHost(), hnp.getPort(), 500)) {
client.aclSetUser("test_kill", "on", "+acl", ">password1");
try {
client2.auth("test_kill", "password1");
assertEquals(1, jedis.clientKill(new ClientKillParams().user("test_kill")));
assertDisconnected(client2);
} finally {
jedis.aclDelUser("test_kill");
}
}
gerzse marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void killMaxAge() throws InterruptedException {
long maxAge = 2;

// sleep twice the maxAge, to be sure
Thread.sleep(maxAge * 2 * 1000);

try (Jedis client2 = new Jedis(hnp.getHost(), hnp.getPort(), 500)) {
client2.auth("foobared");

long killedClients = jedis.clientKill(new ClientKillParams().maxAge(maxAge));

// The reality is that some tests leak clients, so we can't assert
// on the exact number of killed clients.
assertTrue(killedClients > 0);

assertDisconnected(client);
assertConnected(client2);
}
}

Expand Down Expand Up @@ -267,6 +289,10 @@ private void assertDisconnected(Jedis j) {
}
}

private void assertConnected(Jedis j) {
assertEquals("PONG", j.ping());
}

private String findInClientList() {
for (String clientInfo : jedis.clientList().split("\n")) {
if (pattern.matcher(clientInfo).find()) {
Expand Down
Loading