257 points · by tutfbhuf · 5y ago · 144 comments
This is mostly the way today services in big OSS projects are exposed outside for consumption in a RESTful style. One exception I know of is sourcegraph/sourcegraph.
gRPC feels to me like a violation/break in expectations of how the layers of abstraction are supposed to work -- gRPC works over HTTP/2, and it's weird that it translates a lower level (HTTP/1) at a higher level (gRPC) to re-encode it into something at the lower level. An appropriately robust protocol would simply support both lower levels of abstraction if it wanted to (and they were widespread in daily use), right?
The only places I feel like I see this kind of layer violations are in lower level networking and it generally just makes everything worse and more complicated there. Of course, I know that gRPC + gateway is actually not a huge deal in practice -- you've got reverse proxies like envoy that will do it for you automatically[0], but it just... doesn't sit great. The benefits of gRPC are not to be sneezed at (better performance, strict typing at the protocol level, schema enforcement, bidirectional streaming, etc), but it feels like it could have accomplished a lot of those goals without throwing out HTTP/1.1 completely (and then that machinery could have been reused to support HTTP/3).
[0]: https://www.envoyproxy.io/docs/envoy/latest/configuration/ht...
[0]: https://rsocket.io/
You only get a certain number of complexity tokens and IMO it's not worth spending any here.
Between a few breaking upstream changes in grpc-gateway (iirc), learning a new framework vs the stdlib + chi router setup that had already been proven in the greater team, feature shipment dropped through the floor. Couple that with the fact that, as stated, these benefits come with well-iterated data models; conversely, our _not_ well-established data models proved to be a constant PITA when having to regenerate this file and that file. No one used the auto-gen'd docs because they were constantly changing anyway. And integrating with our CI/CD pipeline was a nightmare.
Between providing value with a team's previously proven tools and spending innovation tokens, I'd definitely suggest weighing carefully if you need this to be one of them (bi-directional streaming and a well-established project seem like good candidates though).
I believe it supports, http, websocket and grpc.
These are meant to fit grpc-gateway to scenarios where gRPC server might be implemented in another programming language, or accepting clients at a TCP port or another kind of a socket, possibly under a different network realm than grpc-gateway. Pretty much like a reverse proxy, but can also be integrated with services in the same code level, escaping the overhead of a transport layer.
I don't think there's been a single instance where I've thought "why can't stdlib do this?" nor "why the heck is this in stdlib?"
I've had this a few times, most recently with "how do I add this data file to my binary". At least that one made it to master now, and will be in 1.16!
Another gripe is the lack of a proper parallel safe map (no, map[interface{}]interface{} like sync.Map is just not acceptable) which would be a godsend and should honestly just be the normal map implementation. Maybe eventually with generics... (This is more of a language gripe than a stdlib gripe, though, and I'm sure that once generics come out we'll see a sync.Map that doesn't use interface {})
And before 1.16, there is statik: https://github.com/rakyll/statik. Creates an embeddable file system from files or directories. It’s awesome for packaging web front ends into binaries.
0: https://go.googlesource.com/proposal/+/master/design/draft-e...
Ok fair enough, that one bit me too, but honestly I only felt cheated because Rust makes it so easy. `include_bytes!` was like cheating the first time I used it.
Wait, how?? I've done some unholy things.
package main
import (
"embed"
"net/http"
)
//go:embed assets/*
var assets embed.FS
func main() {
fs := http.FileServer(http.FS(assets))
http.ListenAndServe(":8080", fs)
}
For the next month, the list of options here [0] will have to suffice.0: https://go.googlesource.com/proposal/+/master/design/draft-e...
> I know people will complain about speed of execution of language and framework but do you really care if you are not expecting Google like traffic.
If you don't care that's fine, but there are lots of us that do. This is subjective, but I find the memory use and overall performance of Rails/Django/Spring Boot applications to be completely unacceptable and there are far too many breaking changes.
If we're talking real time sub-millisecond performance, then Rust.
I know this is all opinion and I have colleagues who are excellent engineers that prefer rails, but it goes against everything I enjoy about software development.
Having worked a couple places where Python or Go microservices were hyped up, but every installation, DB schema, and folder structure seemed to be built with a different idea in mind, and or debated, I have a strong appreciation for right standards around convention. Especially when I don’t have to get into debates like whether DB table names should be plural or singular.
On the side I really do enjoy working with Go though, and wouldn’t hesitate to use it if it seemed like the right tool for the moment.
I’m not super convinced that’s a bad thing though. The software industry has always suffered a perennial shortage of engineers. What I predict will happen is that an ecosystem of tools will spring up around rails and there will be serious investment in improving performance rather than the framework being abandoned.
Remember how kubernetes changed infrastructure development to managing config files? It’s what I see happening to most areas of software development. The more experienced software engineers will then be responsible for optimizing/bug fixing/ scaling.
There is a language that has faster-than-go speed and better abstractions, but HN seems to be somewhat decided on whether they think it's awesome or terrible, and there was recently an blog post on front-page about how it was bad for APIs (which I heavily disagree with but I am biased so I opted not to comment on).
Go definitely does have some other web frameworks that make work like this simpler, but on the other hand, people sometimes shy away from those because the stdlib is more universal.
If we're talking just pure abstraction I think there are a lot of other choice that could have delivered similar improvements in performance for an IO-dependent workload, with better methods of abstraction, ecosystem, and safety -- that choice for me would be Typescript.
[EDIT] Also to note, even as a Haskell zealot I'm not crazy enough to suggest someone choose Haskell as an alternative where Go would have been good enough. I have enough experience with business needs to know that the purity/safety/whatever other benefits of haskell just aren't worth the lack of ecosystem, difficulty in finding developers, and hit to developer speed. Haskell is too far on the spectrum (on various axes) to be the right choice most of the time, and not enough companies have shared how they've outperformed with it to even start the conversation. Haskell is like the mercedes of programming languages -- airbags show up there first, but regular cars get them eventually.
It's not about whether you expect Google-like traffic, but about whether you expect a Google-like ratio of traffic/compute to capacity. My day job doesn't deal with "Google-like" traffic (maybe one Google product's worth of traffic), but we also only have about 10 servers to ingest what we do get. My current side project I expect to deal with even fewer orders of magnitude but I also want to be able to run it on a single server to keep costs and operational overhead low.
> There are strong opinions both for and against using frameworks. My goal in these posts is to examine the issue objectively from several angles.
My only question is why the server / HTTP handlers have to deal with the Mutex. That seems like a "leak" from the `TaskStore` abstraction, which otherwise I really like. (Thank you for not using channels in that interface!)
store.Lock()
store.WriteKey("foo", "bar")
x := store.ReadKey("foo")
store.Unlock()
// x is always "bar"
And: store.Lock()
store.WriteKey("foo", "bar")
store.Unlock()
store.Lock()
x := store.ReadKey("foo")
store.Unlock()
// x could be whatever another goroutine set "foo" to, not the "bar" that you just wrote.
In a more complicated app, you'll have library that acts as the datastore, with transaction objects that abstract away the actual mutex (which will be something more complicated): var x string
err := db.DoTx(func(tx *Tx) {
tx.Write("foo", "bar")
x = tx.Read("foo")
})
if err != nil { ... }
// what x is depends on the details of your database; maybe you're running at "read uncommitted", maybe you're running at "serializable".
But, even in the simple examples, it's worth thinking about the difference between lock { write; read } and lock { write }; lock { read }.Can someone chime in? There must be an obvious advantage that I might be missing.
The act of writing these kind of systems is IMHO, somehow, liberating.
Compiled code is also significantly faster in many cases.
2. execution speed - Django's ORM comes to mind
3. concurrency
That being said there are alot of great features with python web frameworks that come out of the box. You need to a lightweight project like gin/echo or something to get these features. Naked net/http is sorta like the 'erector set' of web frameworks.
I remember having the same sentiment a few months back, but investing a considerable amount of time planning the structure of serializers and sticking to DRF's patterns did reduce the amount of "fighting" that I need to do to make my API work as expected.
With "turning it on", you mean you write a systemd configuration file and start it using systemctl, right? Just curious how people do this stuff nowadays.
type Store interface {
GetTask(*Task) error
}
instead of having "GetTaskByID" and "GetTaskByTag" and whatnot. Then in the caller you just do this: task := store.Task{ID: 5}
if err := db.GetTask(&task); err != nil {
// wahtever
}
^ that gets the task by ID task := store.Task{Tag: "foo"}
if err := db.GetTask(&task); err != nil {
// wahtever
}
^ that gets the task by tag.I mean ... for the second problem that's broadly true of making any changes to your data access layer since by definition your Go compiler is not going to, for example, check the validity of a SQL query. So ... yes? but that's not unique to this approach, that's true generally.
I'm curios about the way you handled zero values, field masks could be a solution, but I think it would get bloaty.
func (db *actualStoreImplementation) GetTask(t *Task) error {
if (t.ID != 0) {
// query by ID, mutate the parameter, return nil
}
if (t.Tag != "") {
// query by tag, mutate the parameter, return nil
}
return ErrWhatever
}
usually I have some other package that defines all of the types that can appear on the wire (which I often call `wire` because `proto` is taken by protobuf), define some exported interface in that package with an unexported method so that no other packages can define new types for that interface, and then have a method on my db structs that returns the wire types, like this: func (t Task) Public() wire.Value {
return wire.Task{
// explicitly generate what you want
}
}-CORS support. Deploy this to a non-local domain and try to reach it from a web browser and it will fail. I like https://github.com/rs/cors. I had rolled my own but then moved to that library.
- input validation. I like go-playground/validator.
The other big issue is the locking by hand around the task store. In reality usually there would be a database to handle concurrent read/writes. I use SQLite in production. I know this is just a demo and you want to use just stdlib, but serializing all data access is sort of unacceptable as a solution in a concurrent language like Go. When I'm not handling concurrency with SQLite I like to implement The actor pattern, having a persistent goroutine listen and respond to "taskstore" requests via channels.
Maybe this is just my opinion as I started a significant golang app with just vanilla stdlib and then added each of these complexities in turn, before switching to libraries to solve most of the boilerplate better than my hand-crafted patterns
It contained no explanation or reasoning to that end. It is purely what it is titled as: An explanation of how to do it with the standard library.
In that sense I don't think there is anything wrong with it. It doesn't make any claims; it simply provides the information it said it would.
Also, one of the main selling points of Golang is that it aims to deliver functionality with minimal additional cruft/add ons. Many of the standard library things are this way and don't provide the "extra stuff" you are desiring.
You mention auth, but many of the frameworks for Golang don't even implement auth and merely provided the ability to add auth if desired.
Logging is a debatable topic because the "standard" logging method is very inadequate, and so each of the various frameworks has to just choose a random logging system to use. ( or several )
While some frameworks do offer the ability to restart without killing active connections / requests, it is a more generalized issue with any service in Golang. It is also best addressed by writing your service in a scalable fashion and doing a rolling upgrade. Stop sending traffic to an instance, wait till current requests are done, then kill it. It doesn't need to be addressed within the service itself.
The example is simply the beginning. It can be extended as needed depending on your use case. This is how most things Golang work. You start with the basics and add what you need on a case by case basis.
If you want something with every bell and whistle out of the box, Golang is going to be a disappointment generally as that is not the Golang mentality.
There are of course sufficient frameworks in Golang these days to provide such things though. Hence the article clearly says "standard library" to indicate that it isn't the last word on how to setup a REST service in Go.
1) Be careful with locks in the form "x.Lock(); x.DoSomething(); x.Unlock()". If DoSomething panics, you will still be holding the lock, and that's pretty much the end of your program. ("x.Lock(); defer x.Unlock(); x.DoSomething()" avoids this problem, but obviously in the non-panic case, the lock is released at a different time than in this implementation. Additional tweaking is required.)
Generally I don't like locks in the request critical path because waiting for a lock is uncancelable, but in this very simple case it doesn't matter. For more complicated concurrency requirements, consider the difference between x.Lock()/x.Do()/x.Unlock vs. select { case x := <-ch: doSomethingWithX(x); case <-request.Context().Done(): error(request.Context().Err()) }. The channel wait can be cancelled when the user disconnects, or hits the stop button in the error, or the request timeout is reached.
2) Long if/else statements are harder to read than a switch statement. Instead of:
if(foo == "bar") {
// Bar
} else if (foo == "baz") {
// Baz
} else {
// Error
}
You might like: switch(foo) {
case "bar":
// Bar
case "baz":
// Baz
default:
// Error
}
These are exactly semantically equivalent, and neither protect you at compile-time from forgetting a case, but there is slightly less visual noise. Worth considering.3) I have always found that error handling in http.HandlerFunc-tions cumbersome. The author runs into this, with code like:
foo, err := Foo()
if err != nil {
http.Error(w, ...)
return
}
bar, err := Bar()
if err != nil {
http.Error(w, ...)
return
}
Basically, you end up writing the error handling code a number of times, and you have to do two things in the "err != nil" block, which is annoying. I prefer: func DoTheActualThing() ([]byte, error) {
if everythingIsFine {
return []byte(`{"result":"it worked and you are cool"}`), nil
}
return nil, errors.New("not everything is okay, feels sad")
}
Then in your handler function: func ServeHTTP(w http.ResponseWriter, req *http.Request) {
result, err := DoTheActualThing()
if err != nil {
http.Error(w, ...)
return
}
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(result)
}
In this simple example, it doesn't matter, but when you do more than one thing that can cause an error, you'll like it better.I don't disagree that it's more practical, initially, as you've described it. However, I think it's important to point out the tradeoff rather than presenting it as purely more efficient. I've seen this approach result in poor separation of concerns and bloated use cases (`DoTheActualThing`) which become tedious to refactor, albeit in other languages.
One predictable side effect of the above, if you're working with junior engineers, is that they are likely going to write tests for the application logic with a dependency on the request / response as inputs, and asserting on status codes etc. I shudder to think how many lines of code I've read dedicated to mocking req/res that were never needed in the first place.
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/foo", nil)
ServeHTTP(w, req)
if got, want := w.Code, http.StatusOK; got != want {
t.Errorf("get /foo: status:\n got: %v\n want: %v", got, want)
}
if got, want := w.Body.String(), "it worked"; got != want {
t.Errorf("get /foo: body:\n got: %v\n want: %v", got, want)
}
It leaves very little to the imagination as to whether or not ServeHTTP works, which is nice.Complexity comes from generating requests and parsing the responses, and that is what leads to the desire to factor things out -- test the functions with their native data types instead of http.Request and http.Response. I think most people choose to factor things out to make that possible, but in the simplest of simple cases, many people just use httptest. It gets the job done.
The problem I've seen is over-dependence on writing unit tests with mocks instead of biting the bullet and properly testing all the boundaries. I have seen folk end up with 1000+ tests, of which most are useless because the mocks make far too many assumptions, but are necessary because of the layer coupling.
This was mostly in Node though, where mocking the request/response gets done inconsistently, per framework. Go might have better tooling in that regard, and maybe that sways the equation a bit. IMO there's still merit to decoupling if there's any feasibility of e.g. migrating to GraphQL or another protocol without having to undergo an entire re-write.
Just want to address (1) quickly. As you've mentioned at the end of the parenthesized note, the reason I did not use `defer` here is to avoid the lock staying across the rest of the handler. I wanted to confine it to the datastore interaction.
Thinking more about this now and having read the comments, I'm considering to just hide the lock in TaskStore and avoid all these explicit locks/unlocks in handlers; it seems like it will avoid some confusion for folks reading the example (as well as quite a few lines of code!), and my goal here is really the HTTP server logic. I prefer to deflect any attention from TaskStore in this series of posts.
type HTTPError interface {
GetHTTPCode() int
}
func ServeHTTP(w http.ResponseWriter, req \*http.Request) {
result, err := DoTheActualThing()
if err != nil {
statusCode := http.StatusInternalServerError
if httpError, ok := err.(HTTPError); ok {
statusCode = httpError.GetHTTPCode()
}
http.Error(w, ..., statusCode)
return
}
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(result)
}
You can apply the same approach for the HTTP body in case of error.If that goroutine hasn't finished processing then the channel will be blocked, just like a mutex.
So in your handler you can use a select statement to either write to the channel OR read from the request.Context().Done(). The request context only lives as long as the request. So if the connection drops or times out then the context gets cancelled and a value is pushed onto the done channel and your read is unblocked.
Because you use a select statement then which ever operation unblocks first is what happens. If the write channel unblocks then you get to write your value. If your request context gets cancelled then you can report an error. The request context will always get cancelled eventually, unlike a mutex which will wait forever.
Interesting, thanks. But isn't panicking the end of your program anyway? Could you provide another example where no using defer causes problems?
Also, one thing I'm still waiting for is a decent non-complex di that works using go generate. I'm aware of google's wire (?) But I've looked at it multiple times and I still struggle to understand it.
DI and IoC are for people ignorant of or hostile towards composition.
Further, aspects and reflection are for those unwilling or unable to make reasonable architectural assumptions.
Said another way, meta programming is for personal projects. And maybe for small, disciplined, high trust teams.
Most devs are average (axiomatically) and most projects are CRUD or scraping. So choice of these tools is self soothing to mitigate inferiority complexes. Like Mensa.
A DI framework allows you to set conventions to avoid repeating yourself in the initialization phase. (ie if a class requires a parameter 'foo', look for a class named 'FooImpl').
Mocking objects for testing purposes and swapping them for the real objects is also something commonly done that is helpful.
There is no "real" DI for Golang as far as I've seen. The only DI I've seen that effectively do the sort of thing I am describing are for Java.
As for testing, you can already swap out real objects for test objects--that's a property that interfaces provide; I don't see how a DI framework (e.g, the DSL) helps you here.
I will say that when I've had to operate a Java application, trying to do even the smallest bit of debugging had me digging through all sorts of layers of XML nonsense (Spring, various Spring plugins, and who knows what else) to figure out where the logs were being written. Note that my objection isn't "XML" (versus some other format/DSL), but rather the pointless indirection. I'm sure someone proficient in Java would have no problem, but now your sysadmins need to be Java developers in addition to system administrators (maybe not such a big deal if your shop is already a Java shop and you have people you can ask, but for people operating third party software this is a real pain point). And again, all of that tedium for no apparent value.
err = json.NewEncoder(w).Encode(&task)
I know there used to be a reason why this was disfavored but thought it had been addressed in the stdlib.How are folk managing Go endpoint as apps scale across number of endpoints? I'm skeptical that one struct in one folder leads to good app structure, and have seen this start to break down in a few cases in practice
If you have independent, different elements in that API, you break them out into separate "servers" but still register the endpoints on the same HTTP handler.
I know that people don't like external libraries too much, but I'd like to plug my own here. You declare your API in OpenAPI 3.0 (aka, Swagger) and it generates your server and models for you, so all you need to do is write the business logic. (https://github.com/deepmap/oapi-codegen)