From cd6c60eb727987ebc8679209c698ba011f1e41e8 Mon Sep 17 00:00:00 2001 From: dengliming Date: Tue, 12 Mar 2024 23:07:19 +0800 Subject: [PATCH 1/4] Add support CLIENT KILL [MAXAGE] --- src/main/java/io/lettuce/core/KillArgs.java | 32 +++++++++++++++++++ .../ServerCommandIntegrationTests.java | 13 ++++++++ 2 files changed, 45 insertions(+) diff --git a/src/main/java/io/lettuce/core/KillArgs.java b/src/main/java/io/lettuce/core/KillArgs.java index 505da24723..eea6508265 100644 --- a/src/main/java/io/lettuce/core/KillArgs.java +++ b/src/main/java/io/lettuce/core/KillArgs.java @@ -50,6 +50,8 @@ private enum Type { private String username; + private Long maxAge; + /** * Builder entry points for {@link KillArgs}. */ @@ -155,6 +157,17 @@ public static KillArgs typeSlave() { 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 8.0 + */ + public static KillArgs maxAge(Long maxAge) { + return new KillArgs().maxAge(maxAge); + } } /** @@ -236,6 +249,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 8.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}. * @@ -277,6 +305,10 @@ public void build(CommandArgs args) { if (username != null) { args.add("USER").add(username); } + + if (maxAge != null) { + args.add("MAXAGE").add(maxAge); + } } } diff --git a/src/test/java/io/lettuce/core/commands/ServerCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/ServerCommandIntegrationTests.java index 937295a679..cb68466469 100644 --- a/src/test/java/io/lettuce/core/commands/ServerCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/ServerCommandIntegrationTests.java @@ -29,6 +29,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; @@ -182,6 +183,18 @@ void clientKillUser() { redis.aclDeluser("test_kill"); } + @Test + void clientKillMaxAge() throws InterruptedException { + RedisCommands connection2 = client.connect().sync(); + // can not find other new command to use `@EnabledOnCommand` for now, so check the version + assumeTrue(RedisConditions.of(redis).hasVersionGreaterOrEqualsTo("8.0")); + + long maxAge = 2L; + // sleep for maxAge * 2 seconds, to be sure + TimeUnit.SECONDS.sleep(maxAge * 2); + assertThat(redis.clientKill(KillArgs.Builder.maxAge(maxAge))).isGreaterThan(0); + } + @Test void clientId() { assertThat(redis.clientId()).isNotNull(); From 3a69189cacc8389ee07537a1bb32fce8bc0a02e8 Mon Sep 17 00:00:00 2001 From: dengliming Date: Wed, 13 Mar 2024 21:33:04 +0800 Subject: [PATCH 2/4] polish --- src/main/java/io/lettuce/core/KillArgs.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/lettuce/core/KillArgs.java b/src/main/java/io/lettuce/core/KillArgs.java index eea6508265..1bc354b38c 100644 --- a/src/main/java/io/lettuce/core/KillArgs.java +++ b/src/main/java/io/lettuce/core/KillArgs.java @@ -163,7 +163,7 @@ public static KillArgs user(String username) { * * @return new {@link KillArgs} with {@literal MAXAGE} set. * @see KillArgs#maxAge(Long) - * @since 8.0 + * @since 6.3 */ public static KillArgs maxAge(Long maxAge) { return new KillArgs().maxAge(maxAge); @@ -254,7 +254,7 @@ public KillArgs type(Type type) { * * @param maxAge must not be {@code null}. * @return {@code this} {@link KillArgs}. - * @since 8.0 + * @since 6.3 */ public KillArgs maxAge(Long maxAge) { From bf9cc1bf3931a6bfa9ca68e46d9122682d87a700 Mon Sep 17 00:00:00 2001 From: dengliming Date: Sat, 4 May 2024 01:00:38 +0800 Subject: [PATCH 3/4] address review changes --- src/main/java/io/lettuce/core/KillArgs.java | 4 ++-- .../core/commands/ServerCommandIntegrationTests.java | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/lettuce/core/KillArgs.java b/src/main/java/io/lettuce/core/KillArgs.java index d21596c4ed..70700c60db 100644 --- a/src/main/java/io/lettuce/core/KillArgs.java +++ b/src/main/java/io/lettuce/core/KillArgs.java @@ -167,7 +167,7 @@ public static KillArgs user(String username) { * * @return new {@link KillArgs} with {@literal MAXAGE} set. * @see KillArgs#maxAge(Long) - * @since 6.3 + * @since 7.0 */ public static KillArgs maxAge(Long maxAge) { return new KillArgs().maxAge(maxAge); @@ -258,7 +258,7 @@ public KillArgs type(Type type) { * * @param maxAge must not be {@code null}. * @return {@code this} {@link KillArgs}. - * @since 6.3 + * @since 7.0 */ public KillArgs maxAge(Long maxAge) { diff --git a/src/test/java/io/lettuce/core/commands/ServerCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/ServerCommandIntegrationTests.java index 60481571e4..b9be862e79 100644 --- a/src/test/java/io/lettuce/core/commands/ServerCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/ServerCommandIntegrationTests.java @@ -189,14 +189,20 @@ void clientKillUser() { @Test void clientKillMaxAge() throws InterruptedException { - RedisCommands connection2 = client.connect().sync(); // can not find other new command to use `@EnabledOnCommand` for now, so check the version assumeTrue(RedisConditions.of(redis).hasVersionGreaterOrEqualsTo("8.0")); + RedisCommands 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 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 From 095236daf88fb0b0b6c18604bc4f63b7b7636ba1 Mon Sep 17 00:00:00 2001 From: dengliming Date: Sat, 4 May 2024 01:08:43 +0800 Subject: [PATCH 4/4] format code --- src/main/java/io/lettuce/core/KillArgs.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/io/lettuce/core/KillArgs.java b/src/main/java/io/lettuce/core/KillArgs.java index 70700c60db..d24325c504 100644 --- a/src/main/java/io/lettuce/core/KillArgs.java +++ b/src/main/java/io/lettuce/core/KillArgs.java @@ -172,6 +172,7 @@ public static KillArgs user(String username) { public static KillArgs maxAge(Long maxAge) { return new KillArgs().maxAge(maxAge); } + } /**