Class ConnectionContext
java.lang.Object
com.loomcache.server.network.ConnectionContext
- All Implemented Interfaces:
AutoCloseable
Wraps a TCP connection to a peer with automatic resource management and metrics.
- Since:
- 1.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConnectionContext(String peerId, Socket socket) Creates a new connection context for a peer. -
Method Summary
Modifier and TypeMethodDescriptionlongConnection age in milliseconds since creation.voidclose()Closes the connection and all associated resources.longTotal bytes received on this connection.longTotal bytes sent on this connection.@Nullable StringExtract and cache the Common Name (CN) from the peer's TLS client certificate.@Nullable StringGet the client's IP address.booleanisClosed()booleanReturns true only after this connection has completed the cluster-member handshake.booleanCheck whether this connection has transport-level (mTLS) client authentication.booleanisTransportAuthenticatedAsPeer(String expectedPeerId) Check whether this connection's verified peer certificate matches the expected cluster peer identity.longTotal messages received on this connection.longTotal messages sent on this connection.voidrecordBytesReceived(long bytes) Record bytes received on this connection.voidrecordBytesSent(long bytes) Record bytes sent on this connection.voidRecord an inbound message received on this connection.voidSend a message to this peer.voidsetClientIp(@Nullable String ip) Set the client's IP address for per-IP tracking.toString()
-
Constructor Details
-
ConnectionContext
Creates a new connection context for a peer.- Parameters:
peerId- the peer identifier (non-null)socket- the underlying socket (non-null)- Throws:
NullPointerException- if peerId or socket is nullRuntimeException- if socket streams cannot be obtained
-
-
Method Details
-
send
Send a message to this peer. Thread-safe — uses ReentrantLock (not synchronized) to avoid pinning virtual threads during network I/O.- Parameters:
msg- the message to send (non-null)- Throws:
IOException- if the connection is closed or send failsNullPointerException- if msg is null
-
negotiatedWireCompression
-
close
public void close()Closes the connection and all associated resources. Safe to call multiple times (idempotent). Properly cleans up socket, input stream, and output stream.Use try-with-resources to ensure cleanup: try (var conn = new ConnectionContext(...)) { ... }
- Specified by:
closein interfaceAutoCloseable
-
isTransportAuthenticated
public boolean isTransportAuthenticated()Check whether this connection has transport-level (mTLS) client authentication. Returns true only if the underlying socket is an SSLSocket with a verified peer certificate.- Returns:
- true if the peer's identity was verified via TLS client auth
-
isClusterTransportTrusted
public boolean isClusterTransportTrusted()Returns true only after this connection has completed the cluster-member handshake. A valid mTLS certificate proves transport identity, but it is not enough by itself to authorize Raft, migration, or replication frames. -
isTransportAuthenticatedAsPeer
Check whether this connection's verified peer certificate matches the expected cluster peer identity. Accepts either a DNS/URI SAN or a subject CN equal to the expected peer ID.- Parameters:
expectedPeerId- the expected stable cluster peer ID- Returns:
- true if mTLS client auth succeeded and the certificate identity matches
-
getCertificateCommonName
Extract and cache the Common Name (CN) from the peer's TLS client certificate.Returns
nullif the connection is not TLS, if client auth was not performed, or if the certificate has no CN. The result is cached after the first successful extraction.- Returns:
- the CN string, or null if unavailable
-
getClientIp
Get the client's IP address.- Returns:
- the IP address, or null if not yet assigned
-
setClientIp
Set the client's IP address for per-IP tracking.- Parameters:
ip- the IP address (may be null)
-
isClosed
public boolean isClosed() -
ageMillis
public long ageMillis()Connection age in milliseconds since creation. -
recordMessageReceived
public void recordMessageReceived()Record an inbound message received on this connection. -
messagesSent
public long messagesSent()Total messages sent on this connection. -
messagesReceived
public long messagesReceived()Total messages received on this connection. -
getBytesReceived
public long getBytesReceived()Total bytes received on this connection. -
recordBytesReceived
public void recordBytesReceived(long bytes) Record bytes received on this connection. -
getBytesSent
public long getBytesSent()Total bytes sent on this connection. -
recordBytesSent
public void recordBytesSent(long bytes) Record bytes sent on this connection. -
toString
-