October 2016: Barriers

Meetup #33. Please RSVP at Meetup.

We get started at 6:30 pm.

Sponsors

Venue provided by Startup Edmonton.

Food sponsored by Bellstone Engineering.

Talks

Barrier Pattern in Go (with Context)

Gerrit Renker examined the barrier pattern as a way to fan out work to multiple goroutines and wait for all of them to finish. He began with context.Context — its Done, Err, Deadline, and Value methods, the convention of passing it as the first argument, and how WithCancel/WithDeadline/WithTimeout form nested trees that can cancel whole groups of goroutines. From there he introduced the barrier itself: sync.WaitGroup, the Go equivalent of the classic fork-join pattern, along with its pitfalls (such as a goroutine panicking before calling Done). The second half covered golang.org/x/sync/errgroup, which extends WaitGroup with error capture and cancellation — its Go and Wait methods, how Wait returns the first error and triggers cancel, and why that matters for cancelling sibling work when one task fails. He closed with a worked example spawning stub VMs in parallel.

Key points:

  • context.Context carries cancellation, deadlines, and values down a call tree.
  • Pass ctx as the first argument; derive children with WithCancel/WithTimeout.
  • sync.WaitGroup implements the fork-join barrier pattern.
  • A panic before wg.Done() can leave the barrier stuck.
  • errgroup.Group adds error capture and cancellation to WaitGroup.
  • On first error, errgroup cancels the shared context to stop sibling work.
  • Useful for fan-out tasks like provisioning resources in parallel.

slides

Video: Idiomatic Go Tricks

Mat Ryer (video, slides)

News

Events