@@ -439,6 +439,7 @@ func (p *Protocol) mustFetchChunk(req *types.ChunkInfoMsg) (*types.BlockBodys, p
439
439
alternativePeers := make (chan peer.ID , 100 )
440
440
for _ , pid := range peers {
441
441
localPeers <- pid
442
+ searchedPeers [pid ] = struct {}{}
442
443
}
443
444
444
445
// 优先从已经建立连接的节点上查找数据,因为建立新的连接会耗时,且会导致网络拓扑结构发生变化
@@ -448,10 +449,6 @@ func (p *Protocol) mustFetchChunk(req *types.ChunkInfoMsg) (*types.BlockBodys, p
448
449
case <- ctx .Done ():
449
450
return nil , "" , types2 .ErrNotFound
450
451
case pid := <- localPeers :
451
- if _ , ok := searchedPeers [pid ]; ok {
452
- continue
453
- }
454
- searchedPeers [pid ] = struct {}{}
455
452
start := time .Now ()
456
453
bodys , nearerPeers , err := p .fetchChunkFromPeer (req , pid )
457
454
if err != nil {
@@ -462,16 +459,21 @@ func (p *Protocol) mustFetchChunk(req *types.ChunkInfoMsg) (*types.BlockBodys, p
462
459
return bodys , pid , nil
463
460
}
464
461
for _ , pid := range nearerPeers {
462
+ if _ , ok := searchedPeers [pid ]; ok {
463
+ continue
464
+ }
465
465
if len (p .Host .Network ().ConnsToPeer (pid )) != 0 {
466
466
select {
467
467
case localPeers <- pid :
468
+ searchedPeers [pid ] = struct {}{}
468
469
default :
469
470
log .Info ("mustFetchChunk localPeers channel full" , "pid" , pid )
470
471
}
471
472
472
473
} else if len (p .Host .Peerstore ().Addrs (pid )) != 0 {
473
474
select {
474
475
case alternativePeers <- pid :
476
+ searchedPeers [pid ] = struct {}{}
475
477
default :
476
478
log .Info ("mustFetchChunk alternativePeers channel full" , "pid" , pid )
477
479
}
@@ -491,10 +493,6 @@ func (p *Protocol) mustFetchChunk(req *types.ChunkInfoMsg) (*types.BlockBodys, p
491
493
case <- ctx .Done ():
492
494
return nil , "" , types2 .ErrNotFound
493
495
case pid := <- alternativePeers :
494
- if _ , ok := searchedPeers [pid ]; ok {
495
- continue
496
- }
497
- searchedPeers [pid ] = struct {}{}
498
496
start := time .Now ()
499
497
bodys , nearerPeers , err := p .fetchChunkFromPeer (req , pid )
500
498
if err != nil {
@@ -505,9 +503,13 @@ func (p *Protocol) mustFetchChunk(req *types.ChunkInfoMsg) (*types.BlockBodys, p
505
503
return bodys , pid , nil
506
504
}
507
505
for _ , pid := range nearerPeers {
506
+ if _ , ok := searchedPeers [pid ]; ok {
507
+ continue
508
+ }
508
509
if len (p .Host .Peerstore ().Addrs (pid )) != 0 {
509
510
select {
510
511
case alternativePeers <- pid :
512
+ searchedPeers [pid ] = struct {}{}
511
513
default :
512
514
log .Info ("mustFetchChunk alternativePeers channel full" , "pid" , pid )
513
515
}
0 commit comments