提交 4b5e0291 作者: Juan Batiz-Benet

core context + cancels

Erroring out in core setup should cancel the context
to ensure subsystems are shut down. This has to happen
all over the place we use contexts.

@perfmode @whyrusleeping
上级 f2db4b77
...@@ -65,12 +65,18 @@ type IpfsNode struct { ...@@ -65,12 +65,18 @@ type IpfsNode struct {
// NewIpfsNode constructs a new IpfsNode based on the given config. // NewIpfsNode constructs a new IpfsNode based on the given config.
func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
// derive this from a higher context.
// cancel if we need to fail early.
ctx, cancel := context.WithCancel(context.TODO())
if cfg == nil { if cfg == nil {
cancel()
return nil, fmt.Errorf("configuration required") return nil, fmt.Errorf("configuration required")
} }
d, err := makeDatastore(cfg.Datastore) d, err := makeDatastore(cfg.Datastore)
if err != nil { if err != nil {
cancel()
return nil, err return nil, err
} }
...@@ -89,8 +95,6 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { ...@@ -89,8 +95,6 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
) )
if online { if online {
// add protocol services here.
ctx := context.TODO() // derive this from a higher context.
// when not online, don't need to parse private keys (yet) // when not online, don't need to parse private keys (yet)
local, err := initIdentity(cfg) local, err := initIdentity(cfg)
...@@ -103,17 +107,21 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { ...@@ -103,17 +107,21 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
exchangeService := netservice.NewService(nil) // nil handler for now, need to patch it exchangeService := netservice.NewService(nil) // nil handler for now, need to patch it
if err := dhtService.Start(ctx); err != nil { if err := dhtService.Start(ctx); err != nil {
cancel()
return nil, err return nil, err
} }
if err := exchangeService.Start(ctx); err != nil { if err := exchangeService.Start(ctx); err != nil {
cancel()
return nil, err return nil, err
} }
net, err = inet.NewIpfsNetwork(context.TODO(), local, peerstore, &mux.ProtocolMap{ net, err = inet.NewIpfsNetwork(context.TODO(), local, peerstore, &mux.ProtocolMap{
mux.ProtocolID_Routing: dhtService, mux.ProtocolID_Routing: dhtService,
mux.ProtocolID_Exchange: exchangeService, mux.ProtocolID_Exchange: exchangeService,
// add protocol services here.
}) })
if err != nil { if err != nil {
cancel()
return nil, err return nil, err
} }
...@@ -132,6 +140,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { ...@@ -132,6 +140,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
// session that simply doesn't return blocks // session that simply doesn't return blocks
bs, err := bserv.NewBlockService(d, exchangeSession) bs, err := bserv.NewBlockService(d, exchangeSession)
if err != nil { if err != nil {
cancel()
return nil, err return nil, err
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论