Golang Conc: Building Better Structured Concurrency

Author

Reads 1.1K

Adult male programmer working on code at a modern desk setup with a large monitor.
Credit: pexels.com, Adult male programmer working on code at a modern desk setup with a large monitor.

Golang's concurrency model is all about building scalable and responsive systems. It's designed to handle a high volume of tasks concurrently, making it a popular choice for building cloud-native applications.

The key to structured concurrency in Golang is to use goroutines and channels effectively. This allows you to write concurrent code that's easy to reason about and debug.

Goroutines are lightweight threads that can run concurrently with the main program. They're created using the `go` keyword and can be used to execute tasks in parallel.

In Golang, channels are used to communicate between goroutines. They're a way to send and receive data between tasks, ensuring that data is processed in a predictable and reliable way.

Readers also liked: Golang Concurrency Patterns

Goals and Best Practices

The main goals of the conc package are to make it harder to leak goroutines, handle panics gracefully, and make concurrent code easier to read.

The package's creator wants it to be hard to use incorrectly, which is a noble goal considering the notorious headaches that concurrent code can cause. This goal might mean leaving out some features that could be useful, but would make the package more prone to misuse.

Credit: youtube.com, #67 Golang - Effortless Concurrency: The conc package

One of the key features that the creator has chosen not to include is channels, which could have been a valuable addition but would have compromised the goal of being hard to use incorrectly.

Here are the main goals of the conc package in bullet points:

  1. Make it harder to leak goroutines
  2. Handle panics gracefully
  3. Make concurrent code easier to read

Improving Code Quality

Making concurrency easier to read is a key goal of the conc package. By abstracting boilerplate complexity, developers can focus on the logic of their code.

The conc package provides higher-level abstractions for common concurrency operations, such as running concurrent tasks with a bounded set of goroutines. This is achieved through the pool.New() function.

Here's a comparison of the conc package's approach to concurrency with the standard library's approach:

By using the conc package, developers can write more readable and maintainable code, without sacrificing performance.

Improve Code Readability

Improving code readability is crucial for maintaining a codebase over time. It's like writing a clear recipe for others to follow.

Credit: youtube.com, What Are the Best Code Refactoring Strategies for Improving Quality and Maintainability?

Concurrent code can be particularly tricky to read, but there are tools that can help simplify it. The conc package, for example, abstracts away boilerplate complexity.

To run concurrent tasks with a bounded set of goroutines, you can use pool.New(). This is a much simpler and more readable way to do it than writing the code from scratch.

Here's a comparison of using the conc package versus writing concurrent code by hand:

By using these tools, you can write more readable and maintainable code. It's a small change that can make a big difference in the long run.

ForEach Added in 0.2.0

The Iterator type was added in version 0.2.0, and it's safe to use with reasonable defaults, making it a great addition to your code.

It's also safe for reuse and concurrent use, which means you can use it in multiple places in your code without worrying about it causing any issues.

Worth a look: S Golang

A programmer sits thoughtfully at his desk with computer screens and office setups, reflecting modern workplace dynamics.
Credit: pexels.com, A programmer sits thoughtfully at his desk with computer screens and office setups, reflecting modern workplace dynamics.

The ForEach function, which is part of the Iterator type, executes a function in parallel over each element in an input, using up to the Iterator's configured maximum number of goroutines.

This can save a lot of time, especially when working with large datasets, as it takes roughly 2µs to start up the goroutines.

ForEach also adds an overhead of roughly 50ns per element of input, so keep that in mind when deciding whether to use it.

The Map function, which is part of the Mapper type, also uses up to the configured Mapper's maximum number of goroutines, just like ForEach.

Goroutines and Scope

Goroutines should have an owner, which is responsible for ensuring they exit properly. This is the opinion taken by conc, which scopes concurrency to prevent leaks.

A common mistake is failing to properly wait for goroutines to complete, making it hard to clean them up. conc uses a conc.WaitGroup as the owner of a goroutine, which should always be used with (*WaitGroup).Go() and (*WaitGroup).Wait().

In cases where a spawned goroutine needs to outlast the scope of the caller, a WaitGroup can be passed into the spawning function.

Related reading: Golang Waiting Group

Goroutines, Panics, and Scope

Credit: youtube.com, Go Concurrency Explained: Go Routines & Channels

Goroutines can be a pain to clean up, especially when they're not properly waited for to exit.

A common mistake is failing to properly wait for a goroutine to complete, which can lead to issues like cleaning up resources correctly.

To avoid this, conc takes the approach that all concurrency should be scoped, meaning goroutines should have an owner that ensures they exit properly.

In conc, the owner of a goroutine is always a conc.WaitGroup, and goroutines are spawned in a WaitGroup with (*WaitGroup).Go().

Here are some common mistakes to watch out for in concurrent Go code:

  • Not cleaning up resources correctly.
  • Causing deadlocks.
  • Crashing the entire program because of a panic in a single goroutine.

Sometimes you want your entire program to crash if one goroutine panics, but in many cases, you don't.

Map

The Map function is a powerful tool in Go programming. It applies a given function f to each element of an input, returning the mapped result.

Map can be used with a configurable goroutine limit by creating a custom Mapper. This allows for more control over the number of goroutines used.

Credit: youtube.com, Goroutines: Under the Hood | Vicki Niu | Go Systems Conf SF 2020

The Map function always uses at most the number of goroutines equal to the runtime.GOMAXPROCS value. This is a built-in value in Go that determines the maximum number of goroutines that can run concurrently.

By using a custom Mapper, you can configure the maximum number of goroutines used by the Map function. This can be useful in certain situations where you need more control over goroutine usage.

Predictable Code Flow

Predictable code flow is crucial when working with goroutines. It's easy to get lost in the complexity of concurrent code, but there's a clever solution that can help keep things organized.

The nursery approach is a great way to achieve predictable code flow. It scopes routines to a block that returns when all goroutines are done, keeping code flow sequential outside the block.

This approach is an extension to context.Context, allowing you to spawn routines in a predictable way. You can use it to create a simple yet effective way to manage concurrent code.

Credit: youtube.com, Concurrency is not Parallelism by Rob Pike

Here's an example of how you can use a nursery to achieve predictable code flow:

```markdown

It is a simple extension to context.Context that allows spawning routines.

```

By using a nursery, you can ensure that your code flow remains predictable, even when working with concurrent routines. This is especially useful when debugging or trying to understand complex concurrent code.

Here's a quick comparison of how the nursery approach can simplify your code:

Note that the conc package provides a more concise way to achieve predictable code flow, making it easier to write and maintain concurrent code.

Parallel Processing

Parallel Processing with conc is a game-changer. It makes it easier to write concurrent code that's efficient and safe.

Using conc's pool and iter, you can simplify concurrent processing and avoid boilerplate code. This is especially useful when making multiple network requests, as shown in the example of fetching last names over a network.

Conc's goals include making it harder to leak goroutines, handling panics gracefully, and making concurrent code easier to read. These goals are achieved through features like conc's Stream package, which abstracts away complexity for parallel stream processing.

Credit: youtube.com, Golang Concurrency - All the Basics you have to know!

Here are the three key benefits of using conc for parallel stream processing:

  • Do the network requests in parallel.
  • Show the results to the user as soon as possible.
  • Maintain the order of the stream.

Conc also provides a flexible way to execute functions in parallel, with options like ForEach and Iterator. These functions are safe to use and can be customized to fit your needs.

Parallel Stream Processing

Parallel stream processing is a powerful technique for handling large datasets. It allows you to process elements in parallel, which can significantly speed up your code.

To achieve parallel stream processing, you can use libraries like conc. At Sourcegraph, they use conc to process ordered streams in parallel, which helps them show results to users as soon as possible.

Maintaining the order of the stream is crucial, especially when ranking results. To do this safely and efficiently, conc's Stream package abstracts away complexity.

Here are some key benefits of using conc for parallel stream processing:

  • Network requests can be done in parallel.
  • Results can be shown to the user as soon as possible.
  • The order of the stream is maintained.

The conc library also provides a function called ForEach, which executes a function in parallel over each element in an input. This is safe to mutate the input parameter, making it possible to map in place.

The ForEach function always uses at most runtime.GOMAXPROCS goroutines, which adds an overhead of roughly 50ns per element of input. For a configurable goroutine limit, you can use a custom Iterator.

Simpler Processing with Pool and Iter

Credit: youtube.com, AMD Simplified: Serial vs. Parallel Computing

Parallel processing can be a real game-changer for tasks that involve multiple network requests or computations.

One common pattern in parallel processing is to do a bunch of stuff in parallel and then collect the results into a single data structure. For example, making multiple network requests without being blocked by the total latency for all round trips.

Using Go's standard library, you'd usually create a sync.WaitGroup and a deferred function, which requires a bunch of boilerplate code. This is where conc comes in, making unnecessary all that boilerplate code.

Conc's pool can be used to efficiently fetch the last name for each first name, given a list of first names. This is done by making all the network calls at once and then waiting until the last one has completed.

The conc library's pool and iter features make concurrent code easier to read and handle panics gracefully. This is especially useful when dealing with multiple network requests or computations.

Photography of Pool Near Fence
Credit: pexels.com, Photography of Pool Near Fence

Here are conc's three main goals, which make it a valuable tool for parallel processing:

  • Making it harder to leak goroutines.
  • Handling panics gracefully.
  • Making concurrent code easier to read.

Conc's Iterator can be used to configure the behavior of ForEach and ForEachIdx, with a zero value that's safe to use with reasonable defaults. It's also safe for reuse and concurrent use.

API Design and Implementation

The API design of conc is focused on being natural and unsurprising, with the goal of making it hard to use incorrectly. The author of conc aimed to extend this goal by reducing the feature set to keep it simple and safe.

The author found that many concurrency implementations suffer from problems such as feeling unnatural to use, containing foot guns, making it hard to understand what's happening under the hood, or containing surprising implementation details.

To address these issues, the author removed the limit of goroutines to the number of cores available, which was considered somewhat arbitrary and surprising for most users. This change aligns with the principle of least surprise.

API Design: Natural and Unsurprising

Credit: youtube.com, API Design by Carson Gross ~ BSDC 2025

A natural API feels intuitive to use, like an extension of the language itself. It's surprising how many APIs fall short of this goal.

Most concurrency implementations suffer from at least one of the following problems: they feel unnatural to use, contain foot guns, make it hard to understand what's happening under the hood, or contain surprising implementation details.

APIs should follow the principle of least surprise, which means making it easy for users to understand what's happening and reducing the number of unexpected surprises.

The author of the conc library reduced its feature set to keep it simple and safe, a good approach to ensuring a natural API.

Sourcegraph uses GO_MAX_PROCS to limit goroutines to the number of cores available, but this can be an arbitrary limit. By removing this limit, the author made the API more natural and unsurprising.

APIs should prioritize simplicity and safety over feature richness, especially when it comes to concurrency.

Worth a look: Golang Concurrency

Release to the World

Credit: youtube.com, Good APIs Vs Bad APIs: 7 Tips for API Design

Releasing your API to the world can be a thrilling experience.

Making your GitHub repository public can lead to a rapid spread of your work, as seen with the conc project.

Posting about your API internally can spark interest among your coworkers, but it may not be enough to generate significant attention.

Sharing your work on platforms like Reddit can lead to positive attention and discussion, as conc did.

Receiving feedback from the public can be invaluable, and it's essential to be open to making tweaks based on that feedback.

Joining a community like the Sourcegraph Discord server can provide a platform for further discussion and collaboration.

Performance and Optimization

Spawning a large number of goroutines can have a significant impact on performance. An operation that spawns 100 goroutines is a good example of this.

Goroutines are lightweight threads that can run concurrently, making them a great way to improve performance in Go programs. This is because they don't require the same level of overhead as threads in other languages.

However, spawning too many goroutines at once can lead to performance issues. In the example of spawning 100 goroutines, this can cause problems.

To optimize performance, it's essential to balance the number of goroutines with the available resources. This ensures that the goroutines run efficiently without causing performance issues.

Broaden your view: Example Golang

Error Handling and Recovery

Credit: youtube.com, #27 Golang - Error Handling - Understanding Panic and Recover

Error handling and recovery is a crucial aspect of writing concurrent code in Go. Goroutines can panic, and if not handled properly, this can bring down the entire process.

Ignoring panics is a bad idea because it usually means there's something wrong that needs to be fixed. Logging panics isn't great either, as it doesn't give the spawner any indication that something went wrong.

There are a few options for handling panics, including ignoring them, logging them, turning them into an error and returning that to the goroutine spawner, or propagating the panic to the spawner. However, ignoring panics or just logging them isn't ideal.

Here are the options for handling panics:

The conc package handles this for you, making it easier to write concurrent code.

Internal Library and Reusability

Solving the same problem many times can be frustrating, but it can also lead to the creation of a valuable internal library.

The author of the concurrency library has found themselves writing similar code to handle common concurrency patterns and panics in Go, which is a common issue in many Sourcegraph projects that rely heavily on concurrent Go code.

Credit: youtube.com, GopherCon 2018: Rethinking Classical Concurrency Patterns - Bryan C. Mills

Writing separate functions for different data types was a necessary evil before the introduction of generics in Go 1.18. However, with generics now available, it's easy to operate over whatever data types are present in a consistent way.

Internally, the author's team has used the concurrency library extensively, but it's now being released as an open-source package for other developers and teams to use too.

Iterator added in 0.2.0

Iterator added in 0.2.0 is a significant improvement that makes concurrent code easier to read and write. It's a type that can be used to configure the behaviour of ForEach and ForEachIdx.

The zero value of Iterator is safe to use with reasonable defaults, which is a huge time-saver. This means you can start using it without having to create a new instance every time.

Iterator is also safe for reuse and concurrent use, making it a valuable addition to your concurrent programming toolkit. This is especially useful when you need to make multiple network requests at once, like fetching the last name for each first name in a list.

Broaden your view: Golang Iterator

Vibrant close-up of a computer screen displaying color-coded programming code.
Credit: pexels.com, Vibrant close-up of a computer screen displaying color-coded programming code.

Here's a quick rundown of the benefits of using Iterator:

By using Iterator, you can simplify your concurrent code and make it easier to read and maintain. This is just one of the many ways that the conc library makes concurrent programming more efficient and enjoyable.

Building an Internal Library to Avoid Repeating Solutions

Writing similar code to handle common concurrency patterns and panics in Go is a frustrating experience.

In many Sourcegraph projects, concurrent Go code is used to efficiently search billions of lines of code.

The Go 1.18 release in March 2022 introduced generics, which can fix a lot of the frustrations and make a concurrency library more succinct.

Different functions were needed for different data types, such as strings and tasks, before generics were available.

Around six months ago, an internal library was started to generalize how goroutines are used.

The library was used extensively internally before being cleaned up, documented, and released as an open-source package.

Flight delays across the US in December provided an opportunity to clean up the code and document everything.

A colorful logo representing the spawning and joining of goroutines was even created during this time.

Jeannie Larson

Senior Assigning Editor

Jeannie Larson is a seasoned Assigning Editor with a keen eye for compelling content. With a passion for storytelling, she has curated articles on a wide range of topics, from technology to lifestyle. Jeannie's expertise lies in assigning and editing articles that resonate with diverse audiences.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.