Skip to content

Instantly share code, notes, and snippets.

@mattetti
Last active August 21, 2024 05:26
Show Gist options
  • Save mattetti/3798173 to your computer and use it in GitHub Desktop.
Save mattetti/3798173 to your computer and use it in GitHub Desktop.

Revisions

  1. mattetti revised this gist Feb 10, 2020. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions gistfile1.go
    Original 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 {
    fmt.Printf("Failed to fetch %s\n", err)
    return
    if err == nil {
    resp.Body.Close()
    }
    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)
    }
    }
    }
  2. mattetti revised this gist Feb 8, 2020. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions gistfile1.go
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,9 @@ import (
    )

    var urls = []string{
    "http://pulsoconf.co/",
    "http://golang.org/",
    "http://matt.aimonetti.net/",
    "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)
    }
    }
    }
  3. mattetti revised this gist Nov 28, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.go
    Original 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)
    }
  4. mattetti revised this gist Nov 28, 2012. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions gistfile1.go
    Original 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
    }
    default:
    case <-time.After(50 * time.Millisecond):
    fmt.Printf(".")
    time.Sleep(5e7)
    }
    }

    return responses

    }

    func main() {
    results := asyncHttpGets(urls)
    for _, results := range results {
    fmt.Printf("%s status: %s\n", result.url, result.response.Status)
    for _, result := range results {
    fmt.Printf("%s status: %s\n", result.url,
    result.response.Status)
    }
    }
  5. mattetti revised this gist Sep 29, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.go
    Original 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", response.url, result.response.Status)
    fmt.Printf("%s status: %s\n", result.url, result.response.Status)
    }
    }
  6. mattetti revised this gist Sep 28, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.go
    Original file line number Diff line number Diff line change
    @@ -48,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)
    for _, results := range results {
    fmt.Printf("%s status: %s\n", response.url, result.response.Status)
    }
    }
  7. mattetti revised this gist Sep 28, 2012. 1 changed file with 16 additions and 18 deletions.
    34 changes: 16 additions & 18 deletions gistfile1.go
    Original 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
    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)
    }
    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 {
    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)
    }
    for _, response := range results {
    fmt.Printf("%s status: %s\n", response.url, response.response.Status)
    }
    }
  8. mattetti revised this gist Sep 28, 2012. 1 changed file with 26 additions and 50 deletions.
    76 changes: 26 additions & 50 deletions gistfile1.go
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,6 @@ package main

    import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "time"
    )
    @@ -14,67 +12,45 @@ var urls = []string{
    "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 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}
    }()
    type HttpResponse struct {
    url string
    response *http.Response
    err error
    }

    func asyncFetch(urls []string) map[string]string {
    urlStatus := make(map[string]string)
    status := make(chan State)

    for _, url := range urls {
    fetchUrl(url, status)
    }
    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 {
    for {
    select {
    case s := <-status:
    fmt.Printf("%s is ready\n", s.url)
    urlStatus[s.url] = s.status
    if len(urlStatus) == 3 {
    return urlStatus
    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)
    }
    }
    fmt.Println("done")
    return urlStatus
    return responses

    }

    func main() {
    results := asyncFetch(urls)
    fmt.Printf("Asynchronously loaded %v urls\n", len(results))
    results := asyncHttpGets(urls)
    for _, response := range results {
    fmt.Printf("%s status: %s\n", response.url, response.response.Status)
    }
    }
  9. mattetti revised this gist Sep 28, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.go
    Original 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))
  10. mattetti revised this gist Sep 28, 2012. 1 changed file with 17 additions and 17 deletions.
    34 changes: 17 additions & 17 deletions gistfile1.go
    Original file line number Diff line number Diff line change
    @@ -40,8 +40,23 @@ func (r *Resource) Fetch() string {
    return string(body)
    }

    func monitor(status chan State) map[string]string {
    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 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)
    results := asyncFetch(urls)
    fmt.Printf("Asynchronously loaded %v urls\n", len(results))
    }
  11. mattetti created this gist Sep 28, 2012.
    79 changes: 79 additions & 0 deletions gistfile1.go
    Original 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))
    }