github.com/Jeffail/benthos/v3@v3.65.0/lib/manager/output_wrapper.go (about)

     1  package manager
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/Jeffail/benthos/v3/lib/types"
     9  )
    10  
    11  type outputWrapper struct {
    12  	output types.Output
    13  
    14  	tranChan  chan types.Transaction
    15  	closeOnce sync.Once
    16  }
    17  
    18  func wrapOutput(o types.Output) (*outputWrapper, error) {
    19  	tranChan := make(chan types.Transaction)
    20  	if err := o.Consume(tranChan); err != nil {
    21  		return nil, err
    22  	}
    23  	return &outputWrapper{
    24  		output:   o,
    25  		tranChan: tranChan,
    26  	}, nil
    27  }
    28  
    29  func (w *outputWrapper) WriteTransaction(ctx context.Context, t types.Transaction) error {
    30  	select {
    31  	case w.tranChan <- t:
    32  	case <-ctx.Done():
    33  		return types.ErrTimeout
    34  	}
    35  	return nil
    36  }
    37  
    38  // Connected returns a boolean indicating whether this output is currently
    39  // connected to its target.
    40  func (w *outputWrapper) Connected() bool {
    41  	return w.output.Connected()
    42  }
    43  
    44  func (w *outputWrapper) CloseAsync() {
    45  	w.output.CloseAsync()
    46  }
    47  
    48  func (w *outputWrapper) WaitForClose(timeout time.Duration) error {
    49  	w.closeOnce.Do(func() {
    50  		close(w.tranChan)
    51  	})
    52  	return w.output.WaitForClose(timeout)
    53  }