Add adds the peer to the PeerSet. It returns an error carrying the reason, if the peer is already present.
(peer Peer)
| 41 | // Add adds the peer to the PeerSet. |
| 42 | // It returns an error carrying the reason, if the peer is already present. |
| 43 | func (ps *PeerSet) Add(peer Peer) error { |
| 44 | ps.mtx.Lock() |
| 45 | defer ps.mtx.Unlock() |
| 46 | |
| 47 | if ps.lookup[peer.ID()] != nil { |
| 48 | return ErrSwitchDuplicatePeerID{peer.ID()} |
| 49 | } |
| 50 | if peer.GetRemovalFailed() { |
| 51 | return ErrPeerRemoval{} |
| 52 | } |
| 53 | |
| 54 | index := len(ps.list) |
| 55 | // Appending is safe even with other goroutines |
| 56 | // iterating over the ps.list slice. |
| 57 | ps.list = append(ps.list, peer) |
| 58 | ps.lookup[peer.ID()] = &peerSetItem{peer, index} |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // Has returns true if the set contains the peer referred to by this |
| 63 | // peerKey, otherwise false. |