Revert "Revert an idle sockets change to trigger reliability bot.  Will revert again soon."

Review URL: http://codereview.chromium.org/2761001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49146 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/net/socket/client_socket_pool_base.cc b/net/socket/client_socket_pool_base.cc
index 594a858..80857e5 100644
--- a/net/socket/client_socket_pool_base.cc
+++ b/net/socket/client_socket_pool_base.cc
@@ -212,6 +212,23 @@
   CHECK(handle);
   Group& group = group_map_[group_name];
 
+  // Can we make another active socket now?
+  if (ReachedMaxSocketsLimit() ||
+      !group.HasAvailableSocketSlot(max_sockets_per_group_)) {
+    if (ReachedMaxSocketsLimit()) {
+      // We could check if we really have a stalled group here, but it requires
+      // a scan of all groups, so just flip a flag here, and do the check later.
+      may_have_stalled_group_ = true;
+
+      request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS,
+                                  NULL);
+    } else {
+      request->net_log().AddEvent(
+          NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL);
+    }
+    return ERR_IO_PENDING;
+  }
+
   // Try to reuse a socket.
   while (!group.idle_sockets.empty()) {
     IdleSocket idle_socket = group.idle_sockets.back();
@@ -229,26 +246,6 @@
     delete idle_socket.socket;
   }
 
-  // Can we make another active socket now?
-  if (!group.HasAvailableSocketSlot(max_sockets_per_group_)) {
-    request->net_log().AddEvent(
-        NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL);
-    return ERR_IO_PENDING;
-  }
-
-  if (ReachedMaxSocketsLimit()) {
-    if (idle_socket_count() > 0) {
-      CloseOneIdleSocket();
-    } else {
-      // We could check if we really have a stalled group here, but it requires
-      // a scan of all groups, so just flip a flag here, and do the check later.
-      may_have_stalled_group_ = true;
-      request->net_log().AddEvent(
-          NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL);
-      return ERR_IO_PENDING;
-    }
-  }
-
   // See if we already have enough connect jobs or sockets that will be released
   // soon.
   if (group.HasReleasingSockets()) {
@@ -535,14 +532,10 @@
     int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name);
     if (stalled_group_count >= 1) {
       if (ReachedMaxSocketsLimit()) {
-        if (idle_socket_count() > 0) {
-          CloseOneIdleSocket();
-        } else {
-          // We can't activate more sockets since we're already at our global
-          // limit.
-          may_have_stalled_group_ = true;
-          return;
-        }
+        // We can't activate more sockets since we're already at our global
+        // limit.
+        may_have_stalled_group_ = true;
+        return;
       }
 
       ProcessPendingRequest(top_group_name, top_group);
@@ -680,10 +673,8 @@
         (stalled_group_count == 1 && top_group->num_releasing_sockets == 0)) {
       may_have_stalled_group_ = false;
     }
-    if (stalled_group_count >= 1) {
-      CHECK_GE(1, idle_socket_count());
+    if (stalled_group_count >= 1)
       ProcessPendingRequest(top_group_name, top_group);
-    }
   } else if (!group->pending_requests.empty()) {
     ProcessPendingRequest(group_name, group);
     // |group| may no longer be valid after this point.  Be careful not to
@@ -777,8 +768,7 @@
 
 bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
   // Each connecting socket will eventually connect and be handed out.
-  int total = handed_out_socket_count_ + connecting_socket_count_ +
-      idle_socket_count();
+  int total = handed_out_socket_count_ + connecting_socket_count_;
   DCHECK_LE(total, max_sockets_);
   if (total < max_sockets_)
     return false;
@@ -786,27 +776,6 @@
   return true;
 }
 
-void ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
-  CHECK_GT(idle_socket_count(), 0);
-
-  for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
-    Group& group = i->second;
-
-    if (!group.idle_sockets.empty()) {
-      std::deque<IdleSocket>::iterator j = group.idle_sockets.begin();
-      delete j->socket;
-      group.idle_sockets.erase(j);
-      DecrementIdleCount();
-      if (group.IsEmpty())
-        group_map_.erase(i);
-
-      return;
-    }
-  }
-
-  LOG(DFATAL) << "No idle socket found to close!.";
-}
-
 }  // namespace internal
 
 }  // namespace net