Skip to content

Commit

Permalink
Adds a new method toIdentityString() that returns the identifier in…
Browse files Browse the repository at this point in the history
…fo for the connection
  • Loading branch information
jjz921024 committed Aug 12, 2024
1 parent f64dbb4 commit e0114a3
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 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 @@ -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() {
Expand Down

0 comments on commit e0114a3

Please sign in to comment.