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

Refactor/http gateway #1191

Merged
merged 1 commit into from
May 11, 2015
Merged

Refactor/http gateway #1191

merged 1 commit into from
May 11, 2015

Conversation

cryptix
Copy link
Contributor

@cryptix cryptix commented May 3, 2015

This is supposed to fix #1150 and clean up the gateway http handler in general. Based on #1189.

This is currently WIP but I need some feedback first, see my TODOcomments in the code.

@jbenet jbenet added the status/in-progress In progress label May 3, 2015
@@ -344,39 +344,21 @@ Data should be in the format specified by the --inputenc flag.
}

// objectData takes a key string and writes out the raw bytes of that node (if there is one)
func objectData(n *core.IpfsNode, fpath path.Path) (io.Reader, error) {
dagnode, err := core.Resolve(n, fpath)
func objectData(n *dag.Node, err error) (io.Reader, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's so little left in this function after you remove the Resolve call that I'd just drop this (and objectLinks, etc.) entirely. Unless we really want to keep them around for the debug logging…

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 its probably worth dropping, it provides no tangible value added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thought so too, this is mixed up from PR#1189 though. I'll remove it there and rebase this once it is merged.

@wking
Copy link
Contributor

wking commented May 3, 2015

On Sat, May 02, 2015 at 08:07:34PM -0700, Henry wrote:

  • core: add context.Context param to core.Resolve()

So I've been thinking about this a bit, and I can't wrap my head
around Contexts. There are lots of calls in Go's standard libraries
that don't take Context calls. Are they all non-blocking? Where do
you draw the line for “we better accept a Context in case the caller
wants to bail”? Is this just for stuff that will eventually get wired
up to a web server where the caller could drop the TCP request? Maybe
we just want a goroutine per request and we can kill the whole
goroutine? Does Go do single-process event looping like Nginx workers
1 and Python's asyncio 2? Is there no more elegant way to stop
processing for a particular request without passing around Context
parameters?

On the other hand, I barely speak Go, so feel free to ignore my
confusion and just carry on writing idiomatic Go (whatever that
happens to be ;).

@@ -162,7 +116,7 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
errmsg = errmsg + "bad request for " + r.URL.Path
}
w.Write([]byte(errmsg))
log.Debug(errmsg)
log.Error(errmsg) // TODO(cryptix): Why are we ignoring handler errors?
Copy link
Member

Choose a reason for hiding this comment

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

we should never ignore errors. any time an error is ignored anywhere in our code, its a bug.

Copy link
Member

Choose a reason for hiding this comment

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

any time an error is ignored anywhere in our code, its a bug.

very much agreed.

though:

  1. logging is not always an ignored error. sometimes there is nothing to do in a messy io environment (e.g. receiving incoming connections from things that are not running the ipfs protocol. there is nothing to do but log/bookkeep).
  2. this error isn't ignored. an HTTP error header is written, and the error itself is written out to the user for feedback. we could do something else for bookkeeping (like add it to a counter somewhere, throw an event, etc), but correctness is ok here.

@whyrusleeping
Copy link
Member

@wking contexts should be used when you are going to either perform RPC's, spawn off child goroutines you want to handle, or in general when you want to be able to cancel an operation. Not everything needs to take them, but our high level things should take them, otherwise things may hang. Contexts also allow us to more easily clean up after ourselves; cancelling a context says to all your children "stop whatever you are doing, clean up, and exit ASAP"

@wking
Copy link
Contributor

wking commented May 3, 2015

On Sat, May 02, 2015 at 08:51:09PM -0700, Jeromy Johnson wrote:

@wking contexts should be used when you are going to either perform
RPC's, spawn off child goroutines you want to handle, or when you
want to be able to cancel an operation. Not everything needs to take
them, but our high level things should take them, otherwise things
may hang.

Lots of things can block besides RPCs and goroutines. Why doesn't
io.Reader.ReadAll() take a Context? Or sync.Mutex.Lock()?

Contexts also allow us to more easily clean up after ourselves;
cancelling a context says to all your children "stop whatever you
are doing, clean up, and exit ASAP"

Which you can do between processes with signals, or in an event-loop
situation just by releasing the state associated with the aborted
request. Are Contexts really the easiest way to do this in Go?

@whyrusleeping
Copy link
Member

Ill let google explain it better than I can, lol https://blog.golang.org/context

@jbenet
Copy link
Member

jbenet commented May 3, 2015

@wking on the contexts-- the reality with Go everything is there's no hard and fast "right" way to do all these things. contexts are very nice in many cases, and very hairy in others (try mixing it with net.Conn's, io, and so on). As with most things, Go tries to cover 80% of the cases really simply, and does a very good job at this. We'll certainly find corner cases where things get complex (and even annoying), but for the most part contexts are a very elegant solution to a difficult problem. Anyway, yeah-- read up on it. and read up on the various takes on it. Take a look at how the examples Sameer gives work(and perhaps some of the more esoteric cases, like the go grpc implementation).

return node, p, err
}

// TODO(cryptix): these four helper funcs shoudl also be available elsewhere, i think?
Copy link
Member

Choose a reason for hiding this comment

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

agreed.

@cryptix
Copy link
Contributor Author

cryptix commented May 8, 2015

TODO:

@jbenet
Copy link
Member

jbenet commented May 8, 2015

@cryptix I'll rebase this on master so the CR is easier. (last head: 416f435)

}

if r.Method == "HEAD" {
if r.Method == "GET" || r.Method == "HEAD" {
Copy link
Member

Choose a reason for hiding this comment

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

why not case "GET", "HEAD": in the switch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the above only is for Writeable. I didn't want to introduce another switch with just one case.

Copy link
Member

Choose a reason for hiding this comment

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

ohhh missed that-- my mistake :)

@jbenet
Copy link
Member

jbenet commented May 8, 2015

@cryptix #1189 was merged, so

  • re-enable t0111-writable.sh ?

@cryptix
Copy link
Contributor Author

cryptix commented May 9, 2015

rebased

@jbenet
Copy link
Member

jbenet commented May 9, 2015

@cryptix may want to squash 1a7b85b wherever it's supposed to go, else some previous commit wont pass all tests

@cryptix
Copy link
Contributor Author

cryptix commented May 9, 2015

@jbenet Yea, that one was stupid.. i'll squash related once when I'm done.

@jbenet
Copy link
Member

jbenet commented May 10, 2015

#1219 merged the changed without the put/writable tests parts. Rebased on top of new master.

@cryptix cryptix mentioned this pull request May 11, 2015
36 tasks
jbenet added a commit that referenced this pull request May 11, 2015
@jbenet jbenet merged commit 08ea56c into master May 11, 2015
@jbenet jbenet removed the status/in-progress In progress label May 11, 2015
@jbenet jbenet deleted the refactor/httpGateway branch May 11, 2015 18:43
hacdias pushed a commit to ipfs/boxo that referenced this pull request Jan 27, 2023
Refactor/http gateway

This commit was moved from ipfs/kubo@08ea56c
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.

Gateway broken for IPNS entries
4 participants