Changeset 7765


Ignore:
Timestamp:
Jan 20, 2009, 3:47:25 PM (12 years ago)
Author:
charles
Message:

(trunk libT) better implementation of r7761 to take into account feedback from repattila in the forums

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-mgr.c

    r7763 r7765  
    19841984***/
    19851985
    1986 static int
    1987 shouldPeerBeClosed( const Torrent * t,
    1988                     const tr_peer * peer,
    1989                     int             peerCount )
     1986typedef enum
     1987{
     1988    TR_CAN_KEEP,
     1989    TR_CAN_CLOSE,
     1990    TR_MUST_CLOSE,
     1991}
     1992tr_close_type_t;
     1993
     1994static tr_close_type_t
     1995shouldPeerBeClosed( const Torrent    * t,
     1996                    const tr_peer    * peer,
     1997                    int                peerCount )
    19901998{
    19911999    const tr_torrent *       tor = t->tor;
     
    19982006        tordbg( t, "purging peer %s because its doPurge flag is set",
    19992007                tr_peerIoAddrStr( &atom->addr, atom->port ) );
    2000         return TRUE;
     2008        return TR_MUST_CLOSE;
    20012009    }
    20022010
     
    20212029            tordbg( t, "purging peer %s because we're both seeds",
    20222030                    tr_peerIoAddrStr( &atom->addr, atom->port ) );
    2023             return TRUE;
     2031            return TR_MUST_CLOSE;
    20242032        }
    20252033    }
     
    20422050            tordbg( t, "purging peer %s because it's been %d secs since we shared anything",
    20432051                       tr_peerIoAddrStr( &atom->addr, atom->port ), idleTime );
    2044             return TRUE;
    2045         }
    2046     }
    2047 
    2048     return FALSE;
     2052            return TR_CAN_CLOSE;
     2053        }
     2054    }
     2055
     2056    return TR_CAN_KEEP;
    20492057}
    20502058
    20512059static tr_peer **
    2052 getPeersToClose( Torrent * t, int * setmeSize )
     2060getPeersToClose( Torrent * t, tr_close_type_t closeType, int * setmeSize )
    20532061{
    20542062    int i, peerCount, outsize;
     
    20592067
    20602068    for( i = outsize = 0; i < peerCount; ++i )
    2061         if( shouldPeerBeClosed( t, peers[i], peerCount ) )
     2069        if( shouldPeerBeClosed( t, peers[i], peerCount ) == closeType )
    20622070            ret[outsize++] = peers[i];
    20632071
     
    21872195}
    21882196
     2197static void
     2198closePeer( Torrent * t, tr_peer * peer )
     2199{
     2200    struct peer_atom * atom;
     2201
     2202    assert( t != NULL );
     2203    assert( peer != NULL );
     2204
     2205    /* if we transferred piece data, then they might be good peers,
     2206       so reset their `numFails' weight to zero.  otherwise we connected
     2207       to them fruitlessly, so mark it as another fail */
     2208    atom = getExistingAtom( t, &peer->addr );
     2209    if( atom->piece_data_time )
     2210        atom->numFails = 0;
     2211    else
     2212        ++atom->numFails;
     2213
     2214    tordbg( t, "removing bad peer %s", tr_peerIoGetAddrStr( peer->io ) );
     2215    removePeer( t, peer );
     2216}
     2217
    21892218static int
    21902219reconnectPulse( void * vtorrent )
     
    22102239    else
    22112240    {
    2212         int i, nCandidates, nBad;
    2213         struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates );
    2214         struct tr_peer ** connections = getPeersToClose( t, &nBad );
     2241        int i;
     2242        int canCloseCount;
     2243        int mustCloseCount;
     2244        int candidateCount;
    22152245        int maxCandidates;
    2216 
    2217         maxCandidates = nCandidates;
     2246        struct tr_peer ** canClose = getPeersToClose( t, TR_CAN_CLOSE, &canCloseCount );
     2247        struct tr_peer ** mustClose = getPeersToClose( t, TR_MUST_CLOSE, &mustCloseCount );
     2248        struct peer_atom ** candidates = getPeerCandidates( t, &candidateCount );
     2249
     2250        tordbg( t, "reconnect pulse for [%s]: "
     2251                   "%d must-close connections, "
     2252                   "%d can-close connections, "
     2253                   "%d connection candidates, "
     2254                   "%d atoms, "
     2255                   "max per pulse is %d",
     2256                   t->tor->info.name,
     2257                   mustCloseCount,
     2258                   canCloseCount,
     2259                   candidateCount,
     2260                   tr_ptrArraySize( &t->pool ),
     2261                   MAX_RECONNECTIONS_PER_PULSE );
     2262
     2263        /* disconnect the really bad peers */
     2264        for( i=0; i<mustCloseCount; ++i )
     2265            closePeer( t, mustClose[i] );
     2266
     2267        /* decide how many peers can we try to add in this pass */
     2268        maxCandidates = candidateCount;
    22182269        maxCandidates = MIN( maxCandidates, MAX_RECONNECTIONS_PER_PULSE );
    22192270        maxCandidates = MIN( maxCandidates, getMaxPeerCount( t->tor ) - getPeerCount( t ) );
    22202271        maxCandidates = MIN( maxCandidates, MAX_CONNECTIONS_PER_SECOND - newConnectionsThisSecond );
    22212272
    2222         //if( nBad || nCandidates )
    2223             tordbg( t, "reconnect pulse for [%s]: %d bad connections, "
    2224                     "%d connection candidates, %d atoms, max per pulse is %d",
    2225                     t->tor->info.name, nBad, nCandidates,
    2226                     tr_ptrArraySize( &t->pool ),
    2227                     (int)MAX_RECONNECTIONS_PER_PULSE );
    2228 
    2229         /* disconnect some peers to make room for better ones.
    2230            if we transferred piece data, then they might be good peers,
    2231            so reset their `numFails' weight to zero.  otherwise we connected
    2232            to them fruitlessly, so mark it as another fail */
    2233         for( i = 0; ( i < nBad ) && ( i < maxCandidates ) ; ++i ) {
    2234             tr_peer * peer = connections[i];
    2235             struct peer_atom * atom = getExistingAtom( t, &peer->addr );
    2236             if( atom->piece_data_time )
    2237                 atom->numFails = 0;
    2238             else
    2239                 ++atom->numFails;
    2240             tordbg( t, "removing bad peer %s", tr_peerIoGetAddrStr( peer->io ) );
    2241             removePeer( t, peer );
    2242         }
    2243 
    2244 tordbg( t, "nCandidates is %d, MAX_RECONNECTIONS_PER_PULSE is %d, getPeerCount(t) is %d, getMaxPeerCount(t) is %d, newConnectionsThisSecond is %d, MAX_CONNECTIONS_PER_SECOND is %d",
    2245         (int)nCandidates, (int)MAX_RECONNECTIONS_PER_PULSE, (int)getPeerCount( t ), (int)getMaxPeerCount( t->tor ), (int)newConnectionsThisSecond, (int)MAX_CONNECTIONS_PER_SECOND );
     2273        /* maybe disconnect some lesser peers, if we have candidates to replace them with */
     2274        for( i=0; ( i<canCloseCount ) && ( i<maxCandidates ); ++i )
     2275            closePeer( t, canClose[i] );
     2276
     2277        tordbg( t, "candidateCount is %d, MAX_RECONNECTIONS_PER_PULSE is %d,"
     2278                   " getPeerCount(t) is %d, getMaxPeerCount(t) is %d, "
     2279                   "newConnectionsThisSecond is %d, MAX_CONNECTIONS_PER_SECOND is %d",
     2280                   candidateCount,
     2281                   MAX_RECONNECTIONS_PER_PULSE,
     2282                   getPeerCount( t ),
     2283                   getMaxPeerCount( t->tor ),
     2284                   newConnectionsThisSecond, MAX_CONNECTIONS_PER_SECOND );
    22462285
    22472286        /* add some new ones */
    2248         for( i = 0;    ( i < nCandidates )
    2249            && ( i < MAX_RECONNECTIONS_PER_PULSE )
    2250            && ( getPeerCount( t ) < getMaxPeerCount( t->tor ) )
    2251            && ( newConnectionsThisSecond < MAX_CONNECTIONS_PER_SECOND ); ++i )
     2287        for( i=0; i<maxCandidates; ++i )
    22522288        {
    2253             tr_peerMgr *      mgr = t->manager;
    2254             struct peer_atom * atom = candidates[i];
    2255             tr_peerIo *        io;
     2289            tr_peerMgr        * mgr = t->manager;
     2290            struct peer_atom  * atom = candidates[i];
     2291            tr_peerIo         * io;
    22562292
    22572293            tordbg( t, "Starting an OUTGOING connection with %s",
     
    22872323
    22882324        /* cleanup */
    2289         tr_free( connections );
    22902325        tr_free( candidates );
     2326        tr_free( mustClose );
     2327        tr_free( canClose );
    22912328    }
    22922329
Note: See TracChangeset for help on using the changeset viewer.