From 39b1db460d2d36dd8b710db96c9db2502c002adb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 11 Jan 2008 02:09:20 +0000 Subject: [PATCH] follow the spec's suggestion that new connections be 3x as likely for optimistic unchoke --- libtransmission/peer-io.c | 8 ++++++++ libtransmission/peer-io.h | 4 +++- libtransmission/peer-mgr.c | 35 +++++++++++++++++++++++++++++++--- libtransmission/torrent-ctor.c | 2 +- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index f258b2e82..7e83fefe6 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -58,6 +58,7 @@ struct tr_peerIo int timeout; struct bufferevent * bufev; uint8_t peerId[20]; + time_t timeCreated; tr_extensions extensions; @@ -146,6 +147,7 @@ tr_peerIoNew( struct tr_handle * handle, c->socket = socket; c->isIncoming = isIncoming ? 1 : 0; c->timeout = IO_TIMEOUT_SECS; + c->timeCreated = time( NULL ); c->bufev = bufferevent_new( c->socket, canReadWrapper, didWriteWrapper, @@ -582,3 +584,9 @@ tr_peerIoDrain( tr_peerIo * io, tr_peerIoReadBytes( io, inbuf, tmp, byteCount ); tr_free( tmp ); } + +int +tr_peerGetAge( const tr_peerIo * io ) +{ + return time( NULL ) - io->timeCreated; +} diff --git a/libtransmission/peer-io.h b/libtransmission/peer-io.h index 049c3605d..dc3d1e882 100644 --- a/libtransmission/peer-io.h +++ b/libtransmission/peer-io.h @@ -80,7 +80,9 @@ int tr_peerIoReconnect( tr_peerIo * io ); int tr_peerIoIsIncoming( const tr_peerIo * io ); -void tr_peerIoSetTimeoutSecs( tr_peerIo * io, int secs ); +void tr_peerIoSetTimeoutSecs( tr_peerIo * io, int secs ); + +int tr_peerIoGetAge( const tr_peerIo * io ); /** diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 16679e37c..e5f5e12a2 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -140,6 +140,7 @@ typedef struct tr_timer * refillTimer; tr_timer * swiftTimer; tr_torrent * tor; + tr_peer * optimistic; /* the optimistic peer, or NULL if none */ tr_bitfield * requested; unsigned int isRunning : 1; @@ -1480,6 +1481,7 @@ tr_peerMgrPeerStats( const tr_peerMgr * manager, stat->isUploadingTo = stat->peerIsInterested && !stat->peerIsChoked; pch = stat->flagStr; + if( t->optimistic == peer ) *pch++ = 'O'; if( stat->isDownloadingFrom ) *pch++ = 'D'; else if( stat->clientIsInterested ) *pch++ = 'd'; if( stat->isUploadingTo ) *pch++ = 'U'; @@ -1519,6 +1521,18 @@ compareChoke( const void * va, const void * vb ) return -tr_compareUint32( a->rate, b->rate ); } +static int +isNew( const tr_peer * peer ) +{ + return peer && peer->io && tr_peerIoGetAge( peer->io ) < 45; +} + +static int +isSame( const tr_peer * peer ) +{ + return peer && peer->client && strstr( peer->client, "Transmission" ); +} + /** *** **/ @@ -1580,9 +1594,24 @@ rechoke( Torrent * t ) choke[i++].doUnchoke = 0; /* optimistic unchoke */ - if( i != size ) { - i = n + tr_rand( size - n ); - choke[i].doUnchoke = 1; + if( i < size ) + { + struct ChokeData * c; + tr_ptrArray * randPool = tr_ptrArrayNew( ); + for( ; idoUnchoke = 1; + t->optimistic = c->peer; + tr_ptrArrayFree( randPool, NULL ); } for( i=0; i