/
wrap_with_pipeline.go
76 lines (62 loc) · 2.19 KB
/
wrap_with_pipeline.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
package input
import (
"time"
"github.com/Jeffail/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
// WithPipeline is a type that wraps both an input type and a pipeline type
// by routing the input through the pipeline, and implements the input.Type
// interface in order to act like an ordinary input.
type WithPipeline struct {
in Type
pipe types.Pipeline
}
// WrapWithPipeline routes an input directly into a processing pipeline and
// returns a type that manages both and acts like an ordinary input.
func WrapWithPipeline(procs *int, in Type, pipeConstructor types.PipelineConstructorFunc) (*WithPipeline, error) {
pipe, err := pipeConstructor(procs)
if err != nil {
return nil, err
}
if err := pipe.Consume(in.TransactionChan()); err != nil {
return nil, err
}
return &WithPipeline{
in: in,
pipe: pipe,
}, nil
}
// WrapWithPipelines wraps an input with a variadic number of pipelines.
func WrapWithPipelines(in Type, pipeConstructors ...types.PipelineConstructorFunc) (Type, error) {
procs := 0
var err error
for _, ctor := range pipeConstructors {
if in, err = WrapWithPipeline(&procs, in, ctor); err != nil {
return nil, err
}
}
return in, nil
}
//------------------------------------------------------------------------------
// TransactionChan returns the channel used for consuming transactions from this
// input.
func (i *WithPipeline) TransactionChan() <-chan types.Transaction {
return i.pipe.TransactionChan()
}
// Connected returns a boolean indicating whether this input is currently
// connected to its target.
func (i *WithPipeline) Connected() bool {
return i.in.Connected()
}
//------------------------------------------------------------------------------
// CloseAsync triggers a closure of this object but does not block.
func (i *WithPipeline) CloseAsync() {
i.in.CloseAsync()
i.pipe.CloseAsync()
}
// WaitForClose is a blocking call to wait until the object has finished closing
// down and cleaning up resources.
func (i *WithPipeline) WaitForClose(timeout time.Duration) error {
return i.pipe.WaitForClose(timeout)
}
//------------------------------------------------------------------------------