The objective of this program is to call enqueue sequentially with multiple URLs, and never block, fetching the URLs asynchronously, but on the order they were entered.
In other words: keep calling enqueue to add urls to a buffered channel (queue
) (possibly with a specific callback), and consume one by one in the order they where entered in the queue. I used WaitGroup
to prevent the go program from immediately exiting, but I feel I'm not taking advantage of the buffered channel at all.
package main
import (
"fmt"
"net/http"
"sync"
)
type callback func(url string, res *http.Response, err error)
type callbackRequest struct {
cb callback
url string
}
var queue = make(chan callbackRequest, 1)
var wg sync.WaitGroup
func worker() {
for {
req := <-queue
fmt.Println("processing", req.url)
res, err := http.Get(req.url)
req.cb(req.url, res, err)
}
}
func enqueue(url string) {
fmt.Println("enqueue", url)
cb := func(url string, res *http.Response, err error) {
if err != nil {
fmt.Println("error")
}
fmt.Println(url, res.Status)
wg.Done()
}
cbReq := callbackRequest{url: url, cb: cb}
wg.Add(1)
go func(cbr callbackRequest) {
queue <- cbReq
}(cbReq)
}
func main() {
go worker()
enqueue("http://google.com")
enqueue("http://reddit.com")
enqueue("http://yahoo.com")
enqueue("http://bing.com")
enqueue("http://google.com")
enqueue("http://yahoo.com")
enqueue("http://reddit.com")
enqueue("http://bing.com")
enqueue("http://google.com")
wg.Wait()
}
Output:
$ go run enqueue/main.go enqueue http://google.com enqueue http://reddit.com enqueue http://yahoo.com enqueue http://bing.com enqueue http://google.com enqueue http://yahoo.com enqueue http://reddit.com enqueue http://bing.com enqueue http://google.com processing http://google.com http://google.com 200 OK processing http://reddit.com http://reddit.com 200 OK processing http://yahoo.com http://yahoo.com 200 OK processing http://bing.com http://bing.com 200 OK processing http://google.com http://google.com 200 OK processing http://yahoo.com http://yahoo.com 504 Gateway Timeout processing http://reddit.com http://reddit.com 200 OK processing http://bing.com http://bing.com 200 OK processing http://google.com http://google.com 200 OK