175 points · by nateb2022 · 2y ago · 131 comments
On Kubernetes, running with regular pods it was not desirable. Maybe if we were deploying with StatefulStates.
A while back, I tried to solve no.2 on your list by having a dynamically expanding and shrinking go-routines. POC here: https://github.com/didip/laborunion. It is meant to be used in-conjunction with config library & application object that's update-able over the wire. This idea is good enough to hot-reload a small IoT/metrics agent, but I never got around to truly solve the problem completely.
Also an overlooked part here is that the global Erlang GC is easier to parallellize and/or keep incremental since it won't have object cycles sans PID's (that probably have special handling anyhow).
TlDr; GC's become way harder as soon as you have cyclic objects, Erlang avoids it and thus parts of it being good is more about Erlang being "simple".
Go's GC already has a lot of work done to minimize "stop the world" time down to very small values.
> ... a command-line utility designed to simplify the process of generating boilerplate code for your project based on the Ergo Framework
Why is there any boiler plate code at all? Why isn't hello world just a five line programme that spawns and sends hello world somewhere 5 times?
I've been thinking for years that if a project existed like this for Python it would take over the world. Golang is close, I guess.
It looks like a close copy of Erlang APIs, albeit with the usual golang language limitations and corresponding boilerplate and some additional stuff.
Most interesting to me is it has integration with actual Erlang processes. That could fill a nice gap as Erlang lacks in some areas like media processing - so you could use this to handle those kind of CPU bound / native tasks.
func (a *actorA) HandleMessage(from gen.PID, message any) error {
switch message.(type) {
case doCallLocal:
local := gen.Atom("b")
a.Log().Info("making request to local process %s", local)
if result, err := a.Call(local, MyRequest{MyString: "abc"}); err == nil {
a.Log().Info("received result from local process %s: %#v", local, result)
} else {
a.Log().Error("call local process failed: %s", err)
}
a.SendAfter(a.PID(), doCallRemote{}, time.Second)
return nilWhy a function that takes an Actor instead of each Actor being a type that implements a receive function? There’s so much Feature Envy (Refactoring, Fowler) in this example. There is no world where having one function handle three kinds of actors makes any sense. This is designed for footguns.
I also doubt very much that the Log() call is helping anything. Surely lathe API is thin enough to inline a that child.
That golang is a mess, and demonstrates just what a huge conceptual gap there really is between the two. Erlang relies on many tricks to end up greater than the sum of its parts, like how receiving messages is actually pattern matching over a mailbox, and using a tail recursive pattern to return the next handling state. You could conceivably do that in golang syntax but it will be horrible and absolutely not play nicely with the golang runtime.
defmodule KV.Supervisor do
use Supervisor
def start_link(opts) do
Supervisor.start_link(__MODULE__, :ok, opts)
end
@impl true
def init(:ok) do
children = [
KV.Registry
]
Supervisor.init(children, strategy: :one_for_one)
end
end
In ergoI've written before about how I think the more FBP-style concurrency of Go and the message passing one in Erlang complement each other as much as streaming DNA processing inside the cell and fire and forget cell to cell signaling between cells do in biology:
https://livesys.se/posts/flowbased-vs-erlang-message-passing...
The FBP/CSP-style in Go being more suited for high performance streaming operations inside apps and compute nodes (or cells), while the message passing mechanism shines over more unreliable channels such as over the network (or between cells).
Ergo seems like it might allow both of these mechanisms to be implemented in the same language, which should be a big deal.
For more context, most of my work is around real-time streaming / monitoring of trades and trading metrics.
For example you could parallelize a for loop by creating a goroutine for each item in a list. Or separate actors for logging, session tracking, connection pools, etc.
Doing that with processes and Kafka would be massive overhead.
What is the connection between an actor and a database for instance?
Why do I need actors?
How equivalent are BEAM processes and goroutines?
[1]: you usually use channels instead
[2]: you usually use sync.WaitGroup (or similar, e.g. errgroup.Group) instead
Goroutines communicate through channels, all you need is a queue (eg buffered chan).
> you cannot terminate a goroutine from another goroutine
Termination is propagated via context cancellation. go-A cancels ctx, go-B waits with `select`, reads from `<-ctx.Done()` and does a `return`, or checks it after each blocking call.
> there are no "goroutine-local variables"
Not sure if I got this one, but every var in a function's scope, which has been `go`-routined, would qualify.
I'm currently working on a lib/framework somehow related to Ergo, but taking a more "generic" approach of a state machine[0]. It may solve some of the mentioned issues with Go, like addressing and queues for communication.
You seem to be very attached to an idea of using the same goroutine for a long time, whereas it's usually more dynamic and only schedulers are long lived `go`-s.
I appreciate the mention of context cancelation, that is a good example of how to terminate a goroutine, provided that a) the goroutine has a context to cancel and b) the goroutine does not spend too much time in operations that can't be canceled (e.g., file system calls, time.Sleep, blocking select without case <-ctx.Done(), etc.). This is still cooperative multitasking though, while BEAM processes are fully preemptive.
A "goroutine-local variable" would be a variable accessible by other goroutines which is still scoped to a particular goroutine. Ordinary local variables are only accessible by the goroutine in whose stack they belong. Something like the process dictionary can be constructed in Go using a synchronized map with atomic values, but it certainly isn't a built-in part of the runtime like it is in BEAM.
I generally don't regard goroutines with much attachment. There are, however, some situations in which goroutines must be long-lived: the main goroutine, the "scheduler" goroutines you mentioned, and in general any goroutine performing a supervisory role over other goroutines. Monitoring these important goroutines is more complicated in Go than BEAM languages, but certainly isn't impossible.
The more problematic aspect of goroutines vs. BEAM processes is that, due to the lack of application-accessible preemption, when a critical-path goroutine does get stuck, the only real solution most of the time is to kill the entire program. This is rarer than it was in Go's early days, at least.
var sent bool
select {
case channel<-message:
sent = true
default:
sent = false
}Go is also not supposed to be used in a complicated way. Its use cases must remain simple.
This is something seldom attempted, but I congratulate you. Go is one of a few languages that really is batteries-included. Just about anything you could need is included in the stdlib.
https://fasterthanli.me/articles/i-want-off-mr-golangs-wild-...
Not to mention nothing prevents anyone from using or writing their own library only for the parts that need specialization. You're free to do that and many have.
And standard libraries can be versioned and deprecated too.
The time API, on the other hand, is that bad, and worse. Besides the bizarre monotonic time implementation, there's also the bloated size of the time.Time struct, and the lack of actual "dates" (YYYY-MM-DD) and "times" (HH:MM:SS) that you can do arithmetic on. You can't really roll your own robustly either because time.Time isn't a great foundation to build on and //go:linkname is getting locked down.
Thankfully, build constraints are much better now, since they just use regular boolean operators, e.g.:
//go:build windows && (i386 || amd64)
I agree that there shouldn't be any _buildtag.go magic, and I think they ought to remove that "feature" entirely in a future version of Go (which can be done so that it doesn't affect older or unversioned code). It seems they added a "unix" build tag too.Also, one of my personal gripes is that the standard library exposes no guaranteed way to access the system CSPRNG. Any code can replace (crypto/rand).Reader with another implementation and compromise cryptographic operations. It's a trivial supply-chain attack vector, and even in non-malicious scenarios, it can be done mistakenly and break things all over the place in a subtle but dangerous way. The language developers have so far refused to fix it too.
Then there's log/slog with no TRACE or FATAL levels built-in; yeah, you can roll your own levels, but why should you have to?
It would have been interesting if the author had a suggestion on what the Golang team should've / could've done instead, beyond correctly communicating the nature of the breaking change in the Go 1.9 release notes.
Even something as basic as JSON encoding requires an external package in Java.
Some of the above will never be in Go due to how the community and language designers are philosophically against them.