Changeset 6757


Ignore:
Timestamp:
Sep 16, 2008, 4:46:18 PM (13 years ago)
Author:
charles
Message:

(1.3x libT) backport #1254 (r6700): "reconnecting too frequently to the same peer"

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.3x/libtransmission/peer-mgr.c

    r6565 r6757  
    7474
    7575    /* unreachable for now... but not banned.  if they try to connect to us it's okay */
    76     MYFLAG_UNREACHABLE = 2
     76    MYFLAG_UNREACHABLE = 2,
     77
     78    /* the absolute minimum we'll wait before attempting to reconnect to a peer */
     79    MINIMUM_RECONNECT_INTERVAL_SECS = 5
    7780};
    7881
     
    9497    uint16_t numFails;
    9598    struct in_addr addr;
    96     time_t time;
     99    time_t time; /* the last time the peer's connection status changed */
    97100    time_t piece_data_time;
    98101};
     
    917920
    918921static int
    919 getMaxPeerCount( const tr_torrent * tor UNUSED )
     922getMaxPeerCount( const tr_torrent * tor )
    920923{
    921924    return tor->maxConnectedPeers;
     925}
     926
     927static int
     928getPeerCount( const Torrent * t )
     929{
     930    return tr_ptrArraySize( t->peers ) + tr_ptrArraySize( t->outgoingHandshakes );
    922931}
    923932
     
    976985        ensureAtomExists( t, addr, port, 0, TR_PEER_FROM_INCOMING );
    977986        atom = getExistingAtom( t, addr );
     987        atom->time = time( NULL );
    978988
    979989        if( atom->myflags & MYFLAG_BANNED )
     
    982992            tr_peerIoFree( io );
    983993        }
    984         else if( tr_ptrArraySize( t->peers ) >= getMaxPeerCount( t->tor ) )
     994        else if( tr_peerIoIsIncoming( io )
     995                 && ( getPeerCount( t ) >= getMaxPeerCount( t->tor ) ) )
     996
    985997        {
    986998            tr_peerIoFree( io );
     
    10091021                peer->io = io;
    10101022                peer->msgs = tr_peerMsgsNew( t->tor, peer, peerCallbackFunc, t, &peer->msgsTag );
    1011                 atom->time = time( NULL );
    10121023            }
    10131024        }
     
    17601771{
    17611772    int sec;
    1762 
    1763     switch( atom->numFails )
    1764     {
     1773    const time_t now = time( NULL );
     1774
     1775    /* if we were recently connected to this peer and transferring piece
     1776     * data, try to reconnect to them sooner rather that later -- we don't
     1777     * want network troubles to get in the way of a good peer. */
     1778    if( ( now - atom->piece_data_time ) <= ( MINIMUM_RECONNECT_INTERVAL_SECS * 2 ) )
     1779        sec = MINIMUM_RECONNECT_INTERVAL_SECS;
     1780
     1781    /* don't allow reconnects more often than our minimum */
     1782    else if( ( now - atom->time ) < MINIMUM_RECONNECT_INTERVAL_SECS )
     1783        sec = MINIMUM_RECONNECT_INTERVAL_SECS;
     1784
     1785    /* otherwise, the interval depends on how many times we've tried
     1786     * and failed to connect to the peer */
     1787    else switch( atom->numFails ) {
    17651788        case 0: sec = 0; break;
    17661789        case 1: sec = 5; break;
     
    17901813    for( i=retCount=0; i<atomCount; ++i )
    17911814    {
     1815        int interval;
    17921816        struct peer_atom * atom = atoms[i];
    17931817
     
    18111835            continue;
    18121836
    1813         /* If we were connected to this peer recently and transferring
    1814          * piece data, try to reconnect -- network troubles may have
    1815          * disconnected us.  but if we weren't sharing piece data,
    1816          * hold off on this peer to give another one a try instead */
    1817         if( ( now - atom->piece_data_time ) > 10 )
    1818         {
    1819             const int wait = getReconnectIntervalSecs( atom );
    1820             if( ( now - atom->time ) < wait ) {
    1821                 tordbg( t, "RECONNECT peer %d (%s) is in its grace period of %d seconds..",
    1822                         i, tr_peerIoAddrStr(&atom->addr,atom->port), wait );
    1823                 continue;
    1824             }
     1837        /* don't reconnect too often */
     1838        interval = getReconnectIntervalSecs( atom );
     1839        if( ( now - atom->time ) < interval ) {
     1840            tordbg( t, "RECONNECT peer %d (%s) is in its grace period of %d seconds..",
     1841                    i, tr_peerIoAddrStr(&atom->addr,atom->port), interval );
     1842            continue;
    18251843        }
    18261844
     
    18861904
    18871905        /* add some new ones */
    1888         for( i=0;    i < nCandidates
    1889                   && i < MAX_RECONNECTIONS_PER_PULSE
    1890                   && newConnectionsThisSecond < MAX_CONNECTIONS_PER_SECOND; ++i )
     1906        for( i=0;    ( i < nCandidates )
     1907                  && ( i < MAX_RECONNECTIONS_PER_PULSE )
     1908                  && ( getPeerCount( t ) < getMaxPeerCount( t->tor ) )
     1909                  && ( newConnectionsThisSecond < MAX_CONNECTIONS_PER_SECOND ); ++i )
    18911910        {
    18921911            tr_peerMgr * mgr = t->manager;
Note: See TracChangeset for help on using the changeset viewer.