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
Juan Batiz-Benet committed
Sep 23, 2014 at 05:03 UTC
4b5e0291f89db5ea1f61f5e3e41b36cd309ae1d5
1 file changed
+11
-2
core/core.go
+11
-2
@@ -65,12 +65,18 @@ type IpfsNode struct {
65
66
// NewIpfsNode constructs a new IpfsNode based on the given config.
67
func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
68
+ // derive this from a higher context.
69
+ // cancel if we need to fail early.
70
+ ctx, cancel := context.WithCancel(context.TODO())
71
+
72
if cfg == nil {
73
+ cancel()
74
return nil, fmt.Errorf("configuration required")
75
}
76
77
d, err := makeDatastore(cfg.Datastore)
78
if err != nil {
79
+ cancel()
80
return nil, err
81
}
82
@@ -89,8 +95,6 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
95
)
96
97
if online {
92
- // add protocol services here.
93
- ctx := context.TODO() // derive this from a higher context.
98
99
// when not online, don't need to parse private keys (yet)
100
local, err := initIdentity(cfg)
@@ -103,17 +107,21 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
107
exchangeService := netservice.NewService(nil) // nil handler for now, need to patch it
108
109
if err := dhtService.Start(ctx); err != nil {
110
+ cancel()
111
return nil, err
112
}
113
if err := exchangeService.Start(ctx); err != nil {
114
+ cancel()
115
return nil, err
116
}
117
118
net, err = inet.NewIpfsNetwork(context.TODO(), local, peerstore, &mux.ProtocolMap{
119
mux.ProtocolID_Routing: dhtService,
120
mux.ProtocolID_Exchange: exchangeService,
121
+ // add protocol services here.
122
})
123
if err != nil {
124
+ cancel()
125
return nil, err
126
}
127
@@ -132,6 +140,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
140
// session that simply doesn't return blocks
141
bs, err := bserv.NewBlockService(d, exchangeSession)
142
if err != nil {
143
+ cancel()
144
return nil, err
145
}
146