Sort addresses in /id

and thus in GET `/peers` as well
This commit is contained in:
Kishan Mohanbhai Sagathiya 2019-08-11 21:15:43 +05:30
parent 00e78a6b6d
commit ee6a35d9b5
3 changed files with 22 additions and 0 deletions

View File

@ -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
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))
}

View File

@ -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"
@ -139,9 +140,11 @@ func (pm *Manager) filteredPeerAddrs(p peer.ID) []ma.Multiaddr {
}
if len(peerDNSAddrs) > 0 {
sort.Sort(utils.ByString(peerDNSAddrs))
return peerDNSAddrs
}
sort.Sort(utils.ByString(peerAddrs))
return peerAddrs
}

12
utils/utils.go Normal file
View File

@ -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() }