-
-
Save emailnjv/2f93c97a525235348dc293796ea2c711 to your computer and use it in GitHub Desktop.
Concurrent HTTP GET Response generator
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
// RespGen :: Concurrent HTTP GET Response generator | |
func RespGen(urls ...string) <-chan *http.Response { | |
var wg sync.WaitGroup | |
out := make(chan *http.Response) | |
wg.Add(len(urls)) | |
for _, url := range urls { | |
go func(url string) { | |
req, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
panic(err) | |
} | |
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0") | |
time.Sleep(3 * time.Second) | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
out <- resp | |
wg.Done() | |
}(url) | |
} | |
go func() { | |
wg.Wait() | |
close(out) | |
}() | |
return out | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment