Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort addresses in /id #878

Merged
merged 8 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,14 @@ func (c *Cluster) ID(ctx context.Context) *api.ID {
for _, addr := range c.host.Addrs() {
addrsSet[addr.String()] = struct{}{}
}

var addrSorted []string
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
for k := range addrsSet {
addrSorted = append(addrSorted, k)
}
sort.Strings(addrSorted)

for _, k := range addrSorted {
addr, _ := api.NewMultiaddr(k)
addrs = append(addrs, api.MustLibp2pMultiaddrJoin(addr, c.id))
}
Expand Down
3 changes: 3 additions & 0 deletions pstoremgr/pstoremgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

logging "github.com/ipfs/go-log"
"github.com/ipfs/ipfs-cluster/utils"
host "github.com/libp2p/go-libp2p-core/host"
net "github.com/libp2p/go-libp2p-core/network"
peer "github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -139,9 +140,11 @@ func (pm *Manager) filteredPeerAddrs(p peer.ID) []ma.Multiaddr {
}

if len(peerDNSAddrs) > 0 {
sort.Sort(utils.ByString(peerDNSAddrs))
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
return peerDNSAddrs
}

sort.Sort(utils.ByString(peerAddrs))
return peerAddrs
}

Expand Down
12 changes: 12 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package utils

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

// ByString can sort multiaddresses by its string
type ByString []ma.Multiaddr

func (m ByString) Len() int { return len(m) }
func (m ByString) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m ByString) Less(i, j int) bool { return m[i].String() < m[j].String() }