edu.sc.seis.seisFile.fdsnws.TimeQueryLog Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of seisFile Show documentation
Show all versions of seisFile Show documentation
A library for reading and writing seismic file formats in java.
The newest version!
package edu.sc.seis.seisFile.fdsnws;
import java.net.URI;
import java.time.Instant;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import edu.sc.seis.seisFile.TimeUtils;
public class TimeQueryLog {
/**
* Increments number of recent queries to the given uri.
*
* @param uri request url
* @return number of queries to host in last second.
*/
public static int add(URI uri) {
totalQueries++;
QueryTime current = new QueryTime(uri);
LinkedList byHost = null;
synchronized(recentQueries) {
byHost = recentQueries.get(uri.getHost());
if (byHost == null) {
byHost = new LinkedList();
recentQueries.put(uri.getHost(), byHost);
}
}
int numLastSec = 1; // start at one to include current query
Instant secondAgo = Instant.now().minus(TimeUtils.ONE_SECOND);
synchronized(byHost) {
for (Iterator iterator = byHost.iterator(); iterator.hasNext();) {
QueryTime timeQuery = iterator.next();
if (timeQuery.getWhen().isAfter(secondAgo)) {
numLastSec++;
} else {
iterator.remove();
}
}
byHost.add(current);
if (numLastSec >= 10) {
logger.warn("More than 10 queries in last second for "+uri.getHost()+"!!! " + numLastSec);
List toPrintList = byHost;
if (numLastSec > 10) {
toPrintList = byHost.subList(0, 10);
}
for (QueryTime timeQuery : toPrintList) {
logger.warn(" " + timeQuery.getWhen() + " " + timeQuery.getURI());
}
if (numLastSec > 10) {
logger.warn("...plus "+(numLastSec-10)+" more.");
}
logger.warn("Taking a little break to give the service time to catch its breath.");
try {
synchronized(recentQueries) {
Thread.sleep(1000);
}
} catch(InterruptedException e) {
// oh well....
}
}
return numLastSec;
}
}
static int totalQueries = 0;
static HashMap> recentQueries = new HashMap>();
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TimeQueryLog.class);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy