| 511 | } |
| 512 | |
| 513 | func (s *SyncClient) AddPeer(id peer.ID, shards map[common.Address][]uint64, direction network.Direction) bool { |
| 514 | s.lock.Lock() |
| 515 | if _, ok := s.peers[id]; ok { |
| 516 | s.lg.Debug("Peer was already registered", "peer", id) |
| 517 | s.lock.Unlock() |
| 518 | return true |
| 519 | } |
| 520 | if s.closingPeers { |
| 521 | s.lock.Unlock() |
| 522 | return false |
| 523 | } |
| 524 | if !s.needThisPeer(shards) { |
| 525 | s.lg.Debug("No need this peer, the connection would be closed later", "maxPeers", s.maxPeers, |
| 526 | "Peer count", len(s.peers), "peer", id.String(), "shards", shards) |
| 527 | s.metrics.IncDropPeerCount() |
| 528 | s.lock.Unlock() |
| 529 | return false |
| 530 | } |
| 531 | // add new peer routine |
| 532 | pr := NewPeer(0, s.chainID, id, s.newStreamFn, direction, s.syncerParams.InitRequestSize, s.storageManager.MaxKvSize(), shards) |
| 533 | s.peers[id] = pr |
| 534 | |
| 535 | s.idlerPeers[id] = struct{}{} |
| 536 | s.addPeerToTask(shards) |
| 537 | s.metrics.IncPeerCount() |
| 538 | s.lock.Unlock() |
| 539 | |
| 540 | s.notifyPeerJoin(id) |
| 541 | return true |
| 542 | } |
| 543 | |
| 544 | func (s *SyncClient) RemovePeer(id peer.ID) bool { |
| 545 | s.lock.Lock() |