/
chan_iterator.go
180 lines (145 loc) · 3.96 KB
/
chan_iterator.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package iterators
import (
"context"
"errors"
"io"
"sync"
"golang.org/x/sync/errgroup"
)
var _ StructIterator[dummy] = &ChanIterator[dummy]{}
// ChanIterator is a channel-based iterator that may be used to run a collection of StructIterators in parallel.
//
// Notice that its asynchronous working does not make it suitable to collect ordered items.
//
// The ChanIterator is goroutine-safe and may be iterated by several concurrent goroutines.
//
// The input is collected from a collection of input StructIterators, then may be collected by one or several goroutines
// reading from the ChanIterator using Next() and Item().
//
// Item() may return io.EOF is the iterator is done with producing records (e.g. some other consumer reached the end of the stream).
//
// WithChanFanInBuffers may be used to pre-fetch from input iterators asynchronously.
//
// Methods Collect() and CollectPrt() can't be used by concurrent goroutines and are protected against such a misuse.
type ChanIterator[T any] struct {
fanIn chan T
workerGroup *errgroup.Group
ctx context.Context
mx sync.Mutex
done chan struct{}
*chanIteratorOptions
}
// NewChanIterator builds a ChanIterator and starts the goroutines pumping items from the input iterators.
//
// All goroutines are terminated and input iterators closed if the context is cancelled.
func NewChanIterator[T any](ctx context.Context, iterators []StructIterator[T], opts ...ChanIteratorOption) *ChanIterator[T] {
var pendingWorkers sync.WaitGroup // rendez-vous to close the fan-in channel
workerGroup, groupCtx := errgroup.WithContext(ctx)
iter := &ChanIterator[T]{
ctx: groupCtx,
workerGroup: workerGroup,
chanIteratorOptions: chanIteratorOptionsWithDefault(opts),
done: make(chan struct{}),
}
if iter.fanInBuffers < 0 {
iter.fanInBuffers = len(iterators)
}
iter.fanIn = make(chan T, iter.fanInBuffers)
for i := range iterators {
idx := i
pendingWorkers.Add(1)
workerGroup.Go(func() error {
iterator := iterators[idx]
defer func() {
_ = iterator.Close()
pendingWorkers.Done()
}()
for iterator.Next() {
item, err := iterator.Item()
if err != nil {
return err
}
select {
case <-groupCtx.Done():
return groupCtx.Err()
case iter.fanIn <- item:
}
}
return nil
})
}
workerGroup.Go(func() error {
pendingWorkers.Wait()
close(iter.fanIn)
close(iter.done)
return nil
})
return iter
}
func (d *ChanIterator[T]) Next() bool {
select {
case <-d.done:
return len(d.fanIn) > 0
case <-d.ctx.Done():
return false
default:
return true
}
}
func (d *ChanIterator[T]) Item() (T, error) {
var empty T
select {
case item, ok := <-d.fanIn:
if !ok {
return empty, io.EOF
}
return item, nil
case <-d.ctx.Done():
return empty, d.ctx.Err()
}
}
func (d *ChanIterator[T]) Close() error {
return d.workerGroup.Wait()
}
func (d *ChanIterator[T]) Collect() ([]T, error) {
d.mx.Lock()
defer d.mx.Unlock()
results := make([]T, 0, d.preallocatedItems)
for d.Next() {
item, err := d.Item()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return results, preferErrorOverContext(err, d.Close())
}
results = append(results, item)
}
return results, d.Close()
}
func (d *ChanIterator[T]) CollectPtr() ([]*T, error) {
d.mx.Lock()
defer d.mx.Unlock()
results := make([]*T, 0, d.preallocatedItems)
for d.Next() {
item, err := d.Item()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return results, preferErrorOverContext(err, d.Close())
}
results = append(results, &item)
}
return results, d.Close()
}
// preferErrorOverContext returns a specific error preferrably
// to the generic "context cancelled" error, whenever available.
func preferErrorOverContext(err1, err2 error) error {
isCancelled1 := errors.Is(err1, context.Canceled)
isCancelled2 := errors.Is(err2, context.Canceled)
if isCancelled1 && !isCancelled2 {
return err2
}
return err1
}