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:

committed by
Steven Allen

parent
e4cf66008f
commit
0e6f8d4cc1
@ -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
|
||||
}
|
||||
|
9
thirdparty/math2/math2.go
vendored
9
thirdparty/math2/math2.go
vendored
@ -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
|
||||
}
|
Reference in New Issue
Block a user