Skip to content
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

core: add context.Context param to core.Resolve() #1189

Merged
merged 1 commit into from
May 8, 2015

Conversation

cryptix
Copy link
Contributor

@cryptix cryptix commented May 3, 2015

Following the discussion of #1150, we need to be able to pass a context to core.Resolve() so that a user is able to stop it.

@jbenet jbenet added the status/in-progress In progress label May 3, 2015
return r.resolveRecurse(0)
}

type resolver struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just added this type to make the recursive call easier to grasp.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@cryptix cryptix mentioned this pull request May 3, 2015
@@ -89,7 +91,8 @@ Publish an <ipfs-path> to another public key (not implemented):
}

// TODO n.Keychain.Get(name).PrivKey
output, err := publish(n, n.PrivateKey, p)
// TODO(cryptix): is req.Context().Context a child of n.Context()?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the top level context stuff around here is really confusing.. it uses this context group thing if i remember correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbenet can you tell us?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apparently req.Context().Context() is not yet a child of n.Context(): https://github.com/ipfs/go-ipfs/blob/master/commands/http/handler.go#L52-L54 -- it should be.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and yes it's very confusing :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbenet ohai cmdlib 😞 .. but fixed 😄. Do I need to mirror this change on the offline mode, too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cryptix

Do I need to mirror this change on the offline mode, too?

probably? i can think about this more carefully tomorrow.

TODO(cryptix): we wrapped a new context for each name iteration
is this correct?
moved it out of the loop for now
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this used to be right. we should probably give longer names more time. example: if resolution takes ~10s per hop, 1min is not going to cut it for paths larger than 6. (resolution should be much faster, sure, but remember that resolution might go across the routing system).

in the end, this probably should come from the context coming in, and not have a timeout internally. (but in that case we must make sure all source calls do have proper timeouts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Changed it back.

@jbenet
Copy link
Member

jbenet commented May 3, 2015

This PR is fighting the good fight :) 👍

Some notes (not for this PR, but for when making the core/shell) api:

  • More of these functions should take their own contexts.
  • should be cancellation works when either (a) the request is cancelled, (b) the node itself is cancelled/stopped ("multiple parents" -- https://godoc.org/github.com/jbenet/go-context/ext#WithParents)
  • In many cases, the addition of the context "pollutes" the api, so can have parallel calls[1]

[1] since contexts are pervasive and we'd like to be able to have them in many function calls, we'd like to be able to add them all over to expose the ability to cancel things. But this does pollute the API all over the place, and make it harder to glean for the first time user. One way is to have a package that mirrors the functions without the ctx, for use in examples, and very simple use cases, and so on:

// (simplified)

// in the standard package
function Resolve(ctx context.Context, p path.Path) (*merkledag.Node, error) { ... }

// in a separate (simplifying) package
function Resolve(p path.Path) (*merkledag.Node, error) {
  ctx, cancel := context.WithCancel(n.Context())
  defer cancel()
  return core.Resolve(ctx, p)
}

@whyrusleeping
Copy link
Member

One way is to have a package that mirrors the functions without the ctx

I'm not a fan of expanding the surface area of our exported API. Ideally, we have as few top level functions as possible, to reduce the mental load on users

@cryptix
Copy link
Contributor Author

cryptix commented May 4, 2015

Addressed comments and rebased


I assume it really isn't because ServeHTTP() doesn't take a pointer receiver, but it's really subtule..

Shouldn't the context be just put on the command request?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is super sketchy. I think its probably best to put the context into the request object instead of here on the handler context

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, it is put into the request object, but it's put here: line 104. maybe we can find another place to put it, earlier? (nbd for me -- i want to rewire how all this works already)

@whyrusleeping
Copy link
Member

Looks good to me! its a bit cleaner now too, good job

@jbenet
Copy link
Member

jbenet commented May 4, 2015

@whyrusleeping

I'm not a fan of expanding the surface area of our exported API. Ideally, we have as few top level functions as possible, to reduce the mental load on users

The problem is that many times the type system and many knobs to use are a mental load on users already. Understanding + using contexts correctly is certainly a very big one.

And, it's a separate package, only there if you need it. It's not a bigger surface area on the same package-- it's tucked away. The functions aren't different-- it's a tweaked API for the same thing. Anyway, even if it doesn't go into go-ipfs, i'd make this a package and publish it somewhere.

If modules are very simple, and stable-- more modules isn't bad. Bigger modules with lots of semantics are painful.

@jbenet
Copy link
Member

jbenet commented May 4, 2015

This LGTM.

@jbenet
Copy link
Member

jbenet commented May 4, 2015

Didn't pass on any osx-- dont seem related but running again just in case.

commands/object: remove objectData() and objectLinks() helpers
resolver: added context parameters
sharness: $HASH carried the \r from the http protocol with
sharness: write curl output to individual files
http gw: break PUT handler until PR#1191
@jbenet
Copy link
Member

jbenet commented May 8, 2015

@cryptix 👏 👏 👏 👏 👏 👏 very clean PR!

jbenet added a commit that referenced this pull request May 8, 2015
core: add context.Context param to core.Resolve()
@jbenet jbenet merged commit cd37b67 into master May 8, 2015
@jbenet jbenet deleted the refactor/coreResolve branch May 8, 2015 05:33
@jbenet jbenet removed the status/in-progress In progress label May 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants