Sitemap
Chethan

Chethan

Using errgroup for synchronisation

3 min readSep 2, 2024

Often, we want to ensure the all the concurrent execution that we trigger are completed before proceed further. In such cases, errgroup is what we need. The errgroup allows us to achieve synchronization between goroutines.

There are multiple ways to establish synchronisation. However, errgroup is the most refined and a easiest way to do it.

Before we understand how to achieve synchronisation with errgroup, let us first check different other ways using which we can achieve the same. This way we will understand the advantages of using errgoup, by the time we reach that section.

Using simple channels

Press enter or click to view image in full size

In this above code,

  • The doTask invokes executeCode n number of times as a goroutines.
  • After the method is triggered, the call in the doTask gets blocked at line #7 in the 0th iteration.
  • When one of the many invocations of the executeCode writes a value into the channel, the call blocked at line #7 moves to the next iteration.
  • When the for loop at line #6 reads n number of values from the channel, it exits doTask method

This way you can ensure that all the goroutines that you have triggered finishes before moving to the next part of the code execution.

Using wait group

The wait group is another way to achieve synchronisation in your programming when using goroutines. This approach is also great improvement over what you can achieve in terms of complexity.

Get Chethan’s stories in your inbox

Join Medium for free to get updates from this writer.

Let us modify the above program to use wait group and see how it will look like

Press enter or click to view image in full size

In this case,

  • We define wg which is of type sync.WaitGroup
  • Every time, before we invoke executeCode method, we increment WaitGroup counter by 1. This will inform the wait group about the number of goroutines that it has to wait to finish execution.
  • Once for loop at line #3 finishes execution, the call gets blocked at line #7 until the wait group gets to know that all the executions have finished. Every time the execution of executeCode method finishes, wg.Done() is executed which will reduce the value in waitgroup by 1
  • Once the WaitGroup counter value touches the 0, the call blocked at wg.Wait() statement gets released and code execution continues. If the WaitGroup counter is 5, the wg.Wait() at line #7 will get blocked until 5 goroutines are executed. Calling wg.Done() is important as this is the statement which will reduce the value of WaitGroup counter.

As you can see, this is a significant improvement over the existing channels way of handling the synchronisation. You don’t have to bother of creating, passing and writing into channels. Which usually for me a scary thing.

If you think this was super cool, wait until you meet errgroup.

Using errgroup

errgroup is an improvement over waitgroup, removing some of the statements like Add(), Done(). Along with that, errgroup also provides a flexibility for the user to propagate the error from the any of goroutine back to the caller.

Press enter or click to view image in full size

In the provided code,

  • the `executeCode` function is wrapped inside an anonymous function which is passed to `eg.Go()`. This causes it to be invoked as a goroutine.
  • Internally, the `errgroup` package handles the incrementing of the `WaitGroup` counter by one.
  • When the anonymous function completes its execution, the counter is automatically decremented by one.
  • Once all the goroutines invoked by the `errgroup` have finished executing, `eg.Wait()` unblocks, allowing the program to resume its execution flow.

There you go. Anytime you want to synchronise goroutines, ensure you use errgroup to make use of the best.

--

--