@cryptotaxi247 / kubo / commits / 98f9ee916

a better fix for duplicate peers in getPeers()

Jeromy committed Jan 11, 2015 at 03:09 UTC 98f9ee916e13f6c61ae786ea140e0a01586140a5
1 file changed +12 -7
diagnostics/diag.go
+12 -7
@@ -98,13 +98,18 @@ func (di *DiagInfo) Marshal() []byte {
98 }
99
100 func (d *Diagnostics) getPeers() []peer.ID {
101 - conns := d.host.Network().Conns()
102 - peers := make([]peer.ID, len(conns))
103 -
104 - for i, c := range conns {
105 - peers[i] = c.RemotePeer()
101 + peers := d.host.Network().Peers()
102 + pmap := make(map[peer.ID]struct{})
103 + out := make([]peer.ID, 0, len(peers))
104 + for _, p := range peers {
105 + _, ok := pmap[p]
106 + if !ok {
107 + out = append(out, p)
108 + pmap[p] = struct{}{}
109 + }
110 }
107 - return peers
111 +
112 + return out
113 }
114
115 func (d *Diagnostics) getDiagInfo() *DiagInfo {
@@ -239,7 +244,7 @@ func (d *Diagnostics) sendRequest(ctx context.Context, p peer.ID, pmes *pb.Messa
244 }
245
246 func (d *Diagnostics) handleDiagnostic(p peer.ID, pmes *pb.Message) (*pb.Message, error) {
242 - log.Debugf("HandleDiagnostic from %s for id = %s", p, pmes.GetDiagID())
247 + log.Debugf("HandleDiagnostic from %s for id = %s", p, util.Key(pmes.GetDiagID()).B58String())
248 resp := newMessage(pmes.GetDiagID())
249
250 // Make sure we havent already handled this request to prevent loops