Skip to content

Commit

Permalink
Change Process interface into object variable
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: rht <[email protected]>
  • Loading branch information
rht committed Jun 18, 2015
1 parent ef365be commit dfa843c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
29 changes: 17 additions & 12 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type IpfsNode struct {

IpnsFs *ipnsfs.Filesystem

goprocess.Process
proc goprocess.Process

mode mode
}
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions routing/dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -367,7 +367,7 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) {
}
cancel()
}
case <-dht.Closing():
case <-dht.proc.Closing():
return
}
}
Expand Down
6 changes: 3 additions & 3 deletions routing/dht/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ProviderManager struct {
newprovs chan *addProv
getprovs chan *getProv
period time.Duration
goprocess.Process
proc goprocess.Process
}

type providerSet struct {
Expand All @@ -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
}
Expand Down

0 comments on commit dfa843c

Please sign in to comment.