-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.go
More file actions
165 lines (142 loc) · 3.07 KB
/
Copy pathpipeline.go
File metadata and controls
165 lines (142 loc) · 3.07 KB
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
//
// pipeline.go
//
// Created by Frederic DELBOS - fred@hyperboloide.com on Feb 8 2015.
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
//
package sprocess
import (
"io"
"sync"
)
type Pipeline struct {
lastReader io.ReadCloser
errorChain []chan error
cancelChain []chan struct{}
Errors []error
size int
}
type EncodingPipeline struct {
Encoders []Encoder
Output Outputer
}
func (ep *EncodingPipeline) GetOutputs() []string {
names := []string{ep.Output.GetName()}
for _, encoder := range ep.Encoders {
if tee, found := encoder.(*Tee); found == true {
if n := tee.GetOutputs(); n != nil && len(n) > 0 {
names = append(names, n...)
}
}
}
return names
}
type DecodingPipeline struct {
Decoders []Decoder
Input Inputer
}
func (p *Pipeline) mergeErrors(chans []chan error) <-chan error {
var wg sync.WaitGroup
out := make(chan error, 1)
output := func(c <-chan error) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(p.size)
for _, c := range chans {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
func (p *Pipeline) stopAll() {
for _, c := range p.cancelChain {
close(c)
}
}
func (p *Pipeline) Exec(w io.Writer) error {
defer p.stopAll()
errCh := make(chan error)
go func() {
_, err := io.Copy(w, p.lastReader)
errCh <- err
close(errCh)
}()
errCopy := <-errCh
if errCopy != nil {
return errCopy
}
p.Errors = make([]error, p.size)
i := 0
var firstError error
for n := range p.mergeErrors(p.errorChain) {
p.Errors[i] = n
if firstError == nil && p.Errors[i] != nil {
firstError = p.Errors[i]
break
}
i += 1
}
return firstError
}
func newPipeline(r io.ReadCloser, size int) *Pipeline {
p := &Pipeline{
lastReader: r,
errorChain: make([]chan error, size),
cancelChain: make([]chan struct{}, size),
size: size,
}
return p
}
func NewEncoding(encoders []Encoder, r io.ReadCloser, d *Data) *Pipeline {
p := newPipeline(r, len(encoders))
for i, e := range encoders {
errCh := make(chan error, 1)
cancelCh := make(chan struct{}, 1)
nextReader, writer := io.Pipe()
go func(e Encoder, r io.ReadCloser, w *io.PipeWriter, d *Data) {
defer w.Close()
defer close(errCh)
go func() {
select {
case <-cancelCh:
r.Close()
}
}()
errCh <- e.Encode(r, w, d)
}(e, p.lastReader, writer, d)
p.lastReader = nextReader
p.errorChain[i] = errCh
p.cancelChain[i] = cancelCh
}
return p
}
func NewDecoding(decoders []Decoder, r io.ReadCloser, d *Data) *Pipeline {
p := newPipeline(r, len(decoders))
for i, e := range decoders {
errCh := make(chan error, 1)
cancelCh := make(chan struct{}, 1)
nextReader, writer := io.Pipe()
go func(e Decoder, r io.ReadCloser, w *io.PipeWriter, d *Data) {
defer w.Close()
defer close(errCh)
go func() {
select {
case <-cancelCh:
r.Close()
}
}()
errCh <- e.Decode(r, w, d)
}(e, p.lastReader, writer, d)
p.lastReader = nextReader
p.errorChain[i] = errCh
p.cancelChain[i] = cancelCh
}
return p
}