Skip to content

Instantly share code, notes, and snippets.

@lummie
Created May 3, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lummie/6fb1122876e6b908254513c48308dd4b to your computer and use it in GitHub Desktop.
Save lummie/6fb1122876e6b908254513c48308dd4b to your computer and use it in GitHub Desktop.
Simple pattern using a channel to throttle the number of concurrent workers
// change the channel size to the number of concurrent tasks you'd like
var throttle = make(chan struct{}, 1)
func DoStuff() {
// block until we can add to throttle channel
throttle <- struct{}{}
defer func() {
<-throttle // remove from channel when we are done
}()
// do your funky stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment