@cryptotaxi247 / kubo / commits / c2c95d110

start threading context through the system

License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Dec 9, 2014 at 07:11 UTC c2c95d11052dd52fd65a7a35abd5b4e65693ecbe
5 files changed +36 -31
cmd/ipfs/init.go
+4 -1
@@ -8,6 +8,7 @@ import (
8 "path"
9 "path/filepath"
10
11 + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
12 cmds "github.com/jbenet/go-ipfs/commands"
13 config "github.com/jbenet/go-ipfs/config"
14 core "github.com/jbenet/go-ipfs/core"
@@ -118,11 +119,13 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
119 // minted node. On success, it calls onSuccess
120 func addTheWelcomeFile(conf *config.Config) error {
121 // TODO extract this file creation operation into a function
121 - nd, err := core.NewIpfsNode(conf, false)
122 + ctx, cancel := context.WithCancel(context.Background())
123 + nd, err := core.NewIpfsNode(ctx, conf, false)
124 if err != nil {
125 return err
126 }
127 defer nd.Close()
128 + defer cancel()
129
130 // Set up default file
131 reader := bytes.NewBufferString(welcomeMsg)
cmd/ipfs/main.go
+25 -23
@@ -59,7 +59,7 @@ type cmdInvocation struct {
59 func main() {
60 rand.Seed(time.Now().UnixNano())
61 runtime.GOMAXPROCS(3) // FIXME rm arbitrary choice for n
62 - ctx := context.Background()
62 + ctx := eventlog.ContextWithLoggable(context.Background(), eventlog.Uuid("session"))
63 var err error
64 var invoc cmdInvocation
65 defer invoc.close()
@@ -82,7 +82,7 @@ func main() {
82 }
83
84 // parse the commandline into a command invocation
85 - parseErr := invoc.Parse(os.Args[1:])
85 + parseErr := invoc.Parse(ctx, os.Args[1:])
86
87 // BEFORE handling the parse error, if we have enough information
88 // AND the user requested help, print it out and exit
@@ -172,25 +172,27 @@ func (i *cmdInvocation) Run(ctx context.Context) (output io.Reader, err error) {
172 return res.Reader()
173 }
174
175 -func (i *cmdInvocation) constructNode() (*core.IpfsNode, error) {
176 - if i.req == nil {
177 - return nil, errors.New("constructing node without a request")
178 - }
175 +func (i *cmdInvocation) nodeFunc(ctx context.Context) func() (*core.IpfsNode, error) {
176 + return func() (*core.IpfsNode, error) {
177 + if i.req == nil {
178 + return nil, errors.New("constructing node without a request")
179 + }
180
180 - ctx := i.req.Context()
181 - if ctx == nil {
182 - return nil, errors.New("constructing node without a request context")
183 - }
181 + cmdctx := i.req.Context()
182 + if cmdctx == nil {
183 + return nil, errors.New("constructing node without a request context")
184 + }
185
185 - cfg, err := ctx.GetConfig()
186 - if err != nil {
187 - return nil, fmt.Errorf("constructing node without a config: %s", err)
188 - }
186 + cfg, err := cmdctx.GetConfig()
187 + if err != nil {
188 + return nil, fmt.Errorf("constructing node without a config: %s", err)
189 + }
190
190 - // ok everything is good. set it on the invocation (for ownership)
191 - // and return it.
192 - i.node, err = core.NewIpfsNode(cfg, ctx.Online)
193 - return i.node, err
191 + // ok everything is good. set it on the invocation (for ownership)
192 + // and return it.
193 + i.node, err = core.NewIpfsNode(ctx, cfg, cmdctx.Online)
194 + return i.node, err
195 + }
196 }
197
198 func (i *cmdInvocation) close() {
@@ -203,7 +205,7 @@ func (i *cmdInvocation) close() {
205 }
206 }
207
206 -func (i *cmdInvocation) Parse(args []string) error {
208 +func (i *cmdInvocation) Parse(ctx context.Context, args []string) error {
209 var err error
210
211 i.req, i.cmd, i.path, err = cmdsCli.Parse(args, os.Stdin, Root)
@@ -218,12 +220,12 @@ func (i *cmdInvocation) Parse(args []string) error {
220 log.Debugf("config path is %s", configPath)
221
222 // this sets up the function that will initialize the config lazily.
221 - ctx := i.req.Context()
222 - ctx.ConfigRoot = configPath
223 - ctx.LoadConfig = loadConfig
223 + cmdctx := i.req.Context()
224 + cmdctx.ConfigRoot = configPath
225 + cmdctx.LoadConfig = loadConfig
226 // this sets up the function that will initialize the node
227 // this is so that we can construct the node lazily.
226 - ctx.ConstructNode = i.constructNode
228 + cmdctx.ConstructNode = i.nodeFunc(ctx)
229
230 // if no encoding was specified by user, default to plaintext encoding
231 // (if command doesn't support plaintext, use JSON instead)
core/core.go
+2 -4
@@ -98,7 +98,7 @@ type Mounts struct {
98 }
99
100 // NewIpfsNode constructs a new IpfsNode based on the given config.
101 -func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
101 +func NewIpfsNode(ctx context.Context, cfg *config.Config, online bool) (n *IpfsNode, err error) {
102 success := false // flip to true after all sub-system inits succeed
103 defer func() {
104 if !success && n != nil {
@@ -110,14 +110,12 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
110 return nil, debugerror.Errorf("configuration required")
111 }
112
113 - // derive this from a higher context.
114 - ctx := context.TODO()
113 n = &IpfsNode{
114 onlineMode: online,
115 Config: cfg,
116 }
117 n.ContextCloser = ctxc.NewContextCloser(ctx, n.teardown)
120 - ctx = n.Context()
118 + ctx = n.ContextCloser.Context()
119
120 // setup datastore.
121 if n.Datastore, err = makeDatastore(cfg.Datastore); err != nil {
core/core_test.go
+4 -2
@@ -5,9 +5,11 @@ import (
5
6 config "github.com/jbenet/go-ipfs/config"
7 "github.com/jbenet/go-ipfs/peer"
8 + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9 )
10
11 func TestInitialization(t *testing.T) {
12 + ctx := context.TODO()
13 id := testIdentity
14
15 good := []*config.Config{
@@ -44,14 +46,14 @@ func TestInitialization(t *testing.T) {
46 }
47
48 for i, c := range good {
47 - n, err := NewIpfsNode(c, false)
49 + n, err := NewIpfsNode(ctx, c, false)
50 if n == nil || err != nil {
51 t.Error("Should have constructed.", i, err)
52 }
53 }
54
55 for i, c := range bad {
54 - n, err := NewIpfsNode(c, false)
56 + n, err := NewIpfsNode(ctx, c, false)
57 if n != nil || err == nil {
58 t.Error("Should have failed to construct.", i)
59 }
net/message/message.go
+1 -1
@@ -39,7 +39,7 @@ func (m *message) Data() []byte {
39 func (m *message) Loggable() map[string]interface{} {
40 return map[string]interface{}{
41 "netMessage": map[string]interface{}{
42 - "recipient": m.Peer(),
42 + "recipient": m.Peer().Loggable(),
43 // TODO sizeBytes? bytes? lenBytes?
44 "size": len(m.Data()),
45 },