4 ms·
Life of an HTTP request in a Go server (2017)
- bullen 6y agoIf you need to lock when accessing memory between requests you are using the wrong language: https://eli.thegreenplace.net/2019/on-concurrency-in-go-http-servers https://eli.thegreenplace.net/2019/on-concurrency-in-go-http... Erlang, Rust and Go can only be used to solve embarrassingly parallel problems.
- fwip 6y agoCould you expand on this? Mutexes are not the only way to protect shared state in Go. What language constructs/patterns do you feel are better suited to solving parallelism problems, and what languages exemplify these techniques?
- bullen 6y ago"While I'm on the topic of concurrency I should mention my far too brief chat with Doug Lea. He commented that multi-threaded Java these days far outperforms C, due to the memory management and a garbage collector. If I recall correctly he said "only 12 times faster than C means you haven't started optimizing"." - Martin Fowler https://martinfowler.com/bliki/OOPSLA2005.html https://martinfowler.com/bliki/OOPSLA2005.html
- fwip 6y agoGo has both memory management and a quite performant garbage collector, as you probably know. Your article is from 2005, so I take it to mean that Java 6 was doing the interesting things you talk about - from my recollection, the go-to at that time was java.util.concurrent. (I haven't kept up with Java, so it could still be state-of-the-art.) https://www.sciencedirect.com/science/article/pii/S0167642305000663 https://www.sciencedirect.com/science/article/pii/S016764230... - this paper describes the implementation as "The heart of the framework is maintenance of queues of blocked threads, which are restricted here to FIFO queues." If you're not familiar, this is exactly what go channels are, just baked into the language rather than in the standard library.
- pjmlp 6y agoThe major differences being that java.util.concurrent in Java 16 is a bit more powerful as the one in Java 6, Java doesn't have one single GC rather multiple vendor implementations to choose from, with virtual threads on the horizon. Additionally one benefit over Go, is that in Java (likewise in .NET), the scheduler algorithm is replaceable.
- fwip 6y agoThat's valid, I didn't mean to imply that Java was stuck in 2005. I'm having a hard time following the clues that bullen is trying to leave in order to divine their meaning, so I was trying to limit the scope.
- bullen 6y agohttp://nil.csail.mit.edu/6.824/2016/notes/gomem.pdf http://nil.csail.mit.edu/6.824/2016/notes/gomem.pdf The more you downvote me the stronger I become! :D
- Jtsummers 6y ago> Erlang, Rust and Go can only be used to solve embarrassingly parallel problems. I'm pretty sure each of those have been used for other kinds of parallelism than just embarrassingly parallel problems.
- icholy 6y agoWhat does that even mean?
- kevincox 6y agoI don't see the problem here. You need to have serial access to the counter. You could use a single-threaded webserver but there is a lot more to that request than actually incrementing the counter. It still makes sense in most cases to use a threaded webserver and lock on incrementing the counter. (Especially if you are using a concurrent hashtable so that you can increment multiple counters in parallel)
- jamra 6y agoYou could use the atomic package and forego the lock. You can also have a go routine with a channel that receives the counter messages. No need for a lock there. But use it if you really need it. Likely that’s not your bottleneck.
- CraigJPerry 6y agoThere’s a fascinating, if rather long, dive into Go HTTP processing issues (nodejs too) at https://fasterthanli.me/articles/aiming-for-correctness-with-types https://fasterthanli.me/articles/aiming-for-correctness-with... It’s a long read but it’s one of the most enjoyable things i’ve read in ages.
- parley 6y agoI am not a Go expert so please correct me if I'm wrong, but... The fact that the stdlib HTTP utils in Go recover from panics in handlers and provide no way of changing this via easy setting is one of the things that really annoy me. Seemingly, they consider it a backwards compatibility break since it's a behavior change that would affect all existing middleware you try to use (see e.g. https://github.com/golang/go/issues/16542 https://github.com/golang/go/issues/16542). You need to provide your own error handling middleware (of which several exist). A beginner needs to be very careful and read the docs to catch e.g. this line: "If ServeHTTP panics, the server (the caller of ServeHTTP) assumes that the effect of the panic was isolated to the active request. It recovers the panic, logs a stack trace to the server error log, and either closes the network connection or sends an HTTP/2 RST_STREAM, depending on the HTTP protocol. To abort a handler so the client sees an interrupted response but the server doesn't log an error, panic with the value ErrAbortHandler. " I think that is an unintuitive assumption that makes for brittle software. Also, the fact that Go kind of blesses the use of panics internally in a library (e.g. "The convention in the Go libraries is that even when a package uses panic internally, its external API still presents explicit error return values.", https://blog.golang.org/defer-panic-and-recover https://blog.golang.org/defer-panic-and-recover) makes for even more brittle software, as predicting what state a control flow interruption like panic through a call stack leaves your data in can be challenging and require great care to avoid invariants being violated. Last I checked, e.g. Rust did not allow the reuse of data owned by a panicking call stack without explicitly asserting that it is still considered valid. I guess I'm showing my bias for languages that make it harder to make mistakes, but I don't like brittle stuff like encouraging panic-like control flow for non-exceptional situations, implicit reuse of "panicked data", missing non-exhaustive match on enums, missing enums altogether, missing sum types and pattern matching making everything from detailed error management to proper state representation more brittle, etc etc. I'll stop there and not get into the rest of the stuff. Go is one of the languages my employer pays me to write and I will say that I have a significantly higher appreciation for it now than when I started (it IS very beginner friendly and very ergonomic in general), but I wish it would help me more to write really robust and correct software.
- pjmlp 6y agoI fail to see the problem, Java servlets and ASP.NET handlers do exactly the same by default.
- eliben 6y agoThanks for posting this :) Not sure why the (2017) tag though, this post is from yesterday!