ip/session monitoring

This commit is contained in:
Arndt Brenschede 2021-04-18 17:35:50 +02:00
parent 6d68589353
commit 8206a1ae84
2 changed files with 56 additions and 5 deletions

View file

@ -0,0 +1,42 @@
package btools.server;
import java.util.HashMap;
import java.util.Map;
public class IpAccessMonitor
{
private static Object sync = new Object();
private static HashMap<String,Long> ipAccess = new HashMap<String,Long>();
private static long MAX_IDLE = 900000; // 15 minutes
public static boolean touchIpAccess( String ip )
{
long t = System.currentTimeMillis();
synchronized( sync )
{
Long lastTime = ipAccess.get( ip );
if ( lastTime == null || t - lastTime.longValue() > MAX_IDLE )
{
ipAccess.put( ip, Long.valueOf( t ) );
cleanup(t);
return true; // new session detected
}
ipAccess.put( ip, Long.valueOf( t ) ); // touch existing session
return false;
}
}
private static void cleanup( long t )
{
HashMap<String,Long> newMap = new HashMap<String,Long>(ipAccess.size());
for( Map.Entry<String,Long> e : ipAccess.entrySet() )
{
if ( t - e.getValue().longValue() <= MAX_IDLE )
{
newMap.put( e.getKey(), e.getValue() );
}
}
ipAccess = newMap;
}
}