Last active
August 26, 2016 06:27
-
-
Save TheWaWaR/a16c596d7f529abdaa5a to your computer and use it in GitHub Desktop.
A simple goroutine worker pool. Ref: http://stackoverflow.com/a/18267889
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
| 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