You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
6.3 KiB
166 lines
6.3 KiB
16 years ago
|
// serverAccessTracker.java
|
||
|
// -------------------------------------
|
||
|
// (C) 2009 by Michael Peter Christen; mc@yacy.net
|
||
|
// first published on http://yacy.net
|
||
|
// Frankfurt, Germany, 20.02.2009
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU General Public License as published by
|
||
|
// the Free Software Foundation; either version 2 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
|
||
12 years ago
|
package net.yacy.server;
|
||
16 years ago
|
|
||
14 years ago
|
import java.util.Collection;
|
||
16 years ago
|
import java.util.Iterator;
|
||
|
import java.util.Map;
|
||
14 years ago
|
import java.util.NoSuchElementException;
|
||
13 years ago
|
import java.util.Queue;
|
||
16 years ago
|
import java.util.concurrent.ConcurrentHashMap;
|
||
13 years ago
|
import java.util.concurrent.LinkedBlockingQueue;
|
||
16 years ago
|
|
||
|
public class serverAccessTracker {
|
||
|
|
||
15 years ago
|
private static final long cleanupCycle = 60000; // 1 minute
|
||
13 years ago
|
|
||
15 years ago
|
private final long maxTrackingTime;
|
||
|
private final int maxTrackingCount;
|
||
|
private final int maxHostCount;
|
||
13 years ago
|
private final ConcurrentHashMap<String, Queue<Track>> accessTracker; // mappings from requesting host to an ArrayList of serverTrack-entries
|
||
16 years ago
|
private long lastCleanup;
|
||
13 years ago
|
|
||
15 years ago
|
public static class Track {
|
||
13 years ago
|
private final long time;
|
||
|
private final String path;
|
||
|
public Track(final long time, final String path) {
|
||
15 years ago
|
this.time = time;
|
||
|
this.path = path;
|
||
|
}
|
||
|
public long getTime() {
|
||
|
return this.time;
|
||
|
}
|
||
|
public String getPath() {
|
||
|
return this.path;
|
||
|
}
|
||
|
}
|
||
13 years ago
|
|
||
|
public serverAccessTracker(final long maxTrackingTime, final int maxTrackingCount, final int maxTrackingHostCount) {
|
||
16 years ago
|
this.maxTrackingTime = maxTrackingTime;
|
||
|
this.maxTrackingCount = maxTrackingCount;
|
||
|
this.maxHostCount = maxTrackingHostCount;
|
||
13 years ago
|
this.accessTracker = new ConcurrentHashMap<String, Queue<Track>>();
|
||
16 years ago
|
}
|
||
13 years ago
|
|
||
16 years ago
|
/*
|
||
|
* remove all entries from the access tracker where the age of the last access is greater than the given timeout
|
||
|
*/
|
||
13 years ago
|
private void cleanupAccessTracker() {
|
||
16 years ago
|
|
||
13 years ago
|
synchronized (this) {
|
||
|
if (System.currentTimeMillis() - this.lastCleanup < cleanupCycle) return; // avoid too many scans of the queues
|
||
|
this.lastCleanup = System.currentTimeMillis();
|
||
|
}
|
||
13 years ago
|
|
||
16 years ago
|
// clear entries which had no entry for the maxTrackingTime time
|
||
13 years ago
|
final Iterator<Map.Entry<String, Queue<Track>>> i = this.accessTracker.entrySet().iterator();
|
||
|
Queue<Track> track;
|
||
16 years ago
|
while (i.hasNext()) {
|
||
|
track = i.next().getValue();
|
||
14 years ago
|
clearTooOldAccess(track);
|
||
|
if (track.isEmpty()) {
|
||
16 years ago
|
// all entries are too old. delete the whole track
|
||
|
i.remove();
|
||
|
} else {
|
||
|
// check if the maxTrackingCount is exceeded
|
||
14 years ago
|
while (track.size() > this.maxTrackingCount) try {
|
||
16 years ago
|
// delete the oldest entries
|
||
14 years ago
|
track.remove();
|
||
13 years ago
|
} catch (final NoSuchElementException e) { break; } // concurrency may cause that the track is already empty
|
||
16 years ago
|
}
|
||
|
}
|
||
13 years ago
|
|
||
16 years ago
|
// if there are more entries left than maxTrackingCount, delete some.
|
||
13 years ago
|
while (this.accessTracker.size() > this.maxHostCount) {
|
||
16 years ago
|
// delete just any
|
||
13 years ago
|
final String key = this.accessTracker.keys().nextElement();
|
||
14 years ago
|
if (key == null) break; // may occur because of concurrency effects
|
||
13 years ago
|
this.accessTracker.remove(key);
|
||
16 years ago
|
}
|
||
14 years ago
|
}
|
||
16 years ago
|
|
||
14 years ago
|
/**
|
||
|
* compute the number of accesses to a given host in the latest time
|
||
|
* @param host the host that was accessed
|
||
|
* @param delta the time delta from now to the past where the access times shall be computed
|
||
|
* @return the number of accesses to the host in the given time span
|
||
|
*/
|
||
13 years ago
|
public int latestAccessCount(final String host, final long delta) {
|
||
|
final Collection<Track> timeList = accessTrack(host);
|
||
14 years ago
|
if (timeList == null) return 0;
|
||
13 years ago
|
final long time = System.currentTimeMillis() - delta;
|
||
14 years ago
|
int c = 0;
|
||
13 years ago
|
for (final Track l: timeList) if ( l != null && l.getTime() > time) c++;
|
||
14 years ago
|
return c;
|
||
16 years ago
|
}
|
||
13 years ago
|
|
||
13 years ago
|
private void clearTooOldAccess(final Queue<Track> access) {
|
||
13 years ago
|
final long time = System.currentTimeMillis() - this.maxTrackingTime;
|
||
|
final Iterator<Track> e = access.iterator();
|
||
14 years ago
|
Track l;
|
||
13 years ago
|
int max = access.size(); // ensure termination
|
||
|
while (e.hasNext() && max-- > 0) {
|
||
14 years ago
|
l = e.next();
|
||
|
if (l.getTime() <= time) e.remove();
|
||
16 years ago
|
}
|
||
16 years ago
|
}
|
||
13 years ago
|
|
||
16 years ago
|
public void track(final String host, String accessPath) {
|
||
|
// check storage size
|
||
14 years ago
|
if (System.currentTimeMillis() - this.lastCleanup > cleanupCycle) {
|
||
14 years ago
|
cleanupAccessTracker();
|
||
14 years ago
|
}
|
||
13 years ago
|
|
||
16 years ago
|
// learn that a specific host has accessed a specific path
|
||
|
if (accessPath == null) accessPath="NULL";
|
||
13 years ago
|
Queue<Track> track = this.accessTracker.get(host);
|
||
14 years ago
|
if (track == null) {
|
||
13 years ago
|
track = new LinkedBlockingQueue<Track>();
|
||
14 years ago
|
track.add(new Track(System.currentTimeMillis(), accessPath));
|
||
|
// add to tracker
|
||
13 years ago
|
this.accessTracker.put(host, track);
|
||
14 years ago
|
} else {
|
||
|
track.add(new Track(System.currentTimeMillis(), accessPath));
|
||
|
clearTooOldAccess(track);
|
||
|
}
|
||
16 years ago
|
}
|
||
13 years ago
|
|
||
14 years ago
|
public Collection<Track> accessTrack(final String host) {
|
||
16 years ago
|
// returns mapping from Long(accesstime) to path
|
||
13 years ago
|
|
||
13 years ago
|
final Queue<Track> access = this.accessTracker.get(host);
|
||
16 years ago
|
if (access == null) return null;
|
||
|
// clear too old entries
|
||
14 years ago
|
clearTooOldAccess(access);
|
||
|
if (access.isEmpty()) {
|
||
13 years ago
|
this.accessTracker.remove(host);
|
||
16 years ago
|
}
|
||
|
return access;
|
||
|
}
|
||
13 years ago
|
|
||
16 years ago
|
public Iterator<String> accessHosts() {
|
||
|
// returns an iterator of hosts in tracker (String)
|
||
13 years ago
|
final Map<String, Queue<Track>> accessTrackerClone = new ConcurrentHashMap<String, Queue<Track>>();
|
||
13 years ago
|
accessTrackerClone.putAll(this.accessTracker);
|
||
16 years ago
|
return accessTrackerClone.keySet().iterator();
|
||
|
}
|
||
|
}
|