🔗 The sync package
Virtual Meetup #10 took place on Friday, April 17, 2020, 19:00 CEST via Zoom (thanks to saschagrunert and CNCF).
Michael prepared a great overview of the sync and x/sync packages, which implement concurrency related facilities (concurrency is hard with either classical approaches or CSP, as we learned from a presentation last year at meetup #2).
The overview included among other things a bug hunt in a counter example and a benchmark of the builtin map and sync.Map.
The x/sync contains the very useful errgroup and singleflight.
One thing that the errgroup will not provide for out of the box is the pickup of multiple error, should more than one occur.
Go calls the given function in a new goroutine. The first call to return a non-nil error cancels the group; its error will be returned by Wait. – https://godoc.org/golang.org/x/sync/errgroup#Group.Go
Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them. – https://godoc.org/golang.org/x/sync/errgroup#Group.Wait
An implementation of this feature can be found in k8s apimachinery’s error utilities.
The singleflight pattern can also be found in the perkeep utils go4 repository, in the singleflight package.
Package singleflight provides a duplicate function call suppression mechanism.
The way to test singleflight can be interesting, too. One pools up a number of goroutines (by starting and waiting a bit). Then, a single value sent on a channel serves as a starting shot. At the same time, the atomic counter on calls ensure there has only been a single function call.
In one of the upcoming meetups, we will highlight tools from the Hashicorp stack, as there is certainly in interest in these tools.
Go can be a glue language, e.g. to combine exiting, tested tools with some thin convenience layer. On a lighter side, this is what ttarc does, a TikTok archiving tool: It wraps wget with a bit of networking and JSON parsing to generate WARC files.