Changeset 7765
- Timestamp:
- Jan 20, 2009, 3:47:25 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r7763 r7765 1984 1984 ***/ 1985 1985 1986 static int 1987 shouldPeerBeClosed( const Torrent * t, 1988 const tr_peer * peer, 1989 int peerCount ) 1986 typedef enum 1987 { 1988 TR_CAN_KEEP, 1989 TR_CAN_CLOSE, 1990 TR_MUST_CLOSE, 1991 } 1992 tr_close_type_t; 1993 1994 static tr_close_type_t 1995 shouldPeerBeClosed( const Torrent * t, 1996 const tr_peer * peer, 1997 int peerCount ) 1990 1998 { 1991 1999 const tr_torrent * tor = t->tor; … … 1998 2006 tordbg( t, "purging peer %s because its doPurge flag is set", 1999 2007 tr_peerIoAddrStr( &atom->addr, atom->port ) ); 2000 return TR UE;2008 return TR_MUST_CLOSE; 2001 2009 } 2002 2010 … … 2021 2029 tordbg( t, "purging peer %s because we're both seeds", 2022 2030 tr_peerIoAddrStr( &atom->addr, atom->port ) ); 2023 return TR UE;2031 return TR_MUST_CLOSE; 2024 2032 } 2025 2033 } … … 2042 2050 tordbg( t, "purging peer %s because it's been %d secs since we shared anything", 2043 2051 tr_peerIoAddrStr( &atom->addr, atom->port ), idleTime ); 2044 return TR UE;2045 } 2046 } 2047 2048 return FALSE;2052 return TR_CAN_CLOSE; 2053 } 2054 } 2055 2056 return TR_CAN_KEEP; 2049 2057 } 2050 2058 2051 2059 static tr_peer ** 2052 getPeersToClose( Torrent * t, int * setmeSize )2060 getPeersToClose( Torrent * t, tr_close_type_t closeType, int * setmeSize ) 2053 2061 { 2054 2062 int i, peerCount, outsize; … … 2059 2067 2060 2068 for( i = outsize = 0; i < peerCount; ++i ) 2061 if( shouldPeerBeClosed( t, peers[i], peerCount ) )2069 if( shouldPeerBeClosed( t, peers[i], peerCount ) == closeType ) 2062 2070 ret[outsize++] = peers[i]; 2063 2071 … … 2187 2195 } 2188 2196 2197 static void 2198 closePeer( Torrent * t, tr_peer * peer ) 2199 { 2200 struct peer_atom * atom; 2201 2202 assert( t != NULL ); 2203 assert( peer != NULL ); 2204 2205 /* if we transferred piece data, then they might be good peers, 2206 so reset their `numFails' weight to zero. otherwise we connected 2207 to them fruitlessly, so mark it as another fail */ 2208 atom = getExistingAtom( t, &peer->addr ); 2209 if( atom->piece_data_time ) 2210 atom->numFails = 0; 2211 else 2212 ++atom->numFails; 2213 2214 tordbg( t, "removing bad peer %s", tr_peerIoGetAddrStr( peer->io ) ); 2215 removePeer( t, peer ); 2216 } 2217 2189 2218 static int 2190 2219 reconnectPulse( void * vtorrent ) … … 2210 2239 else 2211 2240 { 2212 int i, nCandidates, nBad; 2213 struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates ); 2214 struct tr_peer ** connections = getPeersToClose( t, &nBad ); 2241 int i; 2242 int canCloseCount; 2243 int mustCloseCount; 2244 int candidateCount; 2215 2245 int maxCandidates; 2216 2217 maxCandidates = nCandidates; 2246 struct tr_peer ** canClose = getPeersToClose( t, TR_CAN_CLOSE, &canCloseCount ); 2247 struct tr_peer ** mustClose = getPeersToClose( t, TR_MUST_CLOSE, &mustCloseCount ); 2248 struct peer_atom ** candidates = getPeerCandidates( t, &candidateCount ); 2249 2250 tordbg( t, "reconnect pulse for [%s]: " 2251 "%d must-close connections, " 2252 "%d can-close connections, " 2253 "%d connection candidates, " 2254 "%d atoms, " 2255 "max per pulse is %d", 2256 t->tor->info.name, 2257 mustCloseCount, 2258 canCloseCount, 2259 candidateCount, 2260 tr_ptrArraySize( &t->pool ), 2261 MAX_RECONNECTIONS_PER_PULSE ); 2262 2263 /* disconnect the really bad peers */ 2264 for( i=0; i<mustCloseCount; ++i ) 2265 closePeer( t, mustClose[i] ); 2266 2267 /* decide how many peers can we try to add in this pass */ 2268 maxCandidates = candidateCount; 2218 2269 maxCandidates = MIN( maxCandidates, MAX_RECONNECTIONS_PER_PULSE ); 2219 2270 maxCandidates = MIN( maxCandidates, getMaxPeerCount( t->tor ) - getPeerCount( t ) ); 2220 2271 maxCandidates = MIN( maxCandidates, MAX_CONNECTIONS_PER_SECOND - newConnectionsThisSecond ); 2221 2272 2222 //if( nBad || nCandidates ) 2223 tordbg( t, "reconnect pulse for [%s]: %d bad connections, " 2224 "%d connection candidates, %d atoms, max per pulse is %d", 2225 t->tor->info.name, nBad, nCandidates, 2226 tr_ptrArraySize( &t->pool ), 2227 (int)MAX_RECONNECTIONS_PER_PULSE ); 2228 2229 /* disconnect some peers to make room for better ones. 2230 if we transferred piece data, then they might be good peers, 2231 so reset their `numFails' weight to zero. otherwise we connected 2232 to them fruitlessly, so mark it as another fail */ 2233 for( i = 0; ( i < nBad ) && ( i < maxCandidates ) ; ++i ) { 2234 tr_peer * peer = connections[i]; 2235 struct peer_atom * atom = getExistingAtom( t, &peer->addr ); 2236 if( atom->piece_data_time ) 2237 atom->numFails = 0; 2238 else 2239 ++atom->numFails; 2240 tordbg( t, "removing bad peer %s", tr_peerIoGetAddrStr( peer->io ) ); 2241 removePeer( t, peer ); 2242 } 2243 2244 tordbg( t, "nCandidates is %d, MAX_RECONNECTIONS_PER_PULSE is %d, getPeerCount(t) is %d, getMaxPeerCount(t) is %d, newConnectionsThisSecond is %d, MAX_CONNECTIONS_PER_SECOND is %d", 2245 (int)nCandidates, (int)MAX_RECONNECTIONS_PER_PULSE, (int)getPeerCount( t ), (int)getMaxPeerCount( t->tor ), (int)newConnectionsThisSecond, (int)MAX_CONNECTIONS_PER_SECOND ); 2273 /* maybe disconnect some lesser peers, if we have candidates to replace them with */ 2274 for( i=0; ( i<canCloseCount ) && ( i<maxCandidates ); ++i ) 2275 closePeer( t, canClose[i] ); 2276 2277 tordbg( t, "candidateCount is %d, MAX_RECONNECTIONS_PER_PULSE is %d," 2278 " getPeerCount(t) is %d, getMaxPeerCount(t) is %d, " 2279 "newConnectionsThisSecond is %d, MAX_CONNECTIONS_PER_SECOND is %d", 2280 candidateCount, 2281 MAX_RECONNECTIONS_PER_PULSE, 2282 getPeerCount( t ), 2283 getMaxPeerCount( t->tor ), 2284 newConnectionsThisSecond, MAX_CONNECTIONS_PER_SECOND ); 2246 2285 2247 2286 /* add some new ones */ 2248 for( i = 0; ( i < nCandidates ) 2249 && ( i < MAX_RECONNECTIONS_PER_PULSE ) 2250 && ( getPeerCount( t ) < getMaxPeerCount( t->tor ) ) 2251 && ( newConnectionsThisSecond < MAX_CONNECTIONS_PER_SECOND ); ++i ) 2287 for( i=0; i<maxCandidates; ++i ) 2252 2288 { 2253 tr_peerMgr *mgr = t->manager;2254 struct peer_atom * atom = candidates[i];2255 tr_peerIo *io;2289 tr_peerMgr * mgr = t->manager; 2290 struct peer_atom * atom = candidates[i]; 2291 tr_peerIo * io; 2256 2292 2257 2293 tordbg( t, "Starting an OUTGOING connection with %s", … … 2287 2323 2288 2324 /* cleanup */ 2289 tr_free( connections );2290 2325 tr_free( candidates ); 2326 tr_free( mustClose ); 2327 tr_free( canClose ); 2291 2328 } 2292 2329
Note: See TracChangeset
for help on using the changeset viewer.