github.com/Jeffail/benthos/v3@v3.65.0/lib/input/wrap_with_pipeline.go (about)

     1  package input
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/Jeffail/benthos/v3/lib/types"
     7  )
     8  
     9  //------------------------------------------------------------------------------
    10  
    11  // WithPipeline is a type that wraps both an input type and a pipeline type
    12  // by routing the input through the pipeline, and implements the input.Type
    13  // interface in order to act like an ordinary input.
    14  type WithPipeline struct {
    15  	in   Type
    16  	pipe types.Pipeline
    17  }
    18  
    19  // WrapWithPipeline routes an input directly into a processing pipeline and
    20  // returns a type that manages both and acts like an ordinary input.
    21  func WrapWithPipeline(procs *int, in Type, pipeConstructor types.PipelineConstructorFunc) (*WithPipeline, error) {
    22  	pipe, err := pipeConstructor(procs)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	if err := pipe.Consume(in.TransactionChan()); err != nil {
    28  		return nil, err
    29  	}
    30  	return &WithPipeline{
    31  		in:   in,
    32  		pipe: pipe,
    33  	}, nil
    34  }
    35  
    36  // WrapWithPipelines wraps an input with a variadic number of pipelines.
    37  func WrapWithPipelines(in Type, pipeConstructors ...types.PipelineConstructorFunc) (Type, error) {
    38  	procs := 0
    39  	var err error
    40  	for _, ctor := range pipeConstructors {
    41  		if in, err = WrapWithPipeline(&procs, in, ctor); err != nil {
    42  			return nil, err
    43  		}
    44  	}
    45  	return in, nil
    46  }
    47  
    48  //------------------------------------------------------------------------------
    49  
    50  // TransactionChan returns the channel used for consuming transactions from this
    51  // input.
    52  func (i *WithPipeline) TransactionChan() <-chan types.Transaction {
    53  	return i.pipe.TransactionChan()
    54  }
    55  
    56  // Connected returns a boolean indicating whether this input is currently
    57  // connected to its target.
    58  func (i *WithPipeline) Connected() bool {
    59  	return i.in.Connected()
    60  }
    61  
    62  //------------------------------------------------------------------------------
    63  
    64  // CloseAsync triggers a closure of this object but does not block.
    65  func (i *WithPipeline) CloseAsync() {
    66  	i.in.CloseAsync()
    67  	i.pipe.CloseAsync()
    68  }
    69  
    70  // WaitForClose is a blocking call to wait until the object has finished closing
    71  // down and cleaning up resources.
    72  func (i *WithPipeline) WaitForClose(timeout time.Duration) error {
    73  	return i.pipe.WaitForClose(timeout)
    74  }
    75  
    76  //------------------------------------------------------------------------------