1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-05-17 23:16:11 +08:00

bootstrap: cleanup randomSubsetOfPeers

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2019-04-08 15:36:25 +02:00
committed by Steven Allen
parent e4cf66008f
commit 0e6f8d4cc1
2 changed files with 7 additions and 18 deletions

View File

@ -20,8 +20,6 @@ import (
"github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p-routing"
"github.com/ipfs/go-ipfs/thirdparty/math2"
)
var log = logging.Logger("bootstrap")
@ -208,13 +206,13 @@ func bootstrapConnect(ctx context.Context, ph host.Host, peers []peerstore.PeerI
}
func randomSubsetOfPeers(in []peerstore.PeerInfo, max int) []peerstore.PeerInfo {
n := math2.IntMin(max, len(in))
var out []peerstore.PeerInfo
for _, val := range rand.Perm(len(in)) {
out = append(out, in[val])
if len(out) >= n {
break
}
if max > len(in) {
max = len(in)
}
out := make([]peerstore.PeerInfo, max)
for i, val := range rand.Perm(len(in))[:max] {
out[i] = in[val]
}
return out
}

View File

@ -1,9 +0,0 @@
package math2
// IntMin returns the smaller of x or y.
func IntMin(x, y int) int {
if x < y {
return x
}
return y
}