-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Make sure ctx in commands are derived from req.Context #1585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@whyrusleeping there are still several left, pending. And how to add a context to https://github.com/ipfs/go-ipfs/blob/master/core/coreunix/metadata_test.go#L52? |
Or keep the option that ipfsnode can be initialized without ctx. and modify (n *ipfsnode) Context() method to assign context.TODO() when n.ctx is nil |
@@ -29,22 +27,13 @@ func NewDirectory(dserv mdag.DAGService) *directoryBuilder { | |||
} | |||
|
|||
// AddChild adds a (name, key)-pair to the root node. | |||
func (d *directoryBuilder) AddChild(name string, k key.Key) error { | |||
// TODO(cryptix): consolidate context managment | |||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no sure if WithTimeout
should be removed.
In other places sometimes it is 30 seconds or a minute, or no WithTimeout
at all.
I there must be WithTimeout
, then this should be inside dserv.Get
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, most of the places we do this (context.TODO with a timeout) should be replaced with a passed in context. and the caller can decide the timeout if they want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to removing this timeout.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove or replace with WithCancel
?
One thing to note, we should try to avoid node.Context() if we can, as its the 'global' context. We should prefer a finer grained context when we can get it. |
though for now, node.Context() is more fine grained than context.tODO() |
for sure! just wanted to make a note of that. |
@@ -460,7 +460,7 @@ func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, er | |||
var modified *mdag.Node | |||
ndata := new(ft.FSNode) | |||
for i, lnk := range nd.Links { | |||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) | |||
_ctx, cancel := context.WithTimeout(ctx, time.Minute) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this timeout one is one to remove?
We'd still want a context derived from the one passed in, so things can be cancelled when this func returns
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetNode
basically calls DAG.Get
, so WithCancel
can be done inside DAG.Get
too.
(ok but most are used for func-specific context)
thanks for this PR, much needed cleanup |
|
93fd7f0
to
7ecbdbd
Compare
The remaining cleanup TODO has larger depths, and requires review:
Won't touch:
|
|
Thanks for doing all this, its really good stuff :) |
4 dagbuilderhelper doesn't have ctx by default. Should it have a ctx field? |
@rht hrm.. i'm torn on this one. The only time a context is used is in a situtation where we are expected to already have the node local. We could wire a context down through there, but i dont think that its going to be worthwhile (i could be wrong though). What we could do is change the |
Or just add ctx explicitly as an extra argument, here the ctx flow is: |
@whyrusleeping how about this? Though most of the buried down, long context pipes are constructed because of the need to call dagservice GetNode / GetDAG. It is possibly much cleaner to hide the pipes inside dagservice? (will check if this is the case) |
why it is worthwhile: to make sure there are no 1-min zombie dagservices when an IpfsNode closes. |
This could be fixed with replacing L120 with Edit: i c, this natmgr.go#L147 is used for eventlog only, so it's fine. |
if err == nil { ready to merge } |
LGTM |
License: MIT Signed-off-by: rht <[email protected]>
License: MIT Signed-off-by: rht <[email protected]>
License: MIT Signed-off-by: rht <[email protected]>
License: MIT Signed-off-by: rht <[email protected]>
I think this should be done in separate PR. Not every dag.Get caller waits only on this function to finish. |
License: MIT Signed-off-by: rht <[email protected]>
License: MIT Signed-off-by: rht <[email protected]>
Added here anyway. I only made the change here when it is unambiguous that having a This also "auto-fix" parts of the code where there is no |
Instead put it inside of DAG.Get. The fix is applied only in the case when the context.WithCancel before a DAG.Get is also used later on in the scope. License: MIT Signed-off-by: rht <[email protected]>
yeah. |
@@ -60,7 +60,8 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa | |||
|
|||
log.Debugf("Storing pubkey at: %s", namekey) | |||
// Store associated public key | |||
timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*10)) | |||
timectx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Second*10)) | |||
defer cancel() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- we can probably make this a constant too (and possibly even remove it??)
- maybe a future PR can go over all the timeouts and make sure they make sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better in another PR. This PR is specifically to make sure all context trees are linked to the root caller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good 👍
Ok, this LGTM. the only remaining things:
maybe let's make all further refactor/cleanup happen on top of 0.4.0 |
thanks @rht , this will improve codebase (+ perf for users in slow networkes!) a lot! |
in what way? |
requests wont timeout prematurely |
if slow means high latency: lunar RTT is about 2.5s, mars RTT is about 500s-2500s. |
gpe (on rht/go-ipfs) doesn't run the test unless it is done on ipfs/go-ipfs itself. |
@rht you have push access to this repo directly. you can push branches here |
|
Ok, sounds good! thanks @rht |
Make sure ctx in commands are derived from req.Context
Make sure ctx in commands are derived from req.Context This commit was moved from ipfs/kubo@a965316
Fixes #505