-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
55 lines (49 loc) · 1.15 KB
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"net/http"
"github.com/bake/goread/feed"
"golang.org/x/sync/semaphore"
)
type request struct {
cat, url string
}
type response struct {
request
feed *feed.Feed
err error
}
// fetch accepts the number of parallel downloads and returns a request and a
// response channel. The caller is responsible to close the request channel
// after all requests are enqueued, the response chan gets closed automatically.
func fetch(n int64, c *http.Client) (chan<- request, <-chan response) {
sem := semaphore.NewWeighted(n)
ctx := context.Background()
reqc := make(chan request)
resc := make(chan response)
go func() {
defer close(resc)
defer sem.Acquire(ctx, n)
for req := range reqc {
sem.Acquire(ctx, 1)
go func(req request) {
defer sem.Release(1)
feed, err := feed.NewParser(c).ParseURL(req.url)
resc <- response{req, feed, err}
}(req)
}
}()
return reqc, resc
}
func fetchAll(n int64, fs feeds) <-chan response {
reqc, resc := fetch(n, &http.Client{})
go func() {
defer close(reqc)
for cat, urls := range fs {
for _, url := range urls {
reqc <- request{cat, url}
}
}
}()
return resc
}