-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.go
More file actions
72 lines (62 loc) · 1.35 KB
/
Copy pathbatch.go
File metadata and controls
72 lines (62 loc) · 1.35 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package limpet
import (
"context"
"sync"
)
// GetMany fetches multiple URLs concurrently with the given concurrency limit.
// The callback fn is called for each completed fetch (in arbitrary order) and
// may be called from multiple goroutines concurrently -- callers must ensure
// fn is safe for concurrent use.
// Stops early if ctx is cancelled. Returns the first non-nil error from fn,
// or nil if all callbacks return nil.
func (c *Client) GetMany(
ctx context.Context,
urls []string,
concurrency int,
cfg DoConfig,
fn func(url string, page *Page, err error) error,
) error {
if len(urls) == 0 {
return nil
}
if concurrency < 1 {
concurrency = 1
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
sem := make(chan struct{}, concurrency)
var (
mu sync.Mutex
firstErr error
)
var wg sync.WaitGroup
for _, u := range urls {
// Check for early exit before launching more work.
if ctx.Err() != nil {
break
}
select {
case sem <- struct{}{}:
case <-ctx.Done():
}
if ctx.Err() != nil {
break
}
wg.Add(1)
go func(url string) {
defer wg.Done()
defer func() { <-sem }()
page, fetchErr := c.Get(ctx, url, cfg)
if cbErr := fn(url, page, fetchErr); cbErr != nil {
mu.Lock()
if firstErr == nil {
firstErr = cbErr
}
mu.Unlock()
cancel()
}
}(u)
}
wg.Wait()
return firstErr
}