@cryptotaxi247 / kubo / commits / 7b5eff5ce

feat(bootstrap) consider active cxns and connect to subset

* only bootstrap if the number of active connections falls below a given threshold * when bootstrapping, connect to a subset of peers License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Dec 8, 2014 at 16:07 UTC 7b5eff5cef212f73b8322744eed12417c16bc7a4
1 file changed +24 -10
core/bootstrap.go
+24 -10
@@ -1,19 +1,23 @@
1 package core
2
3 import (
4 + "math/rand"
5 "sync"
6 "time"
7
8 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9 - "github.com/jbenet/go-ipfs/config"
10 + config "github.com/jbenet/go-ipfs/config"
11 inet "github.com/jbenet/go-ipfs/net"
11 - "github.com/jbenet/go-ipfs/peer"
12 - "github.com/jbenet/go-ipfs/routing/dht"
12 + peer "github.com/jbenet/go-ipfs/peer"
13 + dht "github.com/jbenet/go-ipfs/routing/dht"
14 )
15
15 -const period time.Duration = 30 * time.Second
16 -const timeout time.Duration = period / 3
16 +const (
17 + period = 30 * time.Second // how often to check connection status
18 + connectiontimeout time.Duration = period / 3 // duration to wait when attempting to connect
19 + recoveryThreshold = 4 // attempt to bootstrap if connection count falls below this value
20 +)
21
22 func superviseConnections(parent context.Context,
23 n *IpfsNode,
@@ -22,7 +26,7 @@ func superviseConnections(parent context.Context,
26 peers []*config.BootstrapPeer) error {
27
28 for {
25 - ctx, _ := context.WithTimeout(parent, timeout)
29 + ctx, _ := context.WithTimeout(parent, connectiontimeout)
30 // TODO get config from disk so |peers| always reflects the latest
31 // information
32 if err := bootstrap(ctx, n.Network, route, store, peers); err != nil {
@@ -43,17 +47,22 @@ func bootstrap(ctx context.Context,
47 ps peer.Peerstore,
48 boots []*config.BootstrapPeer) error {
49
46 - var peers []peer.Peer
50 + if len(n.GetConnections()) >= recoveryThreshold {
51 + return nil
52 + }
53 + numCxnsToCreate := recoveryThreshold - len(n.GetConnections())
54 +
55 + var bootstrapPeers []peer.Peer
56 for _, bootstrap := range boots {
57 p, err := toPeer(ps, bootstrap)
58 if err != nil {
59 return err
60 }
52 - peers = append(peers, p)
61 + bootstrapPeers = append(bootstrapPeers, p)
62 }
63
64 var notConnected []peer.Peer
56 - for _, p := range peers {
65 + for _, p := range bootstrapPeers {
66 if !n.IsConnected(p) {
67 notConnected = append(notConnected, p)
68 }
@@ -61,7 +70,12 @@ func bootstrap(ctx context.Context,
70 for _, p := range notConnected {
71 log.Infof("not connected to %v", p)
72 }
64 - if err := connect(ctx, r, notConnected); err != nil {
73 +
74 + var randomSubset []peer.Peer
75 + for _, val := range rand.Perm(numCxnsToCreate) {
76 + randomSubset = append(randomSubset, notConnected[val])
77 + }
78 + if err := connect(ctx, r, randomSubset); err != nil {
79 return err
80 }
81 return nil