Package com.oorian.security
Class ConnectionTracker
java.lang.Object
com.oorian.security.ConnectionTracker
Tracks active WebSocket connections per session and per IP address.
Used to enforce connection limits configured via
Application.setMaxConnectionsPerSession(int) and
Application.setMaxConnectionsPerIp(int). Limits of 0 mean unlimited.
This class is thread-safe. All operations use concurrent data structures and atomic counters.
Usage:
// Check before allowing a new WebSocket connection
if (ConnectionTracker.canConnect(sessionId, ipAddress))
{
ConnectionTracker.onConnect(sessionId, ipAddress);
// Allow connection
}
else
{
// Reject connection
}
// When connection closes
ConnectionTracker.onDisconnect(sessionId, ipAddress);
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanConnect(String sessionId, String ipAddress) Checks whether a new connection is allowed for the given session and IP address.static voidclear()Clears all connection tracking data.static intgetIpConnectionCount(String ipAddress) Returns the current number of active connections from the given IP address.static intgetSessionConnectionCount(String sessionId) Returns the current number of active connections for the given session.static voidRecords a new connection for the given session and IP address.static voidonDisconnect(String sessionId, String ipAddress) Records a closed connection for the given session and IP address.
-
Method Details
-
canConnect
Checks whether a new connection is allowed for the given session and IP address.A connection is allowed if both the per-session and per-IP limits have not been reached. A limit of 0 means unlimited.
- Parameters:
sessionId- the session identifieripAddress- the client's IP address- Returns:
trueif the connection is allowed,falseif a limit would be exceeded
-
onConnect
Records a new connection for the given session and IP address.Increments the connection counters for both the session and the IP address.
- Parameters:
sessionId- the session identifieripAddress- the client's IP address
-
onDisconnect
Records a closed connection for the given session and IP address.Decrements the connection counters. If a counter reaches zero, the entry is removed to prevent memory leaks from accumulated keys.
- Parameters:
sessionId- the session identifieripAddress- the client's IP address
-
getSessionConnectionCount
Returns the current number of active connections for the given session.- Parameters:
sessionId- the session identifier- Returns:
- the number of active connections, or 0 if none
-
getIpConnectionCount
Returns the current number of active connections from the given IP address.- Parameters:
ipAddress- the IP address- Returns:
- the number of active connections, or 0 if none
-
clear
public static void clear()Clears all connection tracking data.Called during application shutdown to release resources.
-