1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 09:13:06 +00:00

(trunk libT) #2607 "avoid unnecessary calls to getPeerCandidates(), wasted cycles in peerIsInUse()"

This commit is contained in:
Charles Kerr 2009-11-29 18:35:02 +00:00
parent 33658647d6
commit 03351e9a4e

View file

@ -1559,7 +1559,7 @@ tr_peerMgrAddIncoming( tr_peerMgr * manager,
{
tr_netClose( session, socket );
}
else /* we don't have a connetion to them yet... */
else /* we don't have a connection to them yet... */
{
tr_peerIo * io;
tr_handshake * handshake;
@ -2523,10 +2523,6 @@ getPeerCandidates( Torrent * t, const time_t now, int * setmeSize )
if( atom->myflags & MYFLAG_UNREACHABLE )
continue;
/* we don't need two connections to the same peer... */
if( peerIsInUse( t, atom ) )
continue;
/* no need to connect if we're both seeds... */
if( seed && ( ( atom->flags & ADDED_F_SEED_FLAG ) ||
( atom->uploadOnly == UPLOAD_ONLY_YES ) ) )
@ -2545,9 +2541,14 @@ getPeerCandidates( Torrent * t, const time_t now, int * setmeSize )
if( tr_sessionIsAddressBlocked( t->manager->session, &atom->addr ) )
continue;
/* we don't need two connections to the same peer... */
if( peerIsInUse( t, atom ) )
continue;
ret[retCount++] = atom;
}
if( retCount != 0 )
qsort( ret, retCount, sizeof( struct peer_atom* ), compareCandidates );
*setmeSize = retCount;
return ret;
@ -2595,48 +2596,68 @@ reconnectTorrent( Torrent * t )
else
{
int i;
int canCloseCount;
int mustCloseCount;
int candidateCount;
int maxCandidates;
struct tr_peer ** canClose = getPeersToClose( t, TR_CAN_CLOSE, now, &canCloseCount );
struct tr_peer ** mustClose = getPeersToClose( t, TR_MUST_CLOSE, now, &mustCloseCount );
struct peer_atom ** candidates = getPeerCandidates( t, now, &candidateCount );
tordbg( t, "reconnect pulse for [%s]: "
"%d must-close connections, "
"%d can-close connections, "
"%d connection candidates, "
"%d atoms, "
"max per pulse is %d",
tr_torrentName( t->tor ),
mustCloseCount,
canCloseCount,
candidateCount,
tr_ptrArraySize( &t->pool ),
MAX_RECONNECTIONS_PER_PULSE );
struct tr_peer ** mustClose;
/* disconnect the really bad peers */
mustClose = getPeersToClose( t, TR_MUST_CLOSE, now, &mustCloseCount );
for( i=0; i<mustCloseCount; ++i )
closePeer( t, mustClose[i] );
tr_free( mustClose );
/* decide how many peers can we try to add in this pass */
maxCandidates = candidateCount;
maxCandidates = MIN( maxCandidates, MAX_RECONNECTIONS_PER_PULSE );
maxCandidates = MAX_RECONNECTIONS_PER_PULSE;
maxCandidates = MIN( maxCandidates, getMaxPeerCount( t->tor ) - getPeerCount( t ) );
maxCandidates = MIN( maxCandidates, MAX_CONNECTIONS_PER_SECOND - newConnectionsThisSecond );
/* select the best candidates, if they are requested */
if( maxCandidates == 0 )
{
tordbg( t, "reconnect pulse for [%s]: %d must-close connections, "
"NO connection candidates needed, %d atoms, "
"max per pulse is %d",
t->tor->info.name, mustCloseCount,
tr_ptrArraySize( &t->pool ),
MAX_RECONNECTIONS_PER_PULSE );
tordbg( t, "maxCandidates 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",
maxCandidates, MAX_RECONNECTIONS_PER_PULSE,
getPeerCount( t ), getMaxPeerCount( t->tor ),
newConnectionsThisSecond, MAX_CONNECTIONS_PER_SECOND );
}
else
{
int canCloseCount = 0;
int candidateCount;
struct peer_atom ** candidates;
candidates = getPeerCandidates( t, now, &candidateCount );
maxCandidates = MIN( maxCandidates, candidateCount );
/* maybe disconnect some lesser peers, if we have candidates to replace them with */
if( maxCandidates != 0 )
{
struct tr_peer ** canClose = getPeersToClose( t, TR_CAN_CLOSE, now, &canCloseCount );
for( i=0; ( i<canCloseCount ) && ( i<maxCandidates ); ++i )
closePeer( t, canClose[i] );
tr_free( canClose );
}
tordbg( t, "reconnect pulse for [%s]: %d must-close connections, "
"%d can-close connections, %d connection candidates, "
"%d atoms, max per pulse is %d",
t->tor->info.name, mustCloseCount,
canCloseCount, candidateCount,
tr_ptrArraySize( &t->pool ), MAX_RECONNECTIONS_PER_PULSE );
tordbg( t, "candidateCount 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",
candidateCount,
MAX_RECONNECTIONS_PER_PULSE,
getPeerCount( t ),
getMaxPeerCount( t->tor ),
candidateCount, MAX_RECONNECTIONS_PER_PULSE,
getPeerCount( t ), getMaxPeerCount( t->tor ),
newConnectionsThisSecond, MAX_CONNECTIONS_PER_SECOND );
/* add some new ones */
@ -2649,7 +2670,11 @@ reconnectTorrent( Torrent * t )
tordbg( t, "Starting an OUTGOING connection with %s",
tr_atomAddrStr( atom ) );
io = tr_peerIoNewOutgoing( mgr->session, mgr->session->bandwidth, &atom->addr, atom->port, t->tor->info.hash );
io = tr_peerIoNewOutgoing( mgr->session,
mgr->session->bandwidth,
&atom->addr,
atom->port,
t->tor->info.hash );
if( io == NULL )
{
@ -2676,11 +2701,8 @@ reconnectTorrent( Torrent * t )
atom->time = now;
}
/* cleanup */
tr_free( candidates );
tr_free( mustClose );
tr_free( canClose );
}
}
}