-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.go
63 lines (52 loc) · 1.36 KB
/
resolver.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
package prevalentify
import (
"context"
"sync"
)
// Resolver reads input from the ImageSource, resolves the images and sends them to the returned Image channel.
// Errors are sent to the returned error channel.
type Resolver func(context.Context, <-chan ImageSource) (<-chan Image, <-chan error)
// ResolveFn is a function used to resolve a given ImageSource.
type ResolveFn func(context.Context, ImageSource) (Image, error)
// NewResolverPool returns a pool of resolver workers in the form of a Resolver.
func NewResolverPool(workers int, resolveFn ResolveFn) Resolver {
return func(ctx context.Context, source <-chan ImageSource) (<-chan Image, <-chan error) {
// initialize channels
imageCh := make(chan Image)
errCh := make(chan error)
var wg sync.WaitGroup
wg.Add(workers)
// spawn workers
for i := 0; i < workers; i++ {
go func() {
resolve(ctx, source, imageCh, errCh, resolveFn)
wg.Done()
}()
}
// handle shutdown
go func() {
wg.Wait()
close(imageCh)
close(errCh)
}()
return imageCh, errCh
}
}
func resolve(ctx context.Context, source <-chan ImageSource, out chan Image, errCh chan error, resolveFn ResolveFn) {
for {
select {
case s, ok := <-source:
if !ok {
return
}
img, err := resolveFn(ctx, s)
if err != nil {
errCh <- err
continue
}
out <- img
case <-ctx.Done():
return
}
}
}