Skip to content

Commit

Permalink
Get enriched Connection information (#3745)
Browse files Browse the repository at this point in the history
* Adds a new method `toIdentityString()` that returns the identifier info for the connection

* Update src/main/java/redis/clients/jedis/Connection.java

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

* fix NPE about invoke `toIdentityString` before connect

* add test case

---------

Co-authored-by: M Sazzadul Hoque <[email protected]>
Co-authored-by: anotherJJz <[email protected]>
  • Loading branch information
3 people authored Aug 12, 2024
1 parent 2af43de commit cd9a1ab
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -72,6 +75,63 @@ public String toString() {
return "Connection{" + socketFactory + "}";
}

public String toIdentityString() {
if (strValActive == broken && strVal != null) {
return strVal;
}

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(101)
.append("[")
.append(classInfo)
.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("[")
.append(classInfo)
.append(", id: 0x")
.append(id)
.append(", L:")
.append(localAddr)
.append(']');
strVal = buf.toString();
} else {
StringBuilder buf = new StringBuilder(56)
.append("[")
.append(classInfo)
.append(", id: 0x")
.append(id)
.append(']');
strVal = buf.toString();
}

strValActive = broken;
return strVal;
}

public final RedisProtocol getRedisProtocol() {
return protocol;
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/redis/clients/jedis/ConnectionTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit cd9a1ab

Please sign in to comment.