Changeset 4356


Ignore:
Timestamp:
Dec 27, 2007, 9:48:41 PM (14 years ago)
Author:
charles
Message:

experimental code that should make tracker interaction (especially shutdown) much faster.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/tracker.c

    r4324 r4356  
    662662    snprintf( buf, sizeof(buf), "%s:%d", address->address, address->port );
    663663    evhttp_add_header( req->output_headers, "Host", buf );
    664     evhttp_add_header( req->output_headers, "Connection", "close" );
    665     evhttp_add_header( req->output_headers, "Content-Length", "0" );
    666664    evhttp_add_header( req->output_headers, "User-Agent",
    667665                                         TR_NAME "/" LONG_VERSION_STRING );
     
    763761struct tr_tracker_handle
    764762{
    765     int socketCount;
    766763    unsigned int isShuttingDown : 1;
    767764    tr_timer * pulseTimer;
    768765    tr_list * requestQueue;
    769766    tr_list * scrapeQueue;
     767    tr_list * connectionPool;
    770768};
    771769
     
    807805
    808806    if( globalsExist
    809         && ( handle->tracker->socketCount < 1 )
     807        && ( handle->tracker->connectionPool == NULL )
    810808        && ( handle->tracker->requestQueue == NULL )
    811809        && ( handle->tracker->scrapeQueue == NULL )
     
    832830    return FALSE;
    833831}
     832
     833static int
     834connectionPoolCompare( const void * a, const void * b )
     835{
     836    return a != b;
     837}
     838
    834839static void
    835840connectionClosedCB( struct evhttp_connection * evcon, void * vhandle )
    836841{
     842    char * address;
     843    unsigned short port;
    837844    tr_handle * handle = vhandle;
     845
     846    evhttp_connection_get_peer( evcon, &address, &port );
     847    dbgmsg( NULL, "%s:%d evcon [%s:%d] has closed... removing from pool", __FILE__, __LINE__, address, port );
     848
     849    tr_list_remove( &handle->tracker->connectionPool,
     850                    evcon,
     851                    connectionPoolCompare );
    838852
    839853    /* libevent references evcon right after calling this function,
     
    844858
    845859static struct evhttp_connection*
     860getExistingConnection( tr_handle * handle, const char * address, int port )
     861{
     862    tr_list * l;
     863    dbgmsg( NULL, "%s:%d looking for a connection to {%s:%d}\n", __FILE__, __LINE__, address, port );
     864
     865    for( l=handle->tracker->connectionPool; l!=NULL; l=l->next )
     866    {
     867        char * c_address;
     868        unsigned short c_port;
     869        evhttp_connection_get_peer( l->data, &c_address, &c_port );
     870        dbgmsg( NULL, "comparing with [%s:%d]...", c_address, c_port );
     871        if( ( port == c_port ) && !strcmp( address, c_address ) ) {
     872            dbgmsg( NULL, "%s:%d reusing cached connection of [%s:%d]\n", __FILE__, __LINE__, address, port );
     873            return l->data;
     874        }
     875    }
     876
     877    dbgmsg( NULL, "%s:%d no match.\n", __FILE__, __LINE__ );
     878    return NULL;
     879}
     880
     881static struct evhttp_connection*
    846882getConnection( tr_handle * handle, const char * address, int port )
    847883{
    848     struct evhttp_connection * c = evhttp_connection_new( address, port );
    849     evhttp_connection_set_closecb( c, connectionClosedCB, handle );
     884    struct evhttp_connection * c = getExistingConnection( handle, address, port );
     885
     886    if( c == NULL )
     887    {
     888        dbgmsg( NULL, "%s:%d creating new connection for [%s:%d]\n", __FILE__, __LINE__, address, port );
     889        c = evhttp_connection_new( address, port );
     890        evhttp_connection_set_closecb( c, connectionClosedCB, handle );
     891        tr_list_prepend( &handle->tracker->connectionPool, c );
     892    }
     893
    850894    return c;
    851895}
     
    860904    if( evhttp_make_request( evcon, req->req, EVHTTP_REQ_GET, req->uri ))
    861905        publishErrorMessageAndStop( t, "Tracker could not be reached." );
    862     else {
    863         ++handle->tracker->socketCount;
    864         dbgmsg( t, "incremented socket count to %d", handle->tracker->socketCount );
    865     }
    866906}
    867907
     
    872912    invokeRequest( handle, req );
    873913    freeRequest( req );
    874 }
    875 
    876 static int
    877 socketIsAvailable( tr_handle * handle )
    878 {
    879     const int max = handle->tracker->isShuttingDown
    880                       ? MAX_TRACKER_SOCKETS_DURING_SHUTDOWN
    881                       : MAX_TRACKER_SOCKETS;
    882     return handle->tracker->socketCount < max;
    883914}
    884915
     
    914945        return FALSE;
    915946
    916     if( handle->tracker->socketCount || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )
    917         dbgmsg( NULL, "tracker pulse... %d sockets, %d reqs left, %d scrapes left", handle->tracker->socketCount, tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );
     947    if( tr_list_size(th->connectionPool) || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )
     948        dbgmsg( NULL, "tracker pulse... %d sockets, %d reqs left, %d scrapes left", tr_list_size(th->connectionPool), tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );
    918949
    919950    /* upkeep: queue periodic rescrape / reannounce */
     
    933964    }
    934965
    935     if( handle->tracker->socketCount || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )
    936         dbgmsg( NULL, "tracker pulse after upkeep... %d sockets, %d reqs left, %d scrapes left", handle->tracker->socketCount, tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );
     966    if( tr_list_size(th->connectionPool) || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )
     967        dbgmsg( NULL, "tracker pulse after upkeep... %d sockets, %d reqs left, %d scrapes left", tr_list_size(th->connectionPool), tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );
    937968
    938969    /* look for things to do... process all the requests, then process all the scrapes */
    939     while( th->requestQueue && socketIsAvailable( handle ) )
     970    while( th->requestQueue )
    940971        invokeNextInQueue( handle, &th->requestQueue );
    941     while( th->scrapeQueue && socketIsAvailable( handle ) )
     972    while( th->scrapeQueue )
    942973        invokeNextInQueue( handle, &th->scrapeQueue );
    943974
    944     if( handle->tracker->socketCount || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )
    945         dbgmsg( NULL, "tracker pulse done... %d sockets, %d reqs left, %d scrapes left", handle->tracker->socketCount, tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );
     975    if( tr_list_size(th->connectionPool) || tr_list_size(th->requestQueue) || tr_list_size(th->scrapeQueue) )
     976        dbgmsg( NULL, "tracker pulse done... %d sockets, %d reqs left, %d scrapes left", tr_list_size(th->connectionPool), tr_list_size(th->requestQueue), tr_list_size(th->scrapeQueue) );
    946977
    947978    return maybeFreeGlobals( handle );
     
    954985    {
    955986        pulse( handle );
    956         --handle->tracker->socketCount;
    957         dbgmsg( NULL, "decrementing socket count to %d", handle->tracker->socketCount );
    958987    }
    959988}
Note: See TracChangeset for help on using the changeset viewer.