diff --git a/history.txt b/history.txt
index e9b6d3f4a2482841c8dd634fb1c5dd22d0c24271..6a502f39f2b01b65f6c1d03275810506d20db878 100644
--- a/history.txt
+++ b/history.txt
@@ -1,4 +1,13 @@
-$Id: history.txt,v 1.352 2005/12/09 03:05:48 jrandom Exp $
+$Id: history.txt,v 1.353 2005/12/13 04:38:52 jrandom Exp $
+
+2005-12-13  zzz
+    * Don't test tunnels expiring within 90 seconds
+    * Defer Test Tunnel jobs if job lag too large
+    * Use JobQueue.getMaxLag() rather than the jobQueue.jobLag stat to measure
+      job lag for tunnel build backoff, allowing for more agile handling
+      (since the stat is only updated once a minute)
+    * Use tunnel length override if all tunnels are expiring within one
+      minute.
 
 2005-12-13  jrandom
     * Fixed I2PSnark's handling of some torrent files to deal with those
diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java
index dff197ec626fd40fe2639050f7ca7e069425e294..487daebee19eff8ec9a5b1a8c452f979109407cd 100644
--- a/router/java/src/net/i2p/router/RouterVersion.java
+++ b/router/java/src/net/i2p/router/RouterVersion.java
@@ -15,9 +15,9 @@ import net.i2p.CoreVersion;
  *
  */
 public class RouterVersion {
-    public final static String ID = "$Revision: 1.308 $ $Date: 2005/12/08 15:53:41 $";
+    public final static String ID = "$Revision: 1.309 $ $Date: 2005/12/09 03:05:48 $";
     public final static String VERSION = "0.6.1.7";
-    public final static long BUILD = 2;
+    public final static long BUILD = 3;
     public static void main(String args[]) {
         System.out.println("I2P Router version: " + VERSION + "-" + BUILD);
         System.out.println("Router ID: " + RouterVersion.ID);
diff --git a/router/java/src/net/i2p/router/tunnel/pool/TestJob.java b/router/java/src/net/i2p/router/tunnel/pool/TestJob.java
index 5db5495147fb8df7e20349333cf23bc550665fda..1be33003a1d5e42bc11fc227a641141ec6b6558f 100644
--- a/router/java/src/net/i2p/router/tunnel/pool/TestJob.java
+++ b/router/java/src/net/i2p/router/tunnel/pool/TestJob.java
@@ -50,6 +50,14 @@ class TestJob extends JobImpl {
     }
     public String getName() { return "Test tunnel"; }
     public void runJob() {
+        long lag = getContext().jobQueue().getMaxLag();
+        if (lag > 3000) {
+            if (_log.shouldLog(Log.WARN))
+                _log.warn("Deferring test of " + _cfg + " due to job lag = " + lag);
+            getContext().statManager().addRateData("tunnel.testAborted", _cfg.getLength(), 0);
+            scheduleRetest();
+            return;
+        }
         _found = false;
         // note: testing with exploratory tunnels always, even if the tested tunnel
         // is a client tunnel (per _cfg.getDestination())
@@ -166,7 +174,7 @@ class TestJob extends JobImpl {
         _outTunnel = null;
         _replyTunnel = null;
         int delay = getDelay();
-        if (_cfg.getExpiration() > getContext().clock().now() + delay)
+        if (_cfg.getExpiration() > getContext().clock().now() + delay + (3 * getTestPeriod()) + 30*1000)
             requeue(delay);
     }
     
diff --git a/router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java b/router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java
index 34ec54c26937f6bade66f6c7b374a12a33062922..e6cb4ff0032d14d89bc774092e3f11b1c6614490 100644
--- a/router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java
+++ b/router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java
@@ -141,7 +141,7 @@ public class TunnelPool {
             if (maxBuild > 0 && build > maxBuild)
                 build = maxBuild;
             int buildThrottle = MAX_BUILDS_PER_MINUTE;
-            int lag = (int) _context.statManager().getRate("jobQueue.jobLag").getRate(60*1000).getAverageValue();
+            long lag = _context.jobQueue().getMaxLag();
             int netErrors = (int) _context.statManager().getRate("udp.sendException").getRate(60*1000).getLastEventCount();
             if (lag > 3 * 1000 || netErrors > 5) {
                 if (_log.shouldLog(Log.WARN))
@@ -161,10 +161,11 @@ public class TunnelPool {
             if (build <= 0) return 0;
 
             if ((_settings.isExploratory() && baseTarget > countUsableTunnels(1)) ||
-                ((!_settings.isExploratory()) && baseTarget > countUsableTunnels(0)))
-                    _settings.setLengthOverride(1);
-                else
-                    _settings.setLengthOverride(0);
+                ((!_settings.isExploratory()) && baseTarget > countUsableTunnels(0)) ||
+                ((!_settings.isExploratory()) && countUsableTunnels(1) == 0))
+                _settings.setLengthOverride(1);
+            else
+                _settings.setLengthOverride(0);
 
             int wanted = build;
             build = _manager.allocateBuilds(build);
@@ -540,7 +541,7 @@ public class TunnelPool {
             else
                 added = refreshBuilders(0, 1);
             if ( (added > 0) && (_log.shouldLog(Log.WARN)) )
-                _log.warn("Additional parallel rebuilding of tunnel for " + TunnelPool.this.toString());
+                _log.warn(added + " additional parallel rebuild(s) for " + TunnelPool.this.toString());
             requeue(30*1000);
         }
     }