Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
c2c95d11
Commit
c2c95d11
authored
Dec 09, 2014
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
start threading context through the system
License: MIT Signed-off-by:
Brian Tiger Chow
<
brian@perfmode.com
>
parent
11df4d5b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
36 additions
and
31 deletions
+36
-31
cmd/ipfs/init.go
cmd/ipfs/init.go
+4
-1
cmd/ipfs/main.go
cmd/ipfs/main.go
+25
-23
core/core.go
core/core.go
+2
-4
core/core_test.go
core/core_test.go
+4
-2
net/message/message.go
net/message/message.go
+1
-1
No files found.
cmd/ipfs/init.go
View file @
c2c95d11
...
...
@@ -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
)
...
...
cmd/ipfs/main.go
View file @
c2c95d11
...
...
@@ -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"
)
}
cmd
ctx
:=
i
.
req
.
Context
()
if
cmd
ctx
==
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
:=
cmd
ctx
.
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
cmd
ctx
:=
i
.
req
.
Context
()
cmd
ctx
.
ConfigRoot
=
configPath
cmd
ctx
.
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
cmd
ctx
.
ConstructNode
=
i
.
nodeFunc
(
ctx
)
// if no encoding was specified by user, default to plaintext encoding
// (if command doesn't support plaintext, use JSON instead)
...
...
core/core.go
View file @
c2c95d11
...
...
@@ -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
{
...
...
core/core_test.go
View file @
c2c95d11
...
...
@@ -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
)
}
...
...
net/message/message.go
View file @
c2c95d11
...
...
@@ -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
()),
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment