Ignore:
Timestamp:
Dec 15, 2008, 9:22:11 PM (12 years ago)
Author:
charles
Message:

(1.4x libT) probable fix for the "greedy peer" bug

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.4x/libtransmission/bandwidth.c

    r7375 r7403  
    250250                   tr_direction    dir,
    251251                   int             period_msec,
    252                    tr_ptrArray   * addme_buffers )
     252                   tr_ptrArray   * iobuf_pool )
    253253{
    254254    assert( isBandwidth( b ) );
     
    269269    }
    270270
    271     /* notify the io buffers that there's more bandwidth available */
    272271    {
    273272        int i;
    274273        const int n = tr_ptrArraySize( b->iobufs );
    275274        for( i=0; i<n; ++i )
    276             tr_ptrArrayAppend( addme_buffers, tr_ptrArrayNth( b->iobufs, i ) );
     275            tr_ptrArrayAppend( iobuf_pool, tr_ptrArrayNth( b->iobufs, i ) );
    277276    }
    278277
     
    287286        struct tr_bandwidth ** children = (struct tr_bandwidth**) tr_ptrArrayPeek( b->children, &n );
    288287        for( i=0; i<n; ++i )
    289             tr_bandwidthAllocate( children[i], dir, period_msec );
     288            allocateBandwidth( children[i], dir, period_msec, iobuf_pool );
    290289    }
    291290}
     
    299298    tr_ptrArray * tmp;
    300299    struct tr_iobuf ** buffers;
    301     const short what = dir==TR_UP ? EV_WRITE : EV_READ;
     300    const size_t chunkSize = 1024; /* arbitrary */
    302301
    303302    tmp = tr_ptrArrayNew( );
     
    305304    buffers = (struct tr_iobuf**) tr_ptrArrayPeek( tmp, &n );
    306305
    307     /* notify the io buffers in a random order s.t. no
    308        particular peer gets to hog all the bandwidth */
    309     while( n > 0 ) {
    310         const int i = tr_cryptoRandInt( n );
    311         tr_iobuf_enable( buffers[i], what );
    312         buffers[i] = buffers[n-1];
    313         --n;
    314     }
    315 
     306    /* loop through all the peers, reading and writing in small chunks,
     307     * until we run out of bandwidth or peers. we do it this way to
     308     * prevent one peer from using up all the bandwidth */
     309    while( n > 0 )
     310    {
     311        int i;
     312        for( i=0; i<n; )
     313        {
     314            int byteCount;
     315            if( dir == TR_UP )
     316                byteCount = tr_iobuf_flush_output_buffer( buffers[i], chunkSize );
     317            else
     318                byteCount = tr_iobuf_tryread( buffers[i], chunkSize );
     319            if( byteCount == (int)chunkSize )
     320                ++i;
     321            else
     322                buffers[i] = buffers[--n];
     323        }
     324    }
     325
     326    /* cleanup */
    316327    tr_ptrArrayFree( tmp, NULL );
    317328}
Note: See TracChangeset for help on using the changeset viewer.