Changeset 5980


Ignore:
Timestamp:
May 31, 2008, 12:16:26 AM (13 years ago)
Author:
charles
Message:

experimental message batcher as discussed in http://forum.transmissionbt.com/viewtopic.php?p=24251#24251

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-msgs.c

    r5932 r5980  
    6565
    6666    /* idle seconds before we send a keepalive */
    67     KEEPALIVE_INTERVAL_SECS = 90,
     67    KEEPALIVE_INTERVAL_SECS = 100,
    6868
    6969    PEX_INTERVAL            = (90 * 1000), /* msec between sendPex() calls */
     
    8585    /* how long a sent request can stay queued before it's returned
    8686       back to the peer-mgr's pool of requests */
    87     SENT_REQUEST_TTL_SECS = 120
     87    SENT_REQUEST_TTL_SECS = 120,
     88
     89    /* used in lowering the outMessages queue period */
     90    IMMEDIATE_PRIORITY_INTERVAL_SECS = 0,
     91    HIGH_PRIORITY_INTERVAL_SECS = 5,
     92    LOW_PRIORITY_INTERVAL_SECS = 30
    8893};
    8994
     
    268273    time_t clientSentPexAt;
    269274    time_t clientSentAnythingAt;
     275
     276    /* when we started batching the outMessages */
     277    time_t outMessagesBatchedAt;
     278
     279    /* how long the outMessages batch should be allowed to grow before
     280     * it's flushed -- some messages (like requests >:) should be sent
     281     * very quickly; others aren't as urgent. */
     282    int outMessagesBatchPeriod;
    270283   
    271284    tr_bitfield * peerAllowedPieces;
     
    316329
    317330static void
     331pokeBatchPeriod( tr_peermsgs * msgs, int interval )
     332{
     333    if( msgs->outMessagesBatchPeriod > interval )
     334    {
     335        msgs->outMessagesBatchPeriod = interval;
     336        dbgmsg( msgs, "lowering batch interval to %d seconds", interval );
     337    }
     338}
     339
     340static void
    318341protocolSendRequest( tr_peermsgs * msgs, const struct peer_request * req )
    319342{
     
    327350    tr_peerIoWriteUint32( io, out, req->offset );
    328351    tr_peerIoWriteUint32( io, out, req->length );
     352    pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    329353}
    330354
     
    341365    tr_peerIoWriteUint32( io, out, req->offset );
    342366    tr_peerIoWriteUint32( io, out, req->length );
     367    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    343368}
    344369
     
    353378    tr_peerIoWriteUint8 ( io, out, BT_HAVE );
    354379    tr_peerIoWriteUint32( io, out, index );
     380    pokeBatchPeriod( msgs, LOW_PRIORITY_INTERVAL_SECS );
    355381}
    356382
     
    364390    tr_peerIoWriteUint32( io, out, sizeof(uint8_t) );
    365391    tr_peerIoWriteUint8 ( io, out, choke ? BT_CHOKE : BT_UNCHOKE );
     392    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    366393}
    367394
     
    497524    tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages,
    498525                   weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED );
     526    pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    499527}
    500528
     
    596624        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, offset );
    597625        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, length );
     626        pokeBatchPeriod( msgs, LOW_PRIORITY_INTERVAL_SECS );
    598627    }
    599628}
     
    625654        tr_peerIoWriteUint8( msgs->io, msgs->outMessages, BT_ALLOWED_FAST );
    626655        tr_peerIoWriteUint32( msgs->io, msgs->outMessages, pieceIndex );
     656        pokeBatchPeriod( msgs, LOW_PRIORITY_INTERVAL_SECS );
    627657    }
    628658}
     
    15711601    dbgmsg( msgs, "sending a keepalive message" );
    15721602    tr_peerIoWriteUint32( msgs->io, msgs->outMessages, 0 );
     1603    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    15731604}
    15741605
     
    16571688    {
    16581689        struct peer_request req;
    1659 
    1660         if(( EVBUFFER_LENGTH( msgs->outMessages ) ))
     1690        int haveMessages = EVBUFFER_LENGTH( msgs->outMessages ) != 0;
     1691
     1692        if( haveMessages && !msgs->outMessagesBatchedAt ) /* fresh batch */
    16611693        {
    1662             dbgmsg( msgs, "flushing outMessages..." );
     1694            dbgmsg( msgs, "started an outMessages batch (length is %d)", (int)EVBUFFER_LENGTH( msgs->outMessages ) );
     1695            msgs->outMessagesBatchedAt = now;
     1696        }
     1697        else if( haveMessages
     1698                 && ( ( now - msgs->outMessagesBatchedAt ) > msgs->outMessagesBatchPeriod ) )
     1699        {
     1700            dbgmsg( msgs, "flushing outMessages... (length is %d)", (int)EVBUFFER_LENGTH( msgs->outMessages ) );
    16631701            tr_peerIoWriteBuf( msgs->io, msgs->outMessages );
    16641702            msgs->clientSentAnythingAt = now;
     1703            msgs->outMessagesBatchedAt = 0;
     1704            msgs->outMessagesBatchPeriod = LOW_PRIORITY_INTERVAL_SECS;
    16651705        }
    16661706        else if( !EVBUFFER_LENGTH( msgs->outBlock )
     
    16871727            tr_free( buf );
    16881728        }
    1689         else if( ( now - msgs->clientSentAnythingAt ) > KEEPALIVE_INTERVAL_SECS )
     1729        else if(    ( !haveMessages )
     1730                 && ( now - msgs->clientSentAnythingAt ) > KEEPALIVE_INTERVAL_SECS )
    16901731        {
    16911732            sendKeepalive( msgs );
     
    17171758    tr_peerIoWriteUint8 ( msgs->io, out, BT_BITFIELD );
    17181759    tr_peerIoWriteBytes ( msgs->io, out, bitfield->bits, bitfield->len );
     1760    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    17191761}
    17201762
     
    18391881        tr_peerIoWriteUint8 ( msgs->io, msgs->outMessages, msgs->ut_pex_id );
    18401882        tr_peerIoWriteBytes ( msgs->io, msgs->outMessages, benc, bencLen );
     1883        pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    18411884
    18421885        /* cleanup */
     
    18901933    m->pexTimer = tr_timerNew( m->handle, pexPulse, m, PEX_INTERVAL );
    18911934    m->outMessages = evbuffer_new( );
     1935    m->outMessagesBatchedAt = 0;
     1936    m->outMessagesBatchPeriod = LOW_PRIORITY_INTERVAL_SECS;
    18921937    m->incoming.block = evbuffer_new( );
    18931938    m->outBlock = evbuffer_new( );
Note: See TracChangeset for help on using the changeset viewer.