-
Notifications
You must be signed in to change notification settings - Fork 0
/
merger.go
71 lines (60 loc) · 1.46 KB
/
merger.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
package gomeetupbris
import (
"github.com/pkg/errors"
"gitlab.com/priceshield/agent-gateway/modelops"
)
// Merge is an indirection of the Merge func, making it simple to mock them in tests
var Merge = modelops.Merge
// Delta is an indirection of the Merge func, making it simple to mock them in tests
var Delta = modelops.Delta
// merger merges new state into existing state and also calculates the delta
type merger struct {
in <-chan pipelineItem
out chan<- pipelineItem
sink chan<- pipelineItem // where things go to die
doneChan chan bool
}
func newMerger(in <-chan pipelineItem, out chan<- pipelineItem, sink chan<- pipelineItem) *merger {
return &merger{
in: in,
out: out,
sink: sink,
doneChan: make(chan bool),
}
}
func (m *merger) start() {
for {
select {
case e := <-m.in:
go func(pi pipelineItem) {
err := m.handleMergeAndDelta(&pi)
if err != nil {
// add to sink
pi.err = err
m.sink <- pi
return
}
// all good. forward on to next step
m.out <- pi
}(e)
case <-m.doneChan:
return
}
}
}
func (m *merger) handleMergeAndDelta(pi *pipelineItem) error {
merged, err := Merge(pi.prevState, &pi.converted)
if err != nil {
return errors.Wrap(err, "merge_error")
}
pi.merged = merged
delta, err := Delta(pi.prevState, merged)
if err != nil {
return errors.Wrap(err, "delta_error")
}
pi.delta = delta
return nil
}
func (m *merger) stop() {
m.doneChan <- true
}