bootstrap: cleanup randomSubsetOfPeers
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Apr 8, 2019 at 15:36 UTC
0e6f8d4cc19ef5bb5d9c47e357a3ffb862b8b067
2 files changed
+7
-18
core/bootstrap/bootstrap.go
+7
-9
@@ -20,8 +20,6 @@ import (
20
"github.com/libp2p/go-libp2p-peer"
21
"github.com/libp2p/go-libp2p-peerstore"
22
"github.com/libp2p/go-libp2p-routing"
23
-
24
- "github.com/ipfs/go-ipfs/thirdparty/math2"
23
)
24
25
var log = logging.Logger("bootstrap")
@@ -208,13 +206,13 @@ func bootstrapConnect(ctx context.Context, ph host.Host, peers []peerstore.PeerI
206
}
207
208
func randomSubsetOfPeers(in []peerstore.PeerInfo, max int) []peerstore.PeerInfo {
211
- n := math2.IntMin(max, len(in))
212
- var out []peerstore.PeerInfo
213
- for _, val := range rand.Perm(len(in)) {
214
- out = append(out, in[val])
215
- if len(out) >= n {
216
- break
217
- }
209
+ if max > len(in) {
210
+ max = len(in)
211
+ }
212
+
213
+ out := make([]peerstore.PeerInfo, max)
214
+ for i, val := range rand.Perm(len(in))[:max] {
215
+ out[i] = in[val]
216
}
217
return out
218
}
thirdparty/math2/math2.go
deleted
-9
@@ -1,9 +0,0 @@
1
-package math2
2
-
3
-// IntMin returns the smaller of x or y.
4
-func IntMin(x, y int) int {
5
- if x < y {
6
- return x
7
- }
8
- return y
9
-}