System.Net.ConnectionGroup.FindMatchingConnection C# (CSharp) Method

FindMatchingConnection() private method

private FindMatchingConnection ( HttpWebRequest request, string connName, Connection &leastbusyConnection ) : Connection
request HttpWebRequest
connName string
leastbusyConnection Connection
return Connection
        private Connection FindMatchingConnection(HttpWebRequest request, string connName, out Connection leastbusyConnection) {
            int minBusyCount = Int32.MaxValue;
            bool freeConnectionsAvail = false;

            leastbusyConnection = null;

            lock (m_ConnectionList) {

                //
                // go through the list of open connections to this service point and pick
                // the first empty one or, if none is empty, pick the least busy one.
                //
                minBusyCount = Int32.MaxValue;
                foreach (Connection currentConnection in m_ConnectionList) {
                    GlobalLog.Print("ConnectionGroup::FindMatchingConnection currentConnection.BusyCount:" + currentConnection.BusyCount.ToString());

                    if (currentConnection.LockedRequest == request) {
                        leastbusyConnection = currentConnection;
                        return currentConnection;
                    }

                    GlobalLog.Print("ConnectionGroup::FindMatchingConnection: lockedRequest# " + ((currentConnection.LockedRequest == null) ? "null" : currentConnection.LockedRequest.GetHashCode().ToString()));
                    if (currentConnection.BusyCount < minBusyCount && currentConnection.LockedRequest == null) {
                        leastbusyConnection = currentConnection;
                        minBusyCount = currentConnection.BusyCount;
                        if (minBusyCount == 0) {
                            freeConnectionsAvail = true;
                        }
                    }
                }

                //
                // If there is NOT a Connection free, then we allocate a new Connection
                //
                if (!freeConnectionsAvail && CurrentConnections < ConnectionLimit) {
                    //
                    // If we can create a new connection, then do it,
                    // this may have complications in pipeling because
                    // we may wish to optimize this case by actually
                    // using existing connections, rather than creating new ones
                    //
                    // Note: this implicately results in a this.Associate being called.
                    //

                    GlobalLog.Print("ConnectionGroup::FindMatchingConnection [returning new Connection] CurrentConnections:" + CurrentConnections.ToString() + " ConnectionLimit:" + ConnectionLimit.ToString());
                    leastbusyConnection = new Connection(this);
                }
            }

            return null; // only if we have a locked Connection that matches can return non-null
        }