(trunk libT) #1823: Ratio limit is only checked when peers are active

This commit is contained in:
Charles Kerr 2009-03-01 13:56:22 +00:00
parent 89eff88dec
commit 857b3dab10
4 changed files with 107 additions and 84 deletions

View File

@ -995,15 +995,6 @@ peerSuggestedPiece( Torrent * t UNUSED,
#endif
}
static void
fireRatioLimitHit( tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
if( tor->ratio_limit_hit_func )
tor->ratio_limit_hit_func( tor, tor->ratio_limit_hit_func_user_data );
}
static void
peerCallbackFunc( void * vpeer, void * vevent, void * vt )
{
@ -1035,7 +1026,6 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt )
{
const time_t now = time( NULL );
tr_torrent * tor = t->tor;
double seedRatio;
tor->activityDate = now;
@ -1053,20 +1043,7 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt )
a->piece_data_time = now;
}
/* if we're seeding and we've reached our seed ratio limit, stop the torrent */
if( tr_torrentIsSeed( tor ) && tr_torrentGetSeedRatio( tor, &seedRatio ) ) {
const double up = tor->uploadedCur + tor->uploadedPrev;
const double down = tor->downloadedCur + tor->downloadedPrev;
const double ratio = tr_getRatio( up, down );
if( ratio >= seedRatio ) {
tr_torrentStop( tor );
/* set to no ratio limit to allow easy restarting */
tr_torrentSetRatioMode( tor, TR_RATIOLIMIT_UNLIMITED );
fireRatioLimitHit( tor );
}
}
tr_torrentCheckSeedRatio( tor );
break;
}

View File

@ -343,7 +343,7 @@ tr_sessionSaveSettings( tr_session * session, const char * configDir, tr_benc *
char * filename;
assert( tr_bencIsDict( settings ) );
filename = tr_buildPath( configDir, "settings.json", NULL );
tr_sessionGetSettings( session, settings );
@ -722,6 +722,53 @@ tr_sessionGetPortForwarding( const tr_session * session )
****
***/
static void
updateSeedRatio( tr_session * session )
{
tr_torrent * tor = NULL;
while(( tor = tr_torrentNext( session, tor )))
tr_torrentCheckSeedRatio( tor );
}
void
tr_sessionSetRatioLimited( tr_session * session, tr_bool isLimited )
{
assert( tr_isSession( session ) );
session->isRatioLimited = isLimited;
updateSeedRatio( session );
}
void
tr_sessionSetRatioLimit( tr_session * session, double desiredRatio )
{
assert( tr_isSession( session ) );
session->desiredRatio = desiredRatio;
updateSeedRatio( session );
}
tr_bool
tr_sessionIsRatioLimited( const tr_session * session )
{
assert( tr_isSession( session ) );
return session->isRatioLimited;
}
double
tr_sessionGetRatioLimit( const tr_session * session )
{
assert( tr_isSession( session ) );
return session->desiredRatio;
}
/***
****
***/
static void
updateBandwidth( tr_session * session, tr_direction dir )
{
@ -748,15 +795,6 @@ tr_sessionSetSpeedLimitEnabled( tr_session * session,
updateBandwidth( session, dir );
}
void
tr_sessionSetRatioLimited( tr_session * session,
tr_bool isLimited )
{
assert( tr_isSession( session ) );
session->isRatioLimited = isLimited;
}
void
tr_sessionSetSpeedLimit( tr_session * session,
tr_direction dir,
@ -769,18 +807,8 @@ tr_sessionSetSpeedLimit( tr_session * session,
updateBandwidth( session, dir );
}
void
tr_sessionSetRatioLimit( tr_session * session,
double desiredRatio )
{
assert( tr_isSession( session ) );
session->desiredRatio = desiredRatio;
}
tr_bool
tr_sessionIsSpeedLimitEnabled( const tr_session * session,
tr_direction dir )
tr_sessionIsSpeedLimitEnabled( const tr_session * session, tr_direction dir )
{
assert( tr_isSession( session ) );
assert( tr_isDirection( dir ) );
@ -788,17 +816,8 @@ tr_sessionIsSpeedLimitEnabled( const tr_session * session,
return session->isSpeedLimited[dir];
}
tr_bool
tr_sessionIsRatioLimited( const tr_session * session )
{
assert( tr_isSession( session ) );
return session->isRatioLimited;
}
int
tr_sessionGetSpeedLimit( const tr_session * session,
tr_direction dir )
tr_sessionGetSpeedLimit( const tr_session * session, tr_direction dir )
{
assert( tr_isSession( session ) );
assert( tr_isDirection( dir ) );
@ -806,21 +825,12 @@ tr_sessionGetSpeedLimit( const tr_session * session,
return session->speedLimit[dir];
}
double
tr_sessionGetRatioLimit( const tr_session * session )
{
assert( tr_isSession( session ) );
return session->desiredRatio;
}
/***
****
***/
void
tr_sessionSetPeerLimit( tr_session * session,
uint16_t maxGlobalPeers )
tr_sessionSetPeerLimit( tr_session * session, uint16_t maxGlobalPeers )
{
assert( tr_isSession( session ) );

View File

@ -58,7 +58,7 @@ tr_torrentFindFromId( tr_session * session, int id )
{
tr_torrent * tor = NULL;
while( ( tor = tr_torrentNext( session, tor ) ) )
while(( tor = tr_torrentNext( session, tor )))
if( tor->uniqueId == id )
return tor;
@ -70,7 +70,7 @@ tr_torrentFindFromHashString( tr_session * session, const char * str )
{
tr_torrent * tor = NULL;
while( ( tor = tr_torrentNext( session, tor ) ) )
while(( tor = tr_torrentNext( session, tor )))
if( !strcmp( str, tor->info.hashString ) )
return tor;
@ -82,7 +82,7 @@ tr_torrentFindFromHash( tr_session * session, const uint8_t * torrentHash )
{
tr_torrent * tor = NULL;
while( ( tor = tr_torrentNext( session, tor ) ) )
while(( tor = tr_torrentNext( session, tor )))
if( *tor->info.hash == *torrentHash )
if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) )
return tor;
@ -96,7 +96,7 @@ tr_torrentFindFromObfuscatedHash( tr_session * session,
{
tr_torrent * tor = NULL;
while( ( tor = tr_torrentNext( session, tor ) ) )
while(( tor = tr_torrentNext( session, tor )))
if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash,
SHA_DIGEST_LENGTH ) )
return tor;
@ -151,13 +151,14 @@ tr_torrentGetSpeedLimit( const tr_torrent * tor,
}
void
tr_torrentSetRatioMode( tr_torrent * tor,
tr_ratiolimit mode )
tr_torrentSetRatioMode( tr_torrent * tor, tr_ratiolimit mode )
{
assert( tr_isTorrent( tor ) );
assert( mode==TR_RATIOLIMIT_GLOBAL || mode==TR_RATIOLIMIT_SINGLE || mode==TR_RATIOLIMIT_UNLIMITED );
tor->ratioLimitMode = mode;
tr_torrentCheckSeedRatio( tor );
}
tr_ratiolimit
@ -174,7 +175,9 @@ tr_torrentSetRatioLimit( tr_torrent * tor,
{
assert( tr_isTorrent( tor ) );
tor->desiredRatio = desiredRatio;
tor->desiredRatio = desiredRatio;
tr_torrentCheckSeedRatio( tor );
}
double
@ -903,7 +906,7 @@ tr_torrentStat( tr_torrent * tor )
else
s->eta = s->leftUntilDone / s->pieceDownloadSpeed / 1024.0;
break;
case TR_STATUS_SEED:
if( tr_torrentGetSeedRatio( tor, &seedRatio ) )
{
@ -915,7 +918,7 @@ tr_torrentStat( tr_torrent * tor )
else
s->eta = TR_ETA_NOT_AVAIL;
break;
default:
s->eta = TR_ETA_NOT_AVAIL;
break;
@ -1544,9 +1547,7 @@ tr_torrentGetFileDL( const tr_torrent * tor,
}
static void
setFileDND( tr_torrent * tor,
tr_file_index_t fileIndex,
int doDownload )
setFileDND( tr_torrent * tor, tr_file_index_t fileIndex, int doDownload )
{
tr_file * file;
const int dnd = !doDownload;
@ -1601,7 +1602,7 @@ setFileDND( tr_torrent * tor,
}
void
tr_torrentInitFileDLs( tr_torrent * tor,
tr_torrentInitFileDLs( tr_torrent * tor,
tr_file_index_t * files,
tr_file_index_t fileCount,
tr_bool doDownload )
@ -1612,9 +1613,10 @@ tr_torrentInitFileDLs( tr_torrent * tor,
tr_torrentLock( tor );
for( i = 0; i < fileCount; ++i )
for( i=0; i<fileCount; ++i )
setFileDND( tor, files[i], doDownload );
tr_cpInvalidateDND ( &tor->completion );
tr_cpInvalidateDND( &tor->completion );
tr_torrentCheckSeedRatio( tor );
tr_torrentUnlock( tor );
}
@ -1748,9 +1750,9 @@ tr_torrentSetFileChecked( tr_torrent * tor,
assert( tr_isTorrent( tor ) );
if( isChecked )
tr_bitfieldAddRange ( &tor->checkedPieces, begin, end );
tr_bitfieldAddRange( &tor->checkedPieces, begin, end );
else
tr_bitfieldRemRange ( &tor->checkedPieces, begin, end );
tr_bitfieldRemRange( &tor->checkedPieces, begin, end );
}
tr_bool
@ -1777,7 +1779,7 @@ tr_torrentUncheck( tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
tr_bitfieldRemRange ( &tor->checkedPieces, 0, tor->info.pieceCount );
tr_bitfieldRemRange( &tor->checkedPieces, 0, tor->info.pieceCount );
}
int
@ -2111,3 +2113,35 @@ tr_torrentDeleteLocalData( tr_torrent * tor, tr_fileFunc fileFunc )
tr_free( path );
}
}
/***
****
***/
void
tr_torrentCheckSeedRatio( tr_torrent * tor )
{
double seedRatio;
assert( tr_isTorrent( tor ) );
/* if we're seeding and we've reached our seed ratio limit, stop the torrent */
if( tr_torrentIsSeed( tor ) && tr_torrentGetSeedRatio( tor, &seedRatio ) )
{
const double up = tor->uploadedCur + tor->uploadedPrev;
const double down = tor->downloadedCur + tor->downloadedPrev;
const double ratio = tr_getRatio( up, down );
if( ratio >= seedRatio )
{
tr_torrentStop( tor );
/* set to no ratio limit to allow easy restarting */
tr_torrentSetRatioMode( tor, TR_RATIOLIMIT_UNLIMITED );
/* maybe notify the client */
if( tor->ratio_limit_hit_func != NULL )
tor->ratio_limit_hit_func( tor, tor->ratio_limit_hit_func_user_data );
}
}
}

View File

@ -91,7 +91,6 @@ void tr_torrentInitFilePriority( tr_torrent * tor,
tr_file_index_t fileIndex,
tr_priority_t priority );
int tr_torrentCountUncheckedPieces( const tr_torrent * );
tr_bool tr_torrentIsFileChecked( const tr_torrent * tor,
@ -116,6 +115,9 @@ time_t* tr_torrentGetMTimes( const tr_torrent * tor,
tr_torrent* tr_torrentNext( tr_session * session,
tr_torrent * current );
void tr_torrentCheckSeedRatio( tr_torrent * tor );
typedef enum
{