From e0114a38b4486dbc2770d88fcb8165e4984fb811 Mon Sep 17 00:00:00 2001 From: anotherJJz <470623352@qq.com> Date: Wed, 28 Feb 2024 09:54:23 +0800 Subject: [PATCH 1/4] Adds a new method `toIdentityString()` that returns the identifier info for the connection --- .../java/redis/clients/jedis/Connection.java | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 50243e20d7..1c315ae863 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -5,6 +5,7 @@ import java.io.Closeable; import java.io.IOException; import java.net.Socket; +import java.net.SocketAddress; import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.CharBuffer; @@ -37,6 +38,8 @@ public class Connection implements Closeable { private int soTimeout = 0; private int infiniteSoTimeout = 0; private boolean broken = false; + private boolean strValActive; + private String strVal; public Connection() { this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT); @@ -67,9 +70,43 @@ public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clie initializeFromClientConfig(clientConfig); } - @Override - public String toString() { - return "Connection{" + socketFactory + "}"; + public String toIdentityString() { + if (strValActive == broken && strVal != null) { + return strVal; + } + + int id = hashCode(); + SocketAddress remoteAddr = socket.getRemoteSocketAddress(); + SocketAddress localAddr = socket.getLocalSocketAddress(); + if (remoteAddr != null) { + StringBuilder buf = new StringBuilder(96) + .append("[id: 0x") + .append(id) + .append(", L:") + .append(localAddr) + .append(broken? " ! " : " - ") + .append("R:") + .append(remoteAddr) + .append(']'); + strVal = buf.toString(); + } else if (localAddr != null) { + StringBuilder buf = new StringBuilder(64) + .append("[id: 0x") + .append(id) + .append(", L:") + .append(localAddr) + .append(']'); + strVal = buf.toString(); + } else { + StringBuilder buf = new StringBuilder(16) + .append("[id: 0x") + .append(id) + .append(']'); + strVal = buf.toString(); + } + + strValActive = broken; + return strVal; } public final RedisProtocol getRedisProtocol() { From 1a1eb8319b6bd2f01b3578b9b2324e70654dd877 Mon Sep 17 00:00:00 2001 From: anotherJJz <470623352@qq.com> Date: Mon, 12 Aug 2024 17:12:05 +0800 Subject: [PATCH 2/4] Update src/main/java/redis/clients/jedis/Connection.java Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> --- src/main/java/redis/clients/jedis/Connection.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 68be622630..6ff973fae2 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -70,6 +70,11 @@ public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clie initializeFromClientConfig(clientConfig); } + @Override + public String toString() { + return "Connection{" + socketFactory + "}"; + } + public String toIdentityString() { if (strValActive == broken && strVal != null) { return strVal; From 1a456b58068a9e930fb77c23b7982e2706e8b6ba Mon Sep 17 00:00:00 2001 From: anotherJJz <470623352@qq.com> Date: Mon, 12 Aug 2024 18:24:59 +0800 Subject: [PATCH 3/4] fix NPE about invoke `toIdentityString` before connect --- .../java/redis/clients/jedis/Connection.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 6ff973fae2..ad40bc2dd2 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -81,11 +81,25 @@ public String toIdentityString() { } int id = hashCode(); + String classInfo = getClass().toString(); + + if (socket == null) { + StringBuilder buf = new StringBuilder(56) + .append("[") + .append(classInfo) + .append(", id: 0x") + .append(id) + .append(']'); + return buf.toString(); + } + SocketAddress remoteAddr = socket.getRemoteSocketAddress(); SocketAddress localAddr = socket.getLocalSocketAddress(); if (remoteAddr != null) { - StringBuilder buf = new StringBuilder(96) - .append("[id: 0x") + StringBuilder buf = new StringBuilder(101) + .append("[") + .append(classInfo) + .append(", id: 0x") .append(id) .append(", L:") .append(localAddr) @@ -96,15 +110,19 @@ public String toIdentityString() { strVal = buf.toString(); } else if (localAddr != null) { StringBuilder buf = new StringBuilder(64) - .append("[id: 0x") + .append("[") + .append(classInfo) + .append(", id: 0x") .append(id) .append(", L:") .append(localAddr) .append(']'); strVal = buf.toString(); } else { - StringBuilder buf = new StringBuilder(16) - .append("[id: 0x") + StringBuilder buf = new StringBuilder(56) + .append("[") + .append(classInfo) + .append(", id: 0x") .append(id) .append(']'); strVal = buf.toString(); From d92b0f9f7b89b636a0222e88c56c41053b4394f2 Mon Sep 17 00:00:00 2001 From: anotherJJz <470622352@qq.com> Date: Mon, 12 Aug 2024 20:10:43 +0800 Subject: [PATCH 4/4] add test case --- src/test/java/redis/clients/jedis/ConnectionTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/redis/clients/jedis/ConnectionTest.java b/src/test/java/redis/clients/jedis/ConnectionTest.java index 28eba8100c..c57248db53 100644 --- a/src/test/java/redis/clients/jedis/ConnectionTest.java +++ b/src/test/java/redis/clients/jedis/ConnectionTest.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import org.junit.After; +import org.junit.Assert; import org.junit.Test; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -40,4 +41,14 @@ public void checkCloseable() { client.connect(); client.close(); } + + @Test + public void checkIdentityString() { + client = new Connection("localhost", 6379); + Assert.assertFalse(client.toIdentityString().contains("6379")); + client.connect(); + Assert.assertTrue(client.toIdentityString().contains("6379")); + client.close(); + Assert.assertTrue(client.toIdentityString().contains("6379")); + } }