-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractors.go
65 lines (58 loc) · 1.52 KB
/
extractors.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
56
57
58
59
60
61
62
63
64
65
// Copyright 2023 cloudeng llc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package crawl
import (
"context"
"cloudeng.io/file/download"
"cloudeng.io/sync/errgroup"
)
type extractorPool struct {
outlinks Outlinks
depth, concurrency int
}
// export this when everything is working.
func newExtractorPool(outlinks Outlinks, depth, concurrency int) *extractorPool {
return &extractorPool{
outlinks: outlinks,
depth: depth,
concurrency: concurrency,
}
}
func (ep *extractorPool) run(ctx context.Context, downloadedCh <-chan download.Downloaded, crawledCh chan<- Crawled) error {
// Extractors return when extractorStop is closed.
var grp errgroup.T
for i := 0; i < ep.concurrency; i++ {
grp.Go(func() error {
ep.runExtractor(ctx, downloadedCh, crawledCh)
return nil
})
}
return grp.Wait()
}
func (ep *extractorPool) runExtractor(ctx context.Context,
downloadedCh <-chan download.Downloaded,
crawledCh chan<- Crawled) {
for {
// Wait for newly downloaded items.
var downloaded download.Downloaded
var ok bool
select {
case <-ctx.Done():
return
case downloaded, ok = <-downloadedCh:
if !ok {
return
}
}
crawled := Crawled{Depth: ep.depth, Downloaded: downloaded}
// Extract outlinks.
extracted := ep.outlinks.Extract(ctx, ep.depth, crawled.Downloaded)
crawled.Outlinks = append(crawled.Outlinks, extracted...)
select {
case <-ctx.Done():
return
case crawledCh <- crawled:
}
}
}