(trunk) rewrite peerCheck code based on comments from mtolman in http://transmission.m0k.org/forum/viewtopic.php?t=2106

This commit is contained in:
Charles Kerr 2007-08-08 06:01:52 +00:00
parent 3cb5ba7eed
commit 51096225bd
1 changed files with 47 additions and 74 deletions

View File

@ -34,81 +34,54 @@ static int peerCmp( tr_peer_t * peer1, tr_peer_t * peer2 )
return memcmp( peer1->id, peer2->id, 20 ); return memcmp( peer1->id, peer2->id, 20 );
} }
static int checkPeer( tr_peer_t * peer ) static int
checkPeer( tr_peer_t * peer )
{ {
tr_torrent_t * tor = peer->tor; int ret;
tr_torrent_t * tor = peer->tor;
uint64_t now; const uint64_t now = tr_date( );
int idleTime, peersWanted, percentOfRange; const uint64_t idleTime = now - peer->date;
int ret; uint64_t lo, hi, minimum;
int relaxStrictnessIfFewerThanN;
now = tr_date(); double strictness;
idleTime = now - peer->date;
/* when deciding whether or not to keep a peer, judge its responsiveness
/* assume any peer over with an idleTime lower than on a sliding scale that's based on how many other peers are available */
8 seconds has not timed out */ relaxStrictnessIfFewerThanN = (int)(((TR_MAX_PEER_COUNT * PERCENT_PEER_WANTED) / 100.0) + 0.5);
if ( idleTime > MIN_CON_TIMEOUT )
{ /* if we have >= relaxIfFewerThan, strictness is 100%.
peersWanted = ( TR_MAX_PEER_COUNT * PERCENT_PEER_WANTED ) / 100; if we have zero connections, strictness is 0% */
if ( tor->peerCount > peersWanted ) if( tor->peerCount >= relaxStrictnessIfFewerThanN )
{ strictness = 1.0;
/* strict requirements for connecting timeout */ else
if ( peer->status < PEER_STATUS_CONNECTED ) strictness = tor->peerCount / (double)relaxStrictnessIfFewerThanN;
{
peer_dbg( "connection timeout, idled %i seconds", /* test: has it been too long since we were properly connected to them? */
( idleTime / 1000 ) ); lo = MIN_CON_TIMEOUT;
return TR_ERROR; hi = MAX_CON_TIMEOUT;
} minimum = lo + ((hi-lo) * strictness);
if( peer->status < PEER_STATUS_CONNECTED && idleTime > minimum ) {
/* strict requirements for idle uploading timeout */ peer_dbg( "connection timeout, idled %i seconds", (int)(idleTime/1000) );
if ( peer->inRequestCount && idleTime > MIN_UPLOAD_IDLE ) return TR_ERROR;
{ }
peer_dbg( "idle uploader timeout, idled %i seconds",
( idleTime / 1000 ) ); /* test: have we been waiting on a request for too long? */
return TR_ERROR; lo = MIN_UPLOAD_IDLE;
} hi = MAX_UPLOAD_IDLE;
minimum = lo + ((hi-lo) * strictness);
/* strict requirements for keep-alive timeout */ if( peer->inRequestCount && idleTime > minimum ) {
if ( idleTime > MIN_KEEP_ALIVE ) peer_dbg( "idle uploader timeout, idled %d seconds", (int)(idleTime/1000));
{ return TR_ERROR;
peer_dbg( "peer timeout, idled %i seconds", }
( idleTime / 1000 ) );
return TR_ERROR; /* test: has it been too long since the peer gave us any response at all? */
} lo = MIN_KEEP_ALIVE;
} hi = MAX_KEEP_ALIVE;
/* if we are tight for peers, relax the enforcement of timeouts */ minimum = lo + ((hi-lo) * strictness);
else if( idleTime > minimum ) {
{ peer_dbg( "peer timeout, idled %d seconds", (int)(idleTime/1000) );
percentOfRange = tor->peerCount / (TR_MAX_PEER_COUNT - peersWanted); return TR_ERROR;
}
/* relax requirements for connecting timeout */
if ( peer->status < PEER_STATUS_CONNECTED && idleTime > MIN_CON_TIMEOUT +
( MAX_CON_TIMEOUT - MIN_CON_TIMEOUT ) * percentOfRange )
{
peer_dbg( "connection timeout, idled %i seconds",
( idleTime / 1000 ) );
return TR_ERROR;
}
/* relax requirements for idle uploading timeout */
if ( peer->inRequestCount && idleTime > MIN_UPLOAD_IDLE +
( MAX_UPLOAD_IDLE - MIN_UPLOAD_IDLE ) * percentOfRange )
{
peer_dbg( "idle uploader timeout, idled %i seconds",
( idleTime / 1000 ) );
return TR_ERROR;
}
/* relax requirements for keep-alive timeout */
if ( idleTime > MIN_KEEP_ALIVE +
( MAX_KEEP_ALIVE - MIN_KEEP_ALIVE ) * percentOfRange )
{
peer_dbg( "peer timeout, idled %i seconds",
( idleTime / 1000 ) );
return TR_ERROR;
}
}
}
if( PEER_STATUS_CONNECTED == peer->status ) if( PEER_STATUS_CONNECTED == peer->status )
{ {