Skip to content

Instantly share code, notes, and snippets.

@eslam-mahmoud
Last active February 18, 2019 16:23
Show Gist options
  • Save eslam-mahmoud/0dfcc41a11d6ffbbd68121a05a3414ad to your computer and use it in GitHub Desktop.
Save eslam-mahmoud/0dfcc41a11d6ffbbd68121a05a3414ad to your computer and use it in GitHub Desktop.
create 50 worker pool what will do Async get request to URL and print result
package main
import (
"fmt"
"sync"
"strconv"
"net/http"
"io/ioutil"
)
var wg sync.WaitGroup
func worker(id int, jobs <-chan int) {
defer wg.Done()
for j := range jobs {
url := "https://api.example.com/products/"+strconv.Itoa(j)
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "Bearer token")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// fmt.Println("response Status:", resp.Status)
// fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
}
func main() {
jobs := make(chan int)
for w := 1; w <= 50; w++ {
wg.Add(1)
go worker(w, jobs)
}
for j := 8600000; j < 8680000; j++ {
jobs <- j
}
close(jobs)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment