Skip to content

Commit

Permalink
Support the MAXAGE option for CLIENT KILL (#3754)
Browse files Browse the repository at this point in the history
* Support the MAXAGE option for CLIENT KILL

Starting with Redis 7.6, the CLIENT KILL command has a new option, called MAXAGE, to kill clients older than a given age. Add support for this new option.

* Ensure clients are closed in some tests

* More concise unit test code

Co-authored-by: M Sazzadul Hoque <[email protected]>

---------

Co-authored-by: Gabriel Erzse <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>
  • Loading branch information
3 people authored Mar 7, 2024
1 parent 25acc8d commit 3dd2dd2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
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,17 +222,38 @@ public void killAddrIpPort() {

@Test
public void killUser() {
Jedis client2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
client.aclSetUser("test_kill", "on", "+acl", ">password1");
try {
try (Jedis client2 = new Jedis(hnp.getHost(), hnp.getPort(), 500)) {
client2.auth("test_kill", "password1");

assertEquals(1, jedis.clientKill(new ClientKillParams().user("test_kill")));
assertDisconnected(client2);
} finally {
jedis.aclDelUser("test_kill");
}
}

@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);
}
}

@Test
public void clientInfo() {
String info = client.clientInfo();
Expand Down Expand Up @@ -267,6 +288,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

0 comments on commit 3dd2dd2

Please sign in to comment.