コード例 #1
0
        /// <devdoc>
        ///    <para>
        ///       Used by the ServicePoint to find a free or new Connection
        ///       for use in making Requests.
        ///    </para>
        /// </devdoc>
        public Connection FindConnection(string connName)
        {
            Connection leastbusyConnection  = null;
            Connection newConnection        = null;
            bool       freeConnectionsAvail = false;

            GlobalLog.Print("ConnectionGroup::FindConnection [" + connName + "] m_ConnectionList.Count:" + m_ConnectionList.Count.ToString());

            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.
                //
                bool completed;
                do
                {
                    int minBusyCount = Int32.MaxValue;
                    completed = true; // by default, only once through the outer loop
                    foreach (WeakReference currentConnectionReference in m_ConnectionList)
                    {
                        Connection currentConnection = null;
                        if (currentConnectionReference != null)
                        {
                            currentConnection = currentConnectionReference.Target as Connection;
                        }
                        //
                        // If the weak reference is alive, see if its not busy,
                        //  otherwise make sure to remove it from the list
                        //
                        if (currentConnection != null)
                        {
                            GlobalLog.Print("ConnectionGroup::FindConnection currentConnection.BusyCount:" + currentConnection.BusyCount.ToString());
                            if (currentConnection.BusyCount < minBusyCount)
                            {
                                leastbusyConnection = currentConnection;
                                minBusyCount        = currentConnection.BusyCount;
                                if (minBusyCount == 0)
                                {
                                    freeConnectionsAvail = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            m_ConnectionList.Remove(currentConnectionReference);
                            completed = false;
                            // now start iterating again because we changed the ArrayList
                            break;
                        }
                    }
                } while (!completed);


                //
                // If there is NOT a Connection free, then we allocate a new Connection
                //
                if (!freeConnectionsAvail && CurrentConnections < InternalConnectionLimit)
                {
                    //
                    // 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::FindConnection [returning new Connection] freeConnectionsAvail:" + freeConnectionsAvail.ToString() + " CurrentConnections:" + CurrentConnections.ToString() + " InternalConnectionLimit:" + InternalConnectionLimit.ToString());

                    newConnection =
                        new Connection(
                            this,
                            m_ServicePoint,
                            m_IPAddress,
                            m_ServicePoint.ProtocolVersion,
                            m_ServicePoint.SupportsPipelining);
                }
                else
                {
                    //
                    // All connections are busy, use the least busy one
                    //

                    GlobalLog.Print("ConnectionGroup::FindConnection [returning leastbusyConnection] freeConnectionsAvail:" + freeConnectionsAvail.ToString() + " CurrentConnections:" + CurrentConnections.ToString() + " InternalConnectionLimit:" + InternalConnectionLimit.ToString());
                    GlobalLog.Assert(leastbusyConnection != null, "Connect.leastbusyConnection != null", "All connections have BusyCount equal to Int32.MaxValue");

                    newConnection = leastbusyConnection;
                }
            }

            return(newConnection);
        }
コード例 #2
0
        /// <devdoc>
        ///    <para>
        ///       Attempts to match a request with a connection, if a connection is unassigned ie not locked with
        ///         a request, then the least busy connections is returned in "leastbusyConnection."  If the
        ///         connection limit allows, and all connections are busy, a new one is allocated and returned.
        ///
        ///     RETURNS: a Connection shown to match a previously locked Request/Connection (OTHERWISE)
        ///              leasebusyConnection - will contain a newly allocated Connection or least Busy one
        ///              suiteable for requests.
        ///
        ///     NOTE: For Whidbey: try to integrate this code into FindConnection()
        ///    </para>
        /// </devdoc>
        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.
                //
                bool completed;
                do
                {
                    minBusyCount = Int32.MaxValue;
                    completed    = true; // by default, only once through the outer loop
                    foreach (WeakReference currentConnectionReference in m_ConnectionList)
                    {
                        Connection currentConnection = null;
                        if (currentConnectionReference != null)
                        {
                            currentConnection = currentConnectionReference.Target as Connection;
                        }
                        //
                        // If the weak reference is alive, see if its not busy,
                        //  otherwise make sure to remove it from the list
                        //
                        if (currentConnection != null)
                        {
                            GlobalLog.Print("ConnectionGroup::FindConnection currentConnection.BusyCount:" + currentConnection.BusyCount.ToString());

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

                            GlobalLog.Print("ConnectionGroup::FindConnection: 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;
                                }
                            }
                        }
                        else
                        {
                            m_ConnectionList.Remove(currentConnectionReference);
                            completed = false;
                            // now start iterating again because we changed the ArrayList
                            break;
                        }
                    }
                } while (!completed);

                //
                // If there is NOT a Connection free, then we allocate a new Connection
                //
                if (!freeConnectionsAvail && CurrentConnections < InternalConnectionLimit)
                {
                    //
                    // 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::FindConnection [returning new Connection] CurrentConnections:" + CurrentConnections.ToString() + " InternalConnectionLimit:" + InternalConnectionLimit.ToString());

                    leastbusyConnection =
                        new Connection(
                            this,
                            m_ServicePoint,
                            m_IPAddress,
                            m_ServicePoint.ProtocolVersion,
                            m_ServicePoint.SupportsPipelining
                            );
                }
            }

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