Skip to content

Instantly share code, notes, and snippets.

@macalinao
Created December 3, 2016 00:10
Show Gist options
  • Save macalinao/7aebba50c4ee28a60c3c57ab0f12ca9b to your computer and use it in GitHub Desktop.
Save macalinao/7aebba50c4ee28a60c3c57ab0f12ca9b to your computer and use it in GitHub Desktop.
func combine(queries []Query) Sum {
  var sum Sum

  var wg sync.WaitGroup
  sumsChan := make(chan Sum)

  // Limit to 10 concurrent requests
  limiter := make(chan struct{}, 10)

  // Fetch sum for each query
  for _, query := range queries {
    wg.Add(1)
    go func(query Query) {
      // Rate limiting
      limiter <- struct{}{}

      // Push to the sums channel
      sumsChan <- fetch(query)

      wg.Done()
      <-limiter
    }(sum)
  }

  // Add the sums as they are fetched
  go func() {
    for sumEl := range sumsChan {
      sum = add(sum, sumEl)
    }
  }()

  // Wait for all of the wg to be done being fetched
  wg.Wait()
  close(sum)
  close(limiter)

  return sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment