refactor(core): isolate complex DHT initialization
Brian Tiger Chow committed
Jan 10, 2015 at 21:09 UTC
ca8190a889322c287694b8fc628c50116f0f258d
1 file changed
+14
-5
core/core.go
+14
-5
@@ -8,6 +8,7 @@ import (
8
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
10
ctxgroup "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
11
+ datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
12
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
13
14
bstore "github.com/jbenet/go-ipfs/blocks/blockstore"
@@ -201,11 +202,12 @@ func (n *IpfsNode) StartOnlineServices() error {
202
n.Diagnostics = diag.NewDiagnostics(n.Identity, n.PeerHost)
203
204
// setup routing service
204
- dhtRouting := dht.NewDHT(ctx, n.PeerHost, n.Datastore)
205
- dhtRouting.Validators[IpnsValidatorTag] = namesys.ValidateIpnsRecord
206
- n.Routing = dhtRouting
205
+ dhtRouting, err := constructDHTRouting(ctx, n.ContextGroup, n.PeerHost, n.Datastore)
206
+ if err != nil {
207
+ return debugerror.Wrap(err)
208
+ }
209
n.DHT = dhtRouting
208
- n.AddChildGroup(dhtRouting)
210
+ n.Routing = dhtRouting
211
212
// setup exchange service
213
const alwaysSendToPeer = true // use YesManStrategy
@@ -232,7 +234,7 @@ func (n *IpfsNode) StartOnlineServices() error {
234
}
235
bootstrapPeers = append(bootstrapPeers, p)
236
}
235
- go superviseConnections(ctx, n.PeerHost, dhtRouting, n.Peerstore, bootstrapPeers)
237
+ go superviseConnections(ctx, n.PeerHost, n.DHT, n.Peerstore, bootstrapPeers)
238
return nil
239
}
240
@@ -381,3 +383,10 @@ func constructPeerHost(ctx context.Context, ctxg ctxgroup.ContextGroup, cfg *con
383
ps.AddAddresses(id, addrs)
384
return peerhost, nil
385
}
386
+
387
+func constructDHTRouting(ctx context.Context, ctxg ctxgroup.ContextGroup, host p2phost.Host, ds datastore.ThreadSafeDatastore) (*dht.IpfsDHT, error) {
388
+ dhtRouting := dht.NewDHT(ctx, host, ds)
389
+ dhtRouting.Validators[IpnsValidatorTag] = namesys.ValidateIpnsRecord
390
+ ctxg.AddChildGroup(dhtRouting)
391
+ return dhtRouting, nil
392
+}