Sort addresses in /id
and thus in GET `/peers` as well
This commit is contained in:
parent
00e78a6b6d
commit
ee6a35d9b5
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
12
utils/utils.go
Normal 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() }
|
Loading…
Reference in New Issue
Block a user