2018-01-24 09:24:53 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-01-24 10:18:41 +03:00
|
|
|
"fmt"
|
2018-05-02 13:23:43 +03:00
|
|
|
"net/http"
|
2018-01-24 09:24:53 +03:00
|
|
|
"time"
|
|
|
|
|
2018-05-02 14:53:08 +03:00
|
|
|
"sync"
|
|
|
|
|
2018-01-24 09:24:53 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FetchResult struct {
|
|
|
|
Domain string
|
|
|
|
StatusCode uint
|
|
|
|
Header string
|
|
|
|
}
|
|
|
|
|
|
|
|
func dummyFetchUrl(url string) *FetchResult {
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
return &FetchResult{
|
|
|
|
url,
|
|
|
|
200,
|
|
|
|
"Dummy Header",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 10:18:41 +03:00
|
|
|
func concurrentFetch(urls []string) <-chan *FetchResult {
|
|
|
|
rChan := make(chan *FetchResult, len(urls))
|
|
|
|
for _, url := range urls {
|
|
|
|
go func(url string) {
|
|
|
|
result := dummyFetchUrl(url)
|
|
|
|
rChan <- result
|
|
|
|
}(url)
|
|
|
|
}
|
|
|
|
return rChan
|
|
|
|
}
|
|
|
|
|
2018-01-24 09:24:53 +03:00
|
|
|
func main() {
|
2018-05-02 13:23:43 +03:00
|
|
|
v2()
|
|
|
|
return
|
2018-01-24 10:18:41 +03:00
|
|
|
// mock the urls
|
|
|
|
urls := []string{
|
|
|
|
"test1",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
"test4",
|
|
|
|
"test5",
|
|
|
|
"test6",
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a prefered method
|
|
|
|
chanResults := concurrentFetch(urls)
|
|
|
|
for i := 0; i < len(urls); i++ {
|
|
|
|
select {
|
|
|
|
case c := <-chanResults:
|
|
|
|
spew.Dump(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// i := 0
|
|
|
|
// for v := range concurrentFetch(urls) {
|
|
|
|
// spew.Dump(i, v)
|
|
|
|
// i++
|
|
|
|
// if i == len(urls) {
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
fmt.Println("All my things are done")
|
2018-01-24 09:24:53 +03:00
|
|
|
}
|
2018-05-02 13:23:43 +03:00
|
|
|
|
|
|
|
type Result struct {
|
|
|
|
Error error
|
|
|
|
Response *http.Response
|
|
|
|
}
|
|
|
|
|
|
|
|
func v2() {
|
|
|
|
checkStatus := func(done <-chan interface{}, urls ...string) <-chan Result {
|
|
|
|
results := make(chan Result)
|
|
|
|
go func() {
|
|
|
|
defer close(results)
|
2018-05-02 14:53:08 +03:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(urls))
|
2018-05-02 13:23:43 +03:00
|
|
|
for _, url := range urls {
|
2018-05-02 14:53:08 +03:00
|
|
|
go func(url string) {
|
|
|
|
defer wg.Done()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
println("EXECUTE : ", url)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
results <- Result{Error: err, Response: resp}
|
|
|
|
}
|
|
|
|
}(url)
|
2018-05-02 13:23:43 +03:00
|
|
|
}
|
2018-05-02 14:53:08 +03:00
|
|
|
wg.Wait()
|
2018-05-02 13:23:43 +03:00
|
|
|
}()
|
|
|
|
return results
|
|
|
|
}
|
2018-05-02 14:53:08 +03:00
|
|
|
done := make(chan interface{}, 1)
|
2018-05-02 13:42:40 +03:00
|
|
|
defer close(done)
|
|
|
|
urls := []string{
|
|
|
|
"https://www.google.com",
|
|
|
|
"https://blog.golang.org/pipelines",
|
|
|
|
"https://github.com/jianhan",
|
|
|
|
"https://insights.stackoverflow.com/survey/2017",
|
|
|
|
"https://hackernoon.com/top-10-python-web-frameworks-to-learn-in-2018-b2ebab969d1a",
|
2018-05-02 14:53:08 +03:00
|
|
|
"https://blog.kowalczyk.info/article/1Bkr/3-ways-to-iterate-in-go.html",
|
2018-05-02 13:42:40 +03:00
|
|
|
}
|
2018-05-02 13:23:43 +03:00
|
|
|
for result := range checkStatus(done, urls...) {
|
|
|
|
if result.Error != nil {
|
2018-05-02 13:42:40 +03:00
|
|
|
done <- true
|
2018-05-02 13:23:43 +03:00
|
|
|
fmt.Printf("error: %v\n", result.Error)
|
2018-05-02 13:42:40 +03:00
|
|
|
break
|
2018-05-02 13:23:43 +03:00
|
|
|
}
|
|
|
|
fmt.Printf("Response: %v\n", result.Response.Status)
|
|
|
|
}
|
2018-05-02 13:42:40 +03:00
|
|
|
fmt.Println("End")
|
2018-05-02 13:23:43 +03:00
|
|
|
}
|