Created
May 3, 2020 13:56
-
-
Save lummie/6fb1122876e6b908254513c48308dd4b to your computer and use it in GitHub Desktop.
Simple pattern using a channel to throttle the number of concurrent workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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