close <div id="moderation_required">Attachments you submit will be routed for moderation. If you have an account, please <a href="/login">log in</a> first.</div>

Ticket #3600: fixSlowSpeed4.patch

File fixSlowSpeed4.patch, 5.5 KB (added by charles, 5 years ago)
  • peer-mgr.c

     
    202202
    203203    int                        interestedCount;
    204204    int                        maxPeers;
    205     tr_recentHistory         * noBlocksCancelsCount;
    206 
    207     /* An arbitrary metric of how congested the downloads are.
    208      * Based on how many of requests are cancelled and how many are completed.
    209      * Lower values indicate less congestion. */
    210     double                     cancelRate;
     205    time_t                     lastCancel;
    211206}
    212207Torrent;
    213208
     
    460455    tr_ptrArrayDestruct( &t->pool, (PtrArrayForeachFunc)tr_free );
    461456    tr_ptrArrayDestruct( &t->outgoingHandshakes, NULL );
    462457    tr_ptrArrayDestruct( &t->peers, NULL );
    463     tr_historyFree( t->noBlocksCancelsCount );
    464458
    465459    tr_free( t->requests );
    466460    tr_free( t->pieces );
     
    483477    t->peers = TR_PTR_ARRAY_INIT;
    484478    t->webseeds = TR_PTR_ARRAY_INIT;
    485479    t->outgoingHandshakes = TR_PTR_ARRAY_INIT;
    486     t->noBlocksCancelsCount = tr_historyNew( NO_BLOCKS_CANCEL_HISTORY, ( RECHOKE_PERIOD_MSEC / 1000 ) );
    487480
    488481    for( i = 0; i < tor->info.webseedCount; ++i )
    489482    {
     
    20722065    ensureMgrTimersExist( t->manager );
    20732066
    20742067    t->isRunning = TRUE;
     2068    t->maxPeers = t->tor->maxConnectedPeers;
    20752069
    20762070    rechokePulse( 0, 0, t->manager );
    20772071    managerUnlock( t->manager );
     
    24632457    {
    24642458        int blocks = 0;
    24652459        int cancels = 0;
     2460        time_t timeSinceCancel;
    24662461
    24672462        /* Count up how many blocks & cancels each peer has.
    24682463         *
     
    24912486            cancels += c;
    24922487        }
    24932488
    2494         if( !t->maxPeers )
     2489        if( cancels > 0 )
    24952490        {
    2496             /* this is the torrent's first time to call this function...
    2497              * start off optimistically by allowing interest in many peers */
    2498             maxPeers = t->tor->maxConnectedPeers;
     2491            /* cancelRate: of the block requests we've recently made, the percentage we cancelled.
     2492             * higher values indicate more congestion. */
     2493            const double cancelRate = cancels / (double)(cancels + blocks);
     2494            const double mult = 1 - MIN( cancelRate, 0.5 );
     2495            maxPeers = t->interestedCount * mult;
     2496            tordbg( t, "cancel rate is %.3f -- reducing the "
     2497                       "number of peers we're interested in by %.0f percent",
     2498                       cancelRate, mult * 100 );
     2499            t->lastCancel = now;
    24992500        }
    2500         else if( !blocks && cancels )
    2501         {
    2502             /* we've gotten cancels but zero blocks...
    2503              * something is seriously wrong.  throttle back sharply */
    2504             maxPeers = t->interestedCount * 0.5;
    2505         }
    2506         else if( blocks )
    2507         {
    2508             const double cancelRate = cancels / (double)(cancels + blocks );
    25092501
    2510             /* if we're getting cancels then use interestedCount instead of
    2511              * maxPeers to scale faster */
    2512                  if( cancelRate >= 0.20 ) maxPeers = t->interestedCount * 0.7;
    2513             else if( cancelRate >= 0.10 ) maxPeers = t->interestedCount * 0.8;
    2514             else if( cancelRate >= 0.05 ) maxPeers = t->interestedCount * 0.9;
    2515             else if( cancelRate >= 0.01 ) maxPeers = t->interestedCount;
    2516             else                          maxPeers = t->maxPeers + 1;
    2517 
    2518             /* if things are getting worse, don't add more peers */
    2519             if( ( t->cancelRate > 0.01 ) && ( cancelRate > t->cancelRate ) )
    2520                 maxPeers = MIN( maxPeers, t->maxPeers );
    2521 
    2522             t->cancelRate = cancelRate;
    2523 
    2524             tordbg( t, "cancel rate is %.3f -- changing the "
    2525                        "number of peers we're interested in from %d to %d",
    2526                        cancelRate, t->maxPeers, maxPeers );
    2527         }
    2528         else
     2502        timeSinceCancel = now - t->lastCancel;
     2503        if( timeSinceCancel )
    25292504        {
    2530             const unsigned maxCount = 10; /* maximum times in this block in the past two minutes */
    2531             tr_historyAdd( t->noBlocksCancelsCount, now, 1 );
    2532             if( tr_historyGet( t->noBlocksCancelsCount, now, NO_BLOCKS_CANCEL_HISTORY ) < maxCount )
    2533             {
    2534                 /* no blocks and no cancels means either that the torrent
    2535                  * just started or peers are unresponsive/nonexistent,
    2536                  * either way there's nothing to do */
    2537                 maxPeers = t->maxPeers;
    2538             }
    2539             else
    2540             {
    2541                 /* we've been in here for a while so maybe the network is down or there are no
    2542                  * good peers. maximize the chance of connecting to a good peer
    2543                  * if/when they show up */
    2544                 maxPeers = t->tor->maxConnectedPeers;
    2545             }
     2505            const int maxIncrease = 15;
     2506            const time_t maxHistory = 2 * CANCEL_HISTORY_SEC;
     2507            const double mult = MIN( timeSinceCancel, maxHistory ) / (double) maxHistory;
     2508            const int inc = maxIncrease * mult;
     2509            maxPeers = t->maxPeers + inc;
     2510            tordbg( t, "time since last cancel is %li -- increasing the "
     2511                       "number of peers we're interested in by %d",
     2512                       timeSinceCancel, inc );
    25462513        }
    25472514    }
    25482515
    2549     /* don't let the previous paragraph's number tweaking go too far... */
     2516    /* don't let the previous section's number tweaking go too far... */
    25502517    if( maxPeers < MIN_INTERESTING_PEERS )
    25512518        maxPeers = MIN_INTERESTING_PEERS;
    25522519    if( maxPeers > t->tor->maxConnectedPeers )