* LeaseSet - code cleanup:

- Add exception to enforce max # of leases = 6, should be plenty
      - Rewrite TunnelPool.locked_buildNewLeaseSet() so it doesn't add lots of
        leases and then immediately remove them again, triggering
        the new leaseSet size exception
      - Remove the now unused LeaseSet.removeLease(lease) and
        LeaseSet.removeLease(index)
      - Store first and last expiration for efficiency
This commit is contained in:
zzz
2008-06-05 12:30:12 +00:00
parent 9c06bb3fca
commit db9db18bdf
4 changed files with 59 additions and 55 deletions

View File

@ -17,7 +17,7 @@ import net.i2p.CoreVersion;
public class RouterVersion {
public final static String ID = "$Revision: 1.548 $ $Date: 2008-02-10 15:00:00 $";
public final static String VERSION = "0.6.1.33";
public final static long BUILD = 2001;
public final static long BUILD = 2002;
public static void main(String args[]) {
System.out.println("I2P Router version: " + VERSION + "-" + BUILD);
System.out.println("Router ID: " + RouterVersion.ID);

View File

@ -430,7 +430,7 @@ public class TunnelPool {
}
/**
* Build a leaseSet with all of the tunnels that aren't about to expire
* Build a leaseSet with the required tunnels that aren't about to expire
*
*/
private LeaseSet locked_buildNewLeaseSet() {
@ -438,7 +438,7 @@ public class TunnelPool {
return null;
long expireAfter = _context.clock().now(); // + _settings.getRebuildPeriod();
LeaseSet ls = new LeaseSet();
List leases = new ArrayList(_tunnels.size());
for (int i = 0; i < _tunnels.size(); i++) {
TunnelInfo tunnel = (TunnelInfo)_tunnels.get(i);
if (tunnel.getExpiration() <= expireAfter)
@ -454,34 +454,37 @@ public class TunnelPool {
lease.setEndDate(new Date(tunnel.getExpiration()));
lease.setTunnelId(inId);
lease.setGateway(gw);
ls.addLease(lease);
leases.add(lease);
}
int wanted = _settings.getQuantity();
if (ls.getLeaseCount() < wanted) {
if (leases.size() < wanted) {
if (_log.shouldLog(Log.WARN))
_log.warn(toString() + ": Not enough leases (" + ls.getLeaseCount() + ", wanted " + wanted + ")");
_log.warn(toString() + ": Not enough leases (" + leases.size() + ", wanted " + wanted + ")");
return null;
} else {
// linear search to trim down the leaseSet, removing the ones that
// will expire the earliest. cheaper than a tree for this size
while (ls.getLeaseCount() > wanted) {
while (leases.size() > wanted) {
int earliestIndex = -1;
long earliestExpiration = -1;
for (int i = 0; i < ls.getLeaseCount(); i++) {
Lease cur = ls.getLease(i);
for (int i = 0; i < leases.size(); i++) {
Lease cur = (Lease) leases.get(i);
if ( (earliestExpiration < 0) || (cur.getEndDate().getTime() < earliestExpiration) ) {
earliestIndex = i;
earliestExpiration = cur.getEndDate().getTime();
}
}
if (_log.shouldLog(Log.DEBUG))
_log.debug(toString() + ": Dropping older lease from the leaseSet: " + earliestIndex + " out of " + ls.getLeaseCount());
ls.removeLease(earliestIndex);
leases.remove(earliestIndex);
}
return ls;
}
LeaseSet ls = new LeaseSet();
for (int i = 0; i < leases.size(); i++)
ls.addLease((Lease) leases.get(i));
if (_log.shouldLog(Log.INFO))
_log.info(toString() + ": built new leaseSet: " + ls);
return ls;
}
public long getLifetimeProcessed() { return _lifetimeProcessed; }