Thursday, April 25, 2024
HomeGolangHigher structured concurrency for Golang

Higher structured concurrency for Golang


Go is designed to make concurrency simpler than it’s in different languages, however at Sourcegraph, I nonetheless discovered myself operating into the identical issues repeatedly when writing concurrent code.

Particularly, it’s tough to put in writing concurrent code that operates fairly within the face of panics. We don’t need the whole course of to crash when a panic happens in a spawned goroutine, and we wish to keep away from different issues that may be triggered by panics like deadlocks or leaked goroutines. Go doesn’t present a straightforward means to do that natively.

So I constructed conc, a library that makes writing concurrent code extra elegant and reduces the quantity of boilerplate code. The code under reveals how a lot boilerplate you may scale back when utilizing conc as an alternative of the Go customary library.

Some background: Fixing the identical downside many instances and constructing an inside library

I’ve typically discovered myself writing comparable code to deal with widespread concurrency patterns and panics in Go. In lots of Sourcegraph tasks, we rely closely on concurrent Go code to effectively search billions of strains of code, so after the Go 1.18 launch in March 2022 launched generics, I noticed that this might repair a number of the frustrations I’d had earlier than and make a concurrency library much more succinct.

Beforehand after I’d written helper features to higher deal with concurrency, I wanted completely different features for various information varieties. This meant writing separate features for strings, duties, or some other information sort that I used to be working with. Now that generics can be found, it’s simple to function over no matter information varieties are current in a constant means.

Round six months in the past, I began engaged on an inside library to generalize how we use goroutines. Internally, we’ve used this library extensively already, however now I’ve cleaned up the code, documented every thing, and launched it as an open-source bundle for different builders and groups to make use of too.

I discovered time to do nearly all of this cleansing and documenting whereas I used to be trapped at an airport over the Christmas break as a result of flight delays throughout the US in December. I wrote the README, added and cleaned up the feedback and docstrings, and added and moved the code to a separate repository with a permissive license. I even had time to throw collectively a colourful brand that represents the spawning and becoming a member of of goroutines earlier than I lastly obtained to board my airplane.

conc logo

Diving into goroutines, concurrency, panics, and scope

With Go, concurrency is already a first-class citizen, so it’s typically a fairly low bar so as to add concurrent code to a Go undertaking. However doing it accurately can nonetheless be exhausting and there are many errors I’ve seen (and made) in concurrent Go code, resembling:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments