Skip to content

Commit

Permalink
Merge pull request #436 from jbenet/feat/context-clues
Browse files Browse the repository at this point in the history
add UUID to root context
  • Loading branch information
jbenet committed Dec 9, 2014
2 parents 11df4d5 + fb45e7b commit beb70c9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
5 changes: 4 additions & 1 deletion cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path"
"path/filepath"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
cmds "github.com/jbenet/go-ipfs/commands"
config "github.com/jbenet/go-ipfs/config"
core "github.com/jbenet/go-ipfs/core"
Expand Down Expand Up @@ -118,11 +119,13 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
// minted node. On success, it calls onSuccess
func addTheWelcomeFile(conf *config.Config) error {
// TODO extract this file creation operation into a function
nd, err := core.NewIpfsNode(conf, false)
ctx, cancel := context.WithCancel(context.Background())
nd, err := core.NewIpfsNode(ctx, conf, false)
if err != nil {
return err
}
defer nd.Close()
defer cancel()

// Set up default file
reader := bytes.NewBufferString(welcomeMsg)
Expand Down
48 changes: 25 additions & 23 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type cmdInvocation struct {
func main() {
rand.Seed(time.Now().UnixNano())
runtime.GOMAXPROCS(3) // FIXME rm arbitrary choice for n
ctx := context.Background()
ctx := eventlog.ContextWithLoggable(context.Background(), eventlog.Uuid("session"))
var err error
var invoc cmdInvocation
defer invoc.close()
Expand All @@ -82,7 +82,7 @@ func main() {
}

// parse the commandline into a command invocation
parseErr := invoc.Parse(os.Args[1:])
parseErr := invoc.Parse(ctx, os.Args[1:])

// BEFORE handling the parse error, if we have enough information
// AND the user requested help, print it out and exit
Expand Down Expand Up @@ -172,25 +172,27 @@ func (i *cmdInvocation) Run(ctx context.Context) (output io.Reader, err error) {
return res.Reader()
}

func (i *cmdInvocation) constructNode() (*core.IpfsNode, error) {
if i.req == nil {
return nil, errors.New("constructing node without a request")
}
func (i *cmdInvocation) constructNodeFunc(ctx context.Context) func() (*core.IpfsNode, error) {
return func() (*core.IpfsNode, error) {
if i.req == nil {
return nil, errors.New("constructing node without a request")
}

ctx := i.req.Context()
if ctx == nil {
return nil, errors.New("constructing node without a request context")
}
cmdctx := i.req.Context()
if cmdctx == nil {
return nil, errors.New("constructing node without a request context")
}

cfg, err := ctx.GetConfig()
if err != nil {
return nil, fmt.Errorf("constructing node without a config: %s", err)
}
cfg, err := cmdctx.GetConfig()
if err != nil {
return nil, fmt.Errorf("constructing node without a config: %s", err)
}

// ok everything is good. set it on the invocation (for ownership)
// and return it.
i.node, err = core.NewIpfsNode(cfg, ctx.Online)
return i.node, err
// ok everything is good. set it on the invocation (for ownership)
// and return it.
i.node, err = core.NewIpfsNode(ctx, cfg, cmdctx.Online)
return i.node, err
}
}

func (i *cmdInvocation) close() {
Expand All @@ -203,7 +205,7 @@ func (i *cmdInvocation) close() {
}
}

func (i *cmdInvocation) Parse(args []string) error {
func (i *cmdInvocation) Parse(ctx context.Context, args []string) error {
var err error

i.req, i.cmd, i.path, err = cmdsCli.Parse(args, os.Stdin, Root)
Expand All @@ -218,12 +220,12 @@ func (i *cmdInvocation) Parse(args []string) error {
log.Debugf("config path is %s", configPath)

// this sets up the function that will initialize the config lazily.
ctx := i.req.Context()
ctx.ConfigRoot = configPath
ctx.LoadConfig = loadConfig
cmdctx := i.req.Context()
cmdctx.ConfigRoot = configPath
cmdctx.LoadConfig = loadConfig
// this sets up the function that will initialize the node
// this is so that we can construct the node lazily.
ctx.ConstructNode = i.constructNode
cmdctx.ConstructNode = i.constructNodeFunc(ctx)

// if no encoding was specified by user, default to plaintext encoding
// (if command doesn't support plaintext, use JSON instead)
Expand Down
6 changes: 2 additions & 4 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Mounts struct {
}

// NewIpfsNode constructs a new IpfsNode based on the given config.
func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
func NewIpfsNode(ctx context.Context, cfg *config.Config, online bool) (n *IpfsNode, err error) {
success := false // flip to true after all sub-system inits succeed
defer func() {
if !success && n != nil {
Expand All @@ -110,14 +110,12 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
return nil, debugerror.Errorf("configuration required")
}

// derive this from a higher context.
ctx := context.TODO()
n = &IpfsNode{
onlineMode: online,
Config: cfg,
}
n.ContextCloser = ctxc.NewContextCloser(ctx, n.teardown)
ctx = n.Context()
ctx = n.ContextCloser.Context()

// setup datastore.
if n.Datastore, err = makeDatastore(cfg.Datastore); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (

config "github.com/jbenet/go-ipfs/config"
"github.com/jbenet/go-ipfs/peer"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)

func TestInitialization(t *testing.T) {
ctx := context.TODO()
id := testIdentity

good := []*config.Config{
Expand Down Expand Up @@ -44,14 +46,14 @@ func TestInitialization(t *testing.T) {
}

for i, c := range good {
n, err := NewIpfsNode(c, false)
n, err := NewIpfsNode(ctx, c, false)
if n == nil || err != nil {
t.Error("Should have constructed.", i, err)
}
}

for i, c := range bad {
n, err := NewIpfsNode(c, false)
n, err := NewIpfsNode(ctx, c, false)
if n != nil || err == nil {
t.Error("Should have failed to construct.", i)
}
Expand Down
2 changes: 1 addition & 1 deletion net/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (m *message) Data() []byte {
func (m *message) Loggable() map[string]interface{} {
return map[string]interface{}{
"netMessage": map[string]interface{}{
"recipient": m.Peer(),
"recipient": m.Peer().Loggable(),
// TODO sizeBytes? bytes? lenBytes?
"size": len(m.Data()),
},
Expand Down

0 comments on commit beb70c9

Please sign in to comment.