-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (76 loc) · 1.85 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"fmt"
"math/rand"
"time"
pp "github.com/sonalys/pipego"
"github.com/sonalys/pipego/retry"
)
// API is a generic API implementation.
type API struct{}
// fetchData implements a generic data fetcher signature.
func (a API) fetchData(ctx context.Context, id string) <-chan int {
ch := make(chan int)
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
// Note that if this wasn't async, both fetch and chanDivide would need to be insire parallel stage.
go func() {
defer cancel()
defer close(ch)
for ctx.Err() == nil {
ch <- rand.Intn(10)
time.Sleep(time.Second)
}
}()
return ch
}
type PipelineDependencies struct {
API interface {
fetchData(ctx context.Context, id string) <-chan int
}
}
type Pipeline struct {
dep PipelineDependencies
// We need to use pointers with ChanDivide func because at initialization, the field is not set yet.
values <-chan int
}
func newPipeline(dep PipelineDependencies) Pipeline {
return Pipeline{
dep: dep,
values: make(<-chan int),
}
}
func (s *Pipeline) fetchValues(id string) pp.Step {
return func(ctx context.Context) (err error) {
s.values = s.dep.API.fetchData(ctx, id)
return
}
}
func main() {
ctx := context.Background()
api := API{}
pipeline := newPipeline(PipelineDependencies{
API: api,
})
err := pp.Run(ctx,
// Setup a simple example of a streaming response.
retry.Constant(retry.Inf, time.Second,
pipeline.fetchValues("objectID"),
),
pp.ChanDivide(&pipeline.values,
func(ctx context.Context, i int) (err error) {
fmt.Printf("got value %d", i)
return
},
// Slower worker that will take longer to execute values.
func(ctx context.Context, i int) (err error) {
fmt.Printf("got value %d", i)
time.Sleep(2 * time.Second)
return
},
),
)
if err != nil {
println("could not execute pipeline: ", err.Error())
}
}