Created
January 6, 2015 20:40
-
-
Save jmw511/d0feb500ce0a57b8d17c to your computer and use it in GitHub Desktop.
PostGraphRequests function
This file contains 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 (api GraphV2) PostGraphRequests(urls []string) ([]BatchResponse, error) { | |
batchCh := make(chan []BatchResponse) | |
errs := make(chan error) | |
responses := []BatchResponse{} | |
var wg sync.WaitGroup | |
for _, url := range urls { | |
wg.Add(1) | |
go func(url string) { | |
defer wg.Done() | |
resps, err := PostBatchFacebookGraphRequest(url) | |
if err != nil { | |
errs <- err | |
return | |
} | |
batchCh <- resps | |
}(url) | |
} | |
go func() { | |
wg.Wait() | |
close(batchCh) | |
}() | |
for resps := range batchCh { | |
responses = append(responses, resps...) | |
} | |
select { | |
case err := <-errs: | |
return nil, err | |
default: | |
return responses, nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment