6

I'm experimenting with Go channels and have an issue where the simple program below does not terminate.

Essentially I want to make some async HTTP get requests and then wait till they are all finished. I'm using a buffered channel but I'm not sure this is the idiomatic way.

func GetPrice(quotes chan string) {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://some/api", nil)
    req.Header.Set("Accept", "application/json")
    res, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    quotes <- string(body)
}

func main() {
    const max = 3
    quotes := make(chan string, max)
    for i := 0; i < max; i++ {
        go GetPrice(quotes)
    }

    for n := range quotes {
        fmt.Printf("\n%s", n)
    }
}

The program successfully prints 3(max) items

{"price":"1.00"}
{"price":"2.00"}
{"price":"3.00"}

but then blocks and never exits.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ayub Malik
  • 2,488
  • 6
  • 27
  • 42
  • 1
    It never exits, because you haven’t ended the for loop in any way. You indicate that there’s no more values to recieve from a chan by closing it. – JimB Jan 15 '18 at 22:35

3 Answers3

7

sync.WaitGroup can be used here to wait for all goroutines and then closing the quotes channel:

func getPrice(quotes chan<- string, onExit func()) {
    go func() {
        defer onExit()

        req, _ := http.NewRequest("GET", "https://some/api", nil)
        req.Header.Set("Accept", "application/json")

        client := &http.Client{}
        res, err := client.Do(req)
        if err != nil {
            panic(err) // should be handled properly
        }
        defer res.Body.Close()

        body, err := ioutil.ReadAll(res.Body)
        quotes <- string(body)
    }()
}

func main() {
    const max = 3
    var wg sync.WaitGroup

    quotes := make(chan string, max)
    for i := 0; i < max; i++ {
        wg.Add(1)
        getPrice(quotes, func() { wg.Done() })
    }

    go func() {
        defer close(quotes)
        wg.Wait()
    }()

    for n := range quotes {
        fmt.Printf("\n%s", n)
    }
}
Kaveh Shahbazian
  • 13,088
  • 13
  • 80
  • 139
0

Another way that you can do:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func GetPrice(quotes chan string) {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://some/api", nil)
    req.Header.Set("Accept", "application/json")
    res, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    quotes <- string(body)
}

func run(quotes chan string, quit chan bool, max int) {
    for num := range quotes {
        fmt.Println(num)
        max--

        if max == 0 {
            quit <- true
        }
    }
}

func main() {
    const max = 3
    quotes := make(chan string)
    quit := make(chan bool)

    go run(quotes, quit, max)

    for i := 0; i < max; i++ {
        go GetPrice(quotes)
    }

    <-quit
}
0
func GetPrice(quotes chan string) {
    client := &http.Client{}
    req, _ := http.NewRequest("GET", "https://some/api", nil)
    req.Header.Set("Accept", "application/json")
    res, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    quotes <- string(body)
}

func Consumer(chan string){
    for n ,ok:= range quotes {
        if !ok {
             break
        }
        fmt.Printf("\n%s", n)
    }
}

func main() {
    const max = 3
    quotes := make(chan string, max)
    for i := 0; i < max; i++ {
        go GetPrice(quotes)
    }
    go Consumer(quotes)
    close(quotes)
}
Miffa Young
  • 107
  • 4
  • 3
    While this code may answer the question, providing information on how and why it solves the problem improves its long-term value. – L_J Aug 22 '18 at 09:12
  • According to the documentation, higher-level synchronization is better done via channels and communication. It can be a solution for using chans – jibeeeee Nov 03 '20 at 13:49