From 61d5f4629590b5fc32fca162ab373359a828deb8 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Tue, 24 Sep 2013 12:40:35 +0000
Subject: [PATCH]  * Streaming: Fix active stream counting so it doesn't count
 streams    that are closed and in TIME-WAIT state. Also, break out of the   
 counting loop as soon as we know the answer. (Ticket #1039)

---
 .../client/streaming/ConnectionManager.java   | 25 +++++++++++++------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionManager.java b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionManager.java
index 0f837ad34a..6adca023e4 100644
--- a/apps/streaming/java/src/net/i2p/client/streaming/ConnectionManager.java
+++ b/apps/streaming/java/src/net/i2p/client/streaming/ConnectionManager.java
@@ -361,18 +361,29 @@ class ConnectionManager {
     private boolean locked_tooManyStreams() {
         int max = _defaultOptions.getMaxConns();
         if (max <= 0) return false;
-        if (_connectionByInboundId.size() < max) return false;
+        int size = _connectionByInboundId.size();
+        if (size < max) return false;
+        // count both so we can break out of the for loop asap
         int active = 0;
+        int inactive = 0;
+        int maxInactive = size - max;
         for (Connection con : _connectionByInboundId.values()) {
-            if (con.getIsConnected())
-                active++;
+            // ticket #1039
+            if (con.getIsConnected() &&
+                !(con.getCloseSentOn() > 0 && con.getCloseReceivedOn() > 0)) {
+                if (++active >= max)
+                    return true;
+            } else {
+                if (++inactive > maxInactive)
+                    return false;
+            }
         }
         
-        if ( (_connectionByInboundId.size() > 100) && (_log.shouldLog(Log.INFO)) )
-            _log.info("More than 100 connections!  " + active
-                      + " total: " + _connectionByInboundId.size());
+        //if ( (_connectionByInboundId.size() > 100) && (_log.shouldLog(Log.INFO)) )
+        //    _log.info("More than 100 connections!  " + active
+        //              + " total: " + _connectionByInboundId.size());
 
-        return (active >= max);
+        return false;
     }
     
     /**
-- 
GitLab