From 7eee937c563890f07d31c4363f1e275a89b7f141 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 9 Dec 2008 22:05:45 +0000 Subject: [PATCH] (trunk libT) experimental code to try & fix the "one torrent / one peer eats all my bandwidth" issue reported in the forums & irc --- libtransmission/bandwidth.c | 54 ++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/libtransmission/bandwidth.c b/libtransmission/bandwidth.c index dd5b5fed1..dee025710 100644 --- a/libtransmission/bandwidth.c +++ b/libtransmission/bandwidth.c @@ -17,6 +17,7 @@ #include "transmission.h" #include "bandwidth.h" +#include "crypto.h" #include "iobuf.h" #include "ptrarray.h" #include "utils.h" @@ -244,10 +245,11 @@ tr_bandwidthIsLimited( const tr_bandwidth * b, #define DEBUG_DIRECTION TR_UP #endif -void -tr_bandwidthAllocate( tr_bandwidth * b, - tr_direction dir, - int period_msec ) +static void +allocateBandwidth( tr_bandwidth * b, + tr_direction dir, + int period_msec, + tr_ptrArray * addme_buffers ) { assert( isBandwidth( b ) ); assert( isDirection( dir ) ); @@ -255,13 +257,7 @@ tr_bandwidthAllocate( tr_bandwidth * b, if( b->band[dir].isLimited ) { const double desiredSpeed = b->band[dir].desiredSpeed; -#if 0 - const double currentSpeed = getSpeed( &b->band[dir].piece, HISTORY_MSEC - period_msec ); - const double pulseCount = ( HISTORY_MSEC - period_msec ) / (double)period_msec; - const double nextPulseSpeed = desiredSpeed * ( pulseCount + 1 ) - ( currentSpeed * pulseCount ); -#else const double nextPulseSpeed = desiredSpeed; -#endif b->band[dir].bytesLeft = MAX( 0.0, nextPulseSpeed * 1024.0 * period_msec / 1000.0 ); #ifdef DEBUG_DIRECTION @@ -275,16 +271,16 @@ tr_bandwidthAllocate( tr_bandwidth * b, /* notify the io buffers that there's more bandwidth available */ if( !b->band[dir].isLimited || ( b->band[dir].bytesLeft > 0 ) ) { - int i, n=0; - short what = dir==TR_UP ? EV_WRITE : EV_READ; - struct tr_iobuf ** iobufs = (struct tr_iobuf**) tr_ptrArrayPeek( b->iobufs, &n ); + int i; + const int n = tr_ptrArraySize( b->iobufs ); + for( i=0; iiobufs, i ) ); + } + #ifdef DEBUG_DIRECTION if( ( dir == DEBUG_DIRECTION ) && ( n > 1 ) ) fprintf( stderr, "bandwidth %p has %d iobufs\n", b, n ); #endif - for( i=0; ichildren, &n ); + + /* notify the io buffers in a random order s.t. no + particular peer gets to hog all the bandwidth */ + while( n > 0 ) { + const int i = tr_cryptoRandInt( n ); + tr_iobuf_enable( buffers[i], what ); + buffers[i] = buffers[n-1]; + --n; + } + + tr_ptrArrayFree( tmp, NULL ); +} + /*** **** ***/