mirror of
https://github.com/transmission/transmission
synced 2025-03-09 13:50:00 +00:00
(trunk) rewrite peerCheck code based on comments from mtolman in http://transmission.m0k.org/forum/viewtopic.php?t=2106
This commit is contained in:
parent
3cb5ba7eed
commit
51096225bd
1 changed files with 47 additions and 74 deletions
|
@ -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;
|
|
||||||
|
|
||||||
uint64_t now;
|
|
||||||
int idleTime, peersWanted, percentOfRange;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
tr_torrent_t * tor = peer->tor;
|
||||||
|
const uint64_t now = tr_date( );
|
||||||
|
const uint64_t idleTime = now - peer->date;
|
||||||
|
uint64_t lo, hi, minimum;
|
||||||
|
int relaxStrictnessIfFewerThanN;
|
||||||
|
double strictness;
|
||||||
|
|
||||||
now = tr_date();
|
/* when deciding whether or not to keep a peer, judge its responsiveness
|
||||||
idleTime = now - peer->date;
|
on a sliding scale that's based on how many other peers are available */
|
||||||
|
relaxStrictnessIfFewerThanN = (int)(((TR_MAX_PEER_COUNT * PERCENT_PEER_WANTED) / 100.0) + 0.5);
|
||||||
|
|
||||||
/* assume any peer over with an idleTime lower than
|
/* if we have >= relaxIfFewerThan, strictness is 100%.
|
||||||
8 seconds has not timed out */
|
if we have zero connections, strictness is 0% */
|
||||||
if ( idleTime > MIN_CON_TIMEOUT )
|
if( tor->peerCount >= relaxStrictnessIfFewerThanN )
|
||||||
{
|
strictness = 1.0;
|
||||||
peersWanted = ( TR_MAX_PEER_COUNT * PERCENT_PEER_WANTED ) / 100;
|
|
||||||
if ( tor->peerCount > peersWanted )
|
|
||||||
{
|
|
||||||
/* strict requirements for connecting timeout */
|
|
||||||
if ( peer->status < PEER_STATUS_CONNECTED )
|
|
||||||
{
|
|
||||||
peer_dbg( "connection timeout, idled %i seconds",
|
|
||||||
( idleTime / 1000 ) );
|
|
||||||
return TR_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* strict requirements for idle uploading timeout */
|
|
||||||
if ( peer->inRequestCount && idleTime > MIN_UPLOAD_IDLE )
|
|
||||||
{
|
|
||||||
peer_dbg( "idle uploader timeout, idled %i seconds",
|
|
||||||
( idleTime / 1000 ) );
|
|
||||||
return TR_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* strict requirements for keep-alive timeout */
|
|
||||||
if ( idleTime > MIN_KEEP_ALIVE )
|
|
||||||
{
|
|
||||||
peer_dbg( "peer timeout, idled %i seconds",
|
|
||||||
( idleTime / 1000 ) );
|
|
||||||
return TR_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* if we are tight for peers, relax the enforcement of timeouts */
|
|
||||||
else
|
else
|
||||||
{
|
strictness = tor->peerCount / (double)relaxStrictnessIfFewerThanN;
|
||||||
percentOfRange = tor->peerCount / (TR_MAX_PEER_COUNT - peersWanted);
|
|
||||||
|
|
||||||
/* relax requirements for connecting timeout */
|
/* test: has it been too long since we were properly connected to them? */
|
||||||
if ( peer->status < PEER_STATUS_CONNECTED && idleTime > MIN_CON_TIMEOUT +
|
lo = MIN_CON_TIMEOUT;
|
||||||
( MAX_CON_TIMEOUT - MIN_CON_TIMEOUT ) * percentOfRange )
|
hi = MAX_CON_TIMEOUT;
|
||||||
{
|
minimum = lo + ((hi-lo) * strictness);
|
||||||
peer_dbg( "connection timeout, idled %i seconds",
|
if( peer->status < PEER_STATUS_CONNECTED && idleTime > minimum ) {
|
||||||
( idleTime / 1000 ) );
|
peer_dbg( "connection timeout, idled %i seconds", (int)(idleTime/1000) );
|
||||||
return TR_ERROR;
|
return TR_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* relax requirements for idle uploading timeout */
|
/* test: have we been waiting on a request for too long? */
|
||||||
if ( peer->inRequestCount && idleTime > MIN_UPLOAD_IDLE +
|
lo = MIN_UPLOAD_IDLE;
|
||||||
( MAX_UPLOAD_IDLE - MIN_UPLOAD_IDLE ) * percentOfRange )
|
hi = MAX_UPLOAD_IDLE;
|
||||||
{
|
minimum = lo + ((hi-lo) * strictness);
|
||||||
peer_dbg( "idle uploader timeout, idled %i seconds",
|
if( peer->inRequestCount && idleTime > minimum ) {
|
||||||
( idleTime / 1000 ) );
|
peer_dbg( "idle uploader timeout, idled %d seconds", (int)(idleTime/1000));
|
||||||
return TR_ERROR;
|
return TR_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* relax requirements for keep-alive timeout */
|
/* test: has it been too long since the peer gave us any response at all? */
|
||||||
if ( idleTime > MIN_KEEP_ALIVE +
|
lo = MIN_KEEP_ALIVE;
|
||||||
( MAX_KEEP_ALIVE - MIN_KEEP_ALIVE ) * percentOfRange )
|
hi = MAX_KEEP_ALIVE;
|
||||||
{
|
minimum = lo + ((hi-lo) * strictness);
|
||||||
peer_dbg( "peer timeout, idled %i seconds",
|
if( idleTime > minimum ) {
|
||||||
( idleTime / 1000 ) );
|
peer_dbg( "peer timeout, idled %d seconds", (int)(idleTime/1000) );
|
||||||
return TR_ERROR;
|
return TR_ERROR;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if( PEER_STATUS_CONNECTED == peer->status )
|
if( PEER_STATUS_CONNECTED == peer->status )
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue