From dfa843ca20864d6e8cdac9d649d4d146895d9708 Mon Sep 17 00:00:00 2001 From: rht Date: Thu, 18 Jun 2015 17:17:38 +0700 Subject: [PATCH] Change Process interface into object variable License: MIT Signed-off-by: rht --- core/core.go | 29 +++++++++++++++++------------ core/mock/mock.go | 2 +- routing/dht/dht.go | 12 ++++++------ routing/dht/providers.go | 6 +++--- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/core/core.go b/core/core.go index 5aa4639293cf..a63c58520262 100644 --- a/core/core.go +++ b/core/core.go @@ -105,7 +105,7 @@ type IpfsNode struct { IpnsFs *ipnsfs.Filesystem - goprocess.Process + proc goprocess.Process mode mode } @@ -120,23 +120,23 @@ type Mounts struct { type ConfigOption func(ctx context.Context) (*IpfsNode, error) -func NewIPFSNode(parent context.Context, option ConfigOption) (*IpfsNode, error) { - procctx := goprocessctx.WithContext(parent) - ctx := parent +func NewIPFSNode(ctx context.Context, option ConfigOption) (*IpfsNode, error) { + node, err := option(ctx) + if err != nil { + return nil, err + } + + proc := goprocessctx.WithContext(ctx) + proc.SetTeardown(node.teardown) + node.proc = proc + success := false // flip to true after all sub-system inits succeed defer func() { if !success { - procctx.Close() + proc.Close() } }() - node, err := option(ctx) - if err != nil { - return nil, err - } - node.Process = procctx - ctxg.SetTeardown(node.teardown) - // Need to make sure it's perfectly clear 1) which variables are expected // to be initialized at this point, and 2) which variables will be // initialized after this point. @@ -334,6 +334,11 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost return nil } +// Process returns the Process object +func (n *IpfsNode) Process() goprocess.Process { + return n.proc +} + // teardown closes owned children. If any errors occur, this function returns // the first error. func (n *IpfsNode) teardown() error { diff --git a/core/mock/mock.go b/core/mock/mock.go index 42b225eabfc2..50a83ed526f8 100644 --- a/core/mock/mock.go +++ b/core/mock/mock.go @@ -42,7 +42,7 @@ func NewMockNode() (*core.IpfsNode, error) { nd.Peerstore = peer.NewPeerstore() nd.Peerstore.AddPrivKey(p, ident.PrivateKey()) nd.Peerstore.AddPubKey(p, ident.PublicKey()) - nd.Process = goprocessctx.WithContext(ctx) + nd.Process() = goprocessctx.WithContext(ctx) nd.PeerHost, err = mocknet.New(ctx).AddPeer(ident.PrivateKey(), ident.Address()) // effectively offline if err != nil { diff --git a/routing/dht/dht.go b/routing/dht/dht.go index fcc14273f839..7da6a42fc33e 100644 --- a/routing/dht/dht.go +++ b/routing/dht/dht.go @@ -59,7 +59,7 @@ type IpfsDHT struct { Validator record.Validator // record validator funcs Context context.Context - goprocess.Process + proc goprocess.Process } // NewDHT creates a new DHT object with the given peer as the 'local' host @@ -73,13 +73,13 @@ func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *Ip // register for network notifs. dht.host.Network().Notify((*netNotifiee)(dht)) - procctx = goprocessctx.WithContext(ctx) - procctx.SetTeardown(func() error { + proc = goprocessctx.WithContext(ctx) + proc.SetTeardown(func() error { // remove ourselves from network notifs. dht.host.Network().StopNotify((*netNotifiee)(dht)) return nil }) - dht.Process = procctx + dht.proc = proc dht.Context = ctx h.SetStreamHandler(ProtocolDHT, dht.handleNewStream) @@ -93,7 +93,7 @@ func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *Ip dht.Validator["pk"] = record.PublicKeyValidator if doPinging { - dht.Go(func() { dht.PingRoutine(time.Second * 10) }) + dht.proc.Go(func() { dht.PingRoutine(time.Second * 10) }) } return dht } @@ -367,7 +367,7 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) { } cancel() } - case <-dht.Closing(): + case <-dht.proc.Closing(): return } } diff --git a/routing/dht/providers.go b/routing/dht/providers.go index 46675604a066..ce337a4f3341 100644 --- a/routing/dht/providers.go +++ b/routing/dht/providers.go @@ -22,7 +22,7 @@ type ProviderManager struct { newprovs chan *addProv getprovs chan *getProv period time.Duration - goprocess.Process + proc goprocess.Process } type providerSet struct { @@ -47,8 +47,8 @@ func NewProviderManager(ctx context.Context, local peer.ID) *ProviderManager { pm.providers = make(map[key.Key]*providerSet) pm.getlocal = make(chan chan []key.Key) pm.local = make(map[key.Key]struct{}) - pm.Process = goprocessctx.WithContext(ctx) - pm.Go(pm.run) + pm.proc = goprocessctx.WithContext(ctx) + pm.proc.Go(pm.run) return pm }