mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 02:47:48 +08:00
start threading context through the system
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
parent
11df4d5b80
commit
c2c95d1105
@ -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"
|
||||
@ -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)
|
||||
|
||||
@ -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()
|
||||
@ -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
|
||||
@ -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) nodeFunc(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() {
|
||||
@ -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)
|
||||
@ -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.nodeFunc(ctx)
|
||||
|
||||
// if no encoding was specified by user, default to plaintext encoding
|
||||
// (if command doesn't support plaintext, use JSON instead)
|
||||
|
||||
@ -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 {
|
||||
@ -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 {
|
||||
|
||||
@ -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{
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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()),
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user