Skip to content

Instantly share code, notes, and snippets.

@TheWaWaR
Last active August 26, 2016 06:27
Show Gist options
  • Select an option

  • Save TheWaWaR/a16c596d7f529abdaa5a to your computer and use it in GitHub Desktop.

Select an option

Save TheWaWaR/a16c596d7f529abdaa5a to your computer and use it in GitHub Desktop.
A simple goroutine worker pool. Ref: http://stackoverflow.com/a/18267889
func worker(linkChan chan string, wg *sync.WaitGroup) {
// Decreasing internal counter for wait-group as soon as goroutine finishes
defer wg.Done()
for url := range linkChan {
// Analyze value and do the job here
}
}
func main() {
lCh := make(chan string)
wg := new(sync.WaitGroup)
// Adding routines to workgroup and running then
for i := 0; i < 250; i++ {
wg.Add(1)
go worker(lCh, wg)
}
// Processing all links by spreading them to `free` goroutines
for _, link := range yourLinksSlice {
lCh <- link
}
// Closing channel (waiting in goroutines won't continue any more)
close(lCh)
// Waiting for all goroutines to finish (otherwise they die as main routine dies)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment