Skip to content

Instantly share code, notes, and snippets.

@mmirolim
Created February 20, 2015 16:35
Show Gist options
  • Select an option

  • Save mmirolim/8d81f45362e1508ea5b7 to your computer and use it in GitHub Desktop.

Select an option

Save mmirolim/8d81f45362e1508ea5b7 to your computer and use it in GitHub Desktop.
rate-limiting with waitGroup
// 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