Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
remove dependency on the go-libp2p-peerstore/addr package (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann authored Jan 3, 2022
1 parent f6e5e13 commit 80f7185
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions backoffcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

"github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-peerstore/addr"

ma "github.com/multiformats/go-multiaddr"
)

// BackoffDiscovery is an implementation of discovery that caches peer data and attenuates repeated queries
Expand Down Expand Up @@ -277,7 +278,20 @@ func findPeerReceiver(ctx context.Context, pch, evtCh chan peer.AddrInfo, rcvPee
}

func mergeAddrInfos(prevAi, newAi peer.AddrInfo) *peer.AddrInfo {
combinedAddrs := addr.UniqueSource(addr.Slice(prevAi.Addrs), addr.Slice(newAi.Addrs)).Addrs()
seen := make(map[string]struct{}, len(prevAi.Addrs))
combinedAddrs := make([]ma.Multiaddr, 0, len(prevAi.Addrs))
addAddrs := func(addrs []ma.Multiaddr) {
for _, addr := range addrs {
if _, ok := seen[addr.String()]; ok {
continue
}
seen[addr.String()] = struct{}{}
combinedAddrs = append(combinedAddrs, addr)
}
}
addAddrs(prevAi.Addrs)
addAddrs(newAi.Addrs)

if len(combinedAddrs) > len(prevAi.Addrs) {
combinedAi := &peer.AddrInfo{ID: prevAi.ID, Addrs: combinedAddrs}
return combinedAi
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/ipfs/go-log/v2 v2.4.0
github.com/libp2p/go-libp2p-blankhost v0.2.0
github.com/libp2p/go-libp2p-core v0.11.0
github.com/libp2p/go-libp2p-peerstore v0.4.0
github.com/libp2p/go-libp2p-swarm v0.8.0
github.com/multiformats/go-multiaddr v0.4.0
github.com/multiformats/go-multihash v0.0.15
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)
Expand Down

0 comments on commit 80f7185

Please sign in to comment.