Add() introduces a peer to the set
(p *Peer)
| 52 | |
| 53 | // Add() introduces a peer to the set |
| 54 | func (ps *PeerSet) Add(p *Peer) (err lib.ErrorI) { |
| 55 | // check if peer is already added |
| 56 | pubKey := lib.BytesToString(p.Address.PublicKey) |
| 57 | if _, found := ps.m[pubKey]; found { |
| 58 | return ErrPeerAlreadyExists(pubKey) |
| 59 | } |
| 60 | // ensure peer is not self |
| 61 | if bytes.Equal(p.Address.PublicKey, ps.publicKey) { |
| 62 | return nil |
| 63 | } |
| 64 | // if not trusted and not must connect, check inbound/outbound limits |
| 65 | if !p.IsTrusted && !p.IsMustConnect { |
| 66 | if err = ps.validateRegularPeerLimits(p); err != nil { |
| 67 | return |
| 68 | } |
| 69 | } |
| 70 | // increment counts |
| 71 | if p.IsOutbound { |
| 72 | ps.outbound++ |
| 73 | } else { |
| 74 | ps.inbound++ |
| 75 | } |
| 76 | // set the peer |
| 77 | ps.set(p) |
| 78 | // update metrics |
| 79 | ps.updateMetrics() |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | // validateRegularPeerLimits checks if adding a regular peer exceeds configured limits |
| 84 | func (ps *PeerSet) validateRegularPeerLimits(p *Peer) lib.ErrorI { |