summaryrefslogtreecommitdiff
path: root/sync/chan_pipeline.go
blob: 6b5f37fbe24563c1464a6a6cfeac7c6e99da52b5 (plain)
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
package sync

import (
	"context"
	"sync"
)

func Pipe[IN, OUT any](
	ctx context.Context,
	in <-chan IN,
	transform func(context.Context, IN) OUT,
	workerCount int,
	buffer int,
) <-chan OUT {
	if in == nil {
		// we only expect this to happen during development
		panic("in-channel is nil")
	}
	if workerCount < 1 {
		workerCount = 1
	}
	if buffer < 0 {
		buffer = 0
	}

	wg := sync.WaitGroup{}

	out := make(chan OUT, buffer)

	for range workerCount {
		wg.Go(func() {
			for v := range OrDone(ctx, in) {
				w := transform(ctx, v)

				if !CancelOrSend(ctx, out, w) {
					return
				}
			}
		})
	}

	go func() {
		wg.Wait()
		close(out)
	}()

	return out
}