Created
February 20, 2015 16:35
-
-
Save mmirolim/8d81f45362e1508ea5b7 to your computer and use it in GitHub Desktop.
rate-limiting with waitGroup
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
| // fetch images concurrently with set limits equal to cl | |
| func fetchImages(imgs Images, cl int, put, del chan<- Img, w *sync.WaitGroup) { | |
| log.Printf("Total images to fetch %+v\n", len(imgs.List)) | |
| // loop through all images and retrive them concurrently | |
| for i := 0; i < len(imgs.List); { | |
| w.Add(cl) | |
| for ii := 0; ii < cl; ii++ { | |
| img := imgs.List[i+ii] | |
| go getImg(img, imgs.GetImgName(i+ii), put, del, w) | |
| } | |
| w.Wait() | |
| i += cl | |
| } | |
| } | |
| // get particular image | |
| func getImg(img Img, n string, out, del chan<- Img, w *sync.WaitGroup) { | |
| defer w.Done() | |
| .... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment