Changeset 3393 for trunk/libtransmission/peer-mgr.c
- Timestamp:
- Oct 13, 2007, 1:54:05 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r3362 r3393 55 55 SNUBBED_SEC = 60, 56 56 57 /* this is arbitrary and, hopefully, temporary until we come up 58 * with a better idea for managing the connection limits */ 57 /* arbitrary */ 59 58 MAX_CONNECTED_PEERS_PER_TORRENT = 60, 59 60 /* when many peers are available, keep idle ones this long */ 61 MIN_UPLOAD_IDLE_SECS = 60, 62 63 /* when few peers are available, keep idle ones this long */ 64 MAX_UPLOAD_IDLE_SECS = 240, 60 65 61 66 /* how many peers to unchoke per-torrent. */ … … 1480 1485 /*** 1481 1486 **** 1482 **** 1487 **** Life and Death 1483 1488 **** 1484 1489 ***/ 1485 1490 1486 #define LAISSEZ_FAIRE_PERIOD_SECS 90 1491 static int 1492 shouldPeerBeClosed( const Torrent * t, const tr_peer * peer, int peerCount ) 1493 { 1494 const tr_torrent * tor = t->tor; 1495 const time_t now = time( NULL ); 1496 const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); 1497 1498 /* if it's marked for purging, close it */ 1499 if( peer->doPurge ) { 1500 tordbg( t, "purging peer %p because its doPurge flag is set", tr_peerIoAddrStr(&atom->addr,atom->port) ); 1501 return TRUE; 1502 } 1503 1504 /* if we're both seeds and it's been long enough for a pex exchange, close it */ 1505 if( 1 ) { 1506 const int clientIsSeed = tr_cpGetStatus( tor->completion ) != TR_CP_INCOMPLETE; 1507 const int peerIsSeed = atom->flags & ADDED_F_SEED_FLAG; 1508 if( peerIsSeed && clientIsSeed && ( tor->pexDisabled || (now-atom->time>=30) ) ) { 1509 tordbg( t, "purging peer %p because we're both seeds", tr_peerIoAddrStr(&atom->addr,atom->port) ); 1510 return TRUE; 1511 } 1512 } 1513 1514 /* disconnect if it's been too long since piece data has been transferred. 1515 * this is on a sliding scale based on number of available peers... */ 1516 if( 1 ) { 1517 const int relaxStrictnessIfFewerThanN = (int)((MAX_CONNECTED_PEERS_PER_TORRENT * 0.9) + 0.5); 1518 /* if we have >= relaxIfFewerThan, strictness is 100%. 1519 * if we have zero connections, strictness is 0% */ 1520 const double strictness = peerCount >= relaxStrictnessIfFewerThanN 1521 ? 1.0 1522 : peerCount / (double)relaxStrictnessIfFewerThanN; 1523 const int lo = MIN_UPLOAD_IDLE_SECS; 1524 const int hi = MAX_UPLOAD_IDLE_SECS; 1525 const int limit = lo + ((hi-lo) * strictness); 1526 const time_t then = peer->pieceDataActivityDate; 1527 const int idleTime = then ? (now-then) : 0; 1528 if( idleTime > limit ) { 1529 tordbg( t, "purging peer %p it's been %d secs since we shared anything", 1530 tr_peerIoAddrStr(&atom->addr,atom->port), idleTime ); 1531 return TRUE; 1532 } 1533 } 1534 1535 return FALSE; 1536 } 1487 1537 1488 1538 static tr_peer ** 1489 getWeakConnections( Torrent * t, int * setmeSize ) 1490 { 1491 int i, insize, outsize; 1492 tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, &insize ); 1493 struct tr_peer ** ret = tr_new( tr_peer*, insize ); 1494 const int clientIsSeed = tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE; 1495 const time_t now = time( NULL ); 1539 getPeersToClose( Torrent * t, int * setmeSize ) 1540 { 1541 int i, peerCount, outsize; 1542 tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, &peerCount ); 1543 struct tr_peer ** ret = tr_new( tr_peer*, peerCount ); 1496 1544 1497 1545 assert( torrentIsLocked( t ) ); 1498 1546 1499 for( i=outsize=0; i<insize; ++i ) 1500 { 1501 tr_peer * peer = peers[i]; 1502 int isWeak; 1503 const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); 1504 const int peerIsSeed = atom->flags & ADDED_F_SEED_FLAG; 1505 const double throughput = getWeightedThroughput( peer ); 1506 1507 assert( atom != NULL ); 1508 1509 if( peer->doPurge ) { 1510 tordbg( t, "purging peer %s because it's got doPurge flagged", tr_peerIoAddrStr(&atom->addr,atom->port) ); 1511 isWeak = TRUE; 1512 } else if( throughput >= 3 ) { 1513 tordbg( t, "keeping peer %s because it's got a good throughput", tr_peerIoAddrStr(&atom->addr,atom->port) ); 1514 isWeak = FALSE; 1515 } else if( peerIsSeed && clientIsSeed ) { 1516 tordbg( t, "%s peer is a seed and so are we", tr_peerIoAddrStr(&atom->addr,atom->port) ); 1517 isWeak = t->tor->pexDisabled || (now-atom->time>=30); 1518 } else if( !atom->time || ( ( now - atom->time ) < LAISSEZ_FAIRE_PERIOD_SECS ) ) { 1519 tordbg( t, "%s too new to purge", tr_peerIoAddrStr(&atom->addr,atom->port) ); 1520 isWeak = FALSE; 1521 } else { 1522 isWeak = ( now - peer->pieceDataActivityDate ) > 180; 1523 tordbg( t, "%s peer %p", (isWeak?"purging":"keeping"), tr_peerIoAddrStr(&atom->addr,atom->port) ); 1524 } 1525 1526 if( isWeak ) 1527 ret[outsize++] = peer; 1528 } 1547 for( i=outsize=0; i<peerCount; ++i ) 1548 if( shouldPeerBeClosed( t, peers[i], peerCount ) ) 1549 ret[outsize++] = peers[i]; 1529 1550 1530 1551 *setmeSize = outsize; … … 1582 1603 1583 1604 /* if we used this peer recently, give someone else a turn */ 1584 if( ( now - atom->time ) < LAISSEZ_FAIRE_PERIOD_SECS) {1605 if( ( now - atom->time ) < 60 ) { 1585 1606 tordbg( t, "RECONNECT peer %d (%s) is in its grace period..", 1586 1607 i, tr_peerIoAddrStr(&atom->addr,atom->port) ); … … 1609 1630 else 1610 1631 { 1611 int i, nCandidates, n Weak, nAdd;1632 int i, nCandidates, nBad, nAdd; 1612 1633 struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates ); 1613 struct tr_peer ** connections = get WeakConnections( t, &nWeak);1634 struct tr_peer ** connections = getPeersToClose( t, &nBad ); 1614 1635 const int peerCount = tr_ptrArraySize( t->peers ); 1615 1636 1616 if( n Weak|| nCandidates )1617 tordbg( t, "reconnect pulse for [%s]: %d weakconnections, "1637 if( nBad || nCandidates ) 1638 tordbg( t, "reconnect pulse for [%s]: %d bad connections, " 1618 1639 "%d connection candidates, %d atoms, max per pulse is %d", 1619 t->tor->info.name, n Weak, nCandidates,1640 t->tor->info.name, nBad, nCandidates, 1620 1641 tr_ptrArraySize(t->pool), 1621 1642 (int)MAX_RECONNECTIONS_PER_PULSE ); 1622 1643 1623 1644 /* disconnect some peers */ 1624 for( i=0; i<n Weak; ++i )1645 for( i=0; i<nBad; ++i ) 1625 1646 removePeer( t, connections[i] ); 1626 1647
Note: See TracChangeset
for help on using the changeset viewer.