Easy limiting of concurrency in Go

PUBLISHED ON 2016-09-16

A common occurrence in Go is wanting to do a lot of something, but with some governor on concurrency, there are lots of patterns for this, but Paul Smith had a really clever one…

SOURCE: https://pauladamsmith.com/blog/2016/04/max-clients-go-net-http.html

I can’t even remember how I stumbled onto Paul’s post – but it is just a killer three line way to limit concurrency.

func main() {
    sema := make(chan struct{}, 10) // 10 is max concurrency
    go func() {
        sema <- struct{}{} // mark one as running (will block when full)
        defer func() { <-sema } // mark one as completed

        // Do something, but you only want max 10 of...
comments powered by Disqus