Last active
August 21, 2024 05:26
-
-
Save mattetti/3798173 to your computer and use it in GitHub Desktop.
Revisions
-
mattetti revised this gist
Feb 10, 2020 . 1 changed file with 8 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -25,11 +25,9 @@ func asyncHttpGets(urls []string) []*HttpResponse { go func(url string) { fmt.Printf("Fetching %s \n", url) resp, err := http.Get(url) if err == nil { resp.Body.Close() } ch <- &HttpResponse{url, resp, err} }(url) } @@ -54,7 +52,12 @@ func asyncHttpGets(urls []string) []*HttpResponse { func main() { results := asyncHttpGets(urls) for _, result := range results { if result.err != nil { fmt.Printf("%s error: %v\n", result.url, result.err) continue } fmt.Printf("%s status: %s\n", result.url, result.response.Status) } } -
mattetti revised this gist
Feb 8, 2020 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,9 +7,9 @@ import ( ) var urls = []string{ "https://splice.com/", "https://golang.org/", "https://matt.aimonetti.net/", } type HttpResponse struct { @@ -25,6 +25,10 @@ func asyncHttpGets(urls []string) []*HttpResponse { go func(url string) { fmt.Printf("Fetching %s \n", url) resp, err := http.Get(url) if err != nil { fmt.Printf("Failed to fetch %s\n", err) return } resp.Body.Close() ch <- &HttpResponse{url, resp, err} }(url) @@ -53,4 +57,4 @@ func main() { fmt.Printf("%s status: %s\n", result.url, result.response.Status) } } -
mattetti revised this gist
Nov 28, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -25,6 +25,7 @@ func asyncHttpGets(urls []string) []*HttpResponse { go func(url string) { fmt.Printf("Fetching %s \n", url) resp, err := http.Get(url) resp.Body.Close() ch <- &HttpResponse{url, resp, err} }(url) } -
mattetti revised this gist
Nov 28, 2012 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -37,18 +37,19 @@ func asyncHttpGets(urls []string) []*HttpResponse { if len(responses) == len(urls) { return responses } case <-time.After(50 * time.Millisecond): fmt.Printf(".") } } return responses } func main() { results := asyncHttpGets(urls) for _, result := range results { fmt.Printf("%s status: %s\n", result.url, result.response.Status) } } -
mattetti revised this gist
Sep 29, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -49,6 +49,6 @@ func asyncHttpGets(urls []string) []*HttpResponse { func main() { results := asyncHttpGets(urls) for _, results := range results { fmt.Printf("%s status: %s\n", result.url, result.response.Status) } } -
mattetti revised this gist
Sep 28, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -48,7 +48,7 @@ func asyncHttpGets(urls []string) []*HttpResponse { func main() { results := asyncHttpGets(urls) for _, results := range results { fmt.Printf("%s status: %s\n", response.url, result.response.Status) } } -
mattetti revised this gist
Sep 28, 2012 . 1 changed file with 16 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,26 +12,24 @@ var urls = []string{ "http://matt.aimonetti.net/", } type HttpResponse struct { url string response *http.Response err error } func asyncHttpGets(urls []string) []*HttpResponse { ch := make(chan *HttpResponse, len(urls)) // buffered responses := []*HttpResponse{} for _, url := range urls { go func(url string) { fmt.Printf("Fetching %s \n", url) resp, err := http.Get(url) ch <- &HttpResponse{url, resp, err} }(url) } for { select { case r := <-ch: fmt.Printf("%s was fetched\n", r.url) @@ -50,7 +48,7 @@ func asyncHttpGets(urls []string) []*HttpResponse { func main() { results := asyncHttpGets(urls) for _, response := range results { fmt.Printf("%s status: %s\n", response.url, response.response.Status) } } -
mattetti revised this gist
Sep 28, 2012 . 1 changed file with 26 additions and 50 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,8 +2,6 @@ package main import ( "fmt" "net/http" "time" ) @@ -14,67 +12,45 @@ var urls = []string{ "http://matt.aimonetti.net/", } type HttpResponse struct { url string response *http.Response err error } func asyncHttpGets(urls []string) []*HttpResponse { ch := make(chan *HttpResponse, len(urls)) // buffered responses := []*HttpResponse{} for _, url := range urls { go func(url string) { fmt.Printf("Fetching %s \n", url) resp, err := http.Get(url) ch <- &HttpResponse{url, resp, err} }(url) } for { select { case r := <-ch: fmt.Printf("%s was fetched\n", r.url) responses = append(responses, r) if len(responses) == len(urls) { return responses } default: fmt.Printf(".") time.Sleep(5e7) } } return responses } func main() { results := asyncHttpGets(urls) for _, response := range results { fmt.Printf("%s status: %s\n", response.url, response.response.Status) } } -
mattetti revised this gist
Sep 28, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -73,6 +73,7 @@ func asyncFetch(urls []string) map[string]string { fmt.Println("done") return urlStatus } func main() { results := asyncFetch(urls) fmt.Printf("Asynchronously loaded %v urls\n", len(results)) -
mattetti revised this gist
Sep 28, 2012 . 1 changed file with 17 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -40,8 +40,23 @@ func (r *Resource) Fetch() string { return string(body) } func fetchUrl(url string, status chan State) { go func() { fmt.Printf("added %s to the pending queue\n", url) r := &Resource{url: url} s := r.Fetch() status <- State{r.url, s} }() } func asyncFetch(urls []string) map[string]string { urlStatus := make(map[string]string) status := make(chan State) for _, url := range urls { fetchUrl(url, status) } for { select { case s := <-status: @@ -58,22 +73,7 @@ func monitor(status chan State) map[string]string { fmt.Println("done") return urlStatus } func main() { results := asyncFetch(urls) fmt.Printf("Asynchronously loaded %v urls\n", len(results)) } -
mattetti created this gist
Sep 28, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ package main import ( "fmt" "io/ioutil" "log" "net/http" "time" ) var urls = []string{ "http://pulsoconf.co/", "http://golang.org/", "http://matt.aimonetti.net/", } // Resource represents an HTTP URL to be fetched. type Resource struct { url string errCount int } // State represents the last-known state of a fetch. type State struct { url string status string } func (r *Resource) Fetch() string { fmt.Printf("fetching %s\n", r.url) resp, err := http.Get(r.url) if err != nil { log.Println("Error", r.url, err) r.errCount++ return err.Error() } r.errCount = 0 defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) return string(body) } func monitor(status chan State) map[string]string { urlStatus := make(map[string]string) for { select { case s := <-status: fmt.Printf("%s is ready\n", s.url) urlStatus[s.url] = s.status if len(urlStatus) == 3 { return urlStatus } default: fmt.Printf(".") time.Sleep(5e7) } } fmt.Println("done") return urlStatus } func fetchUrl(url string, status chan State) { go func() { fmt.Printf("added %s to the pending queue\n", url) r := &Resource{url: url} s := r.Fetch() status <- State{r.url, s} }() } func main() { status := make(chan State) for _, url := range urls { fetchUrl(url, status) } results := monitor(status) fmt.Printf("Asynchronously loaded %v urls\n", len(results)) }