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

     1  package input
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/Jeffail/benthos/v3/internal/docs"
     8  	"github.com/Jeffail/benthos/v3/internal/interop"
     9  	"github.com/Jeffail/benthos/v3/lib/log"
    10  	"github.com/Jeffail/benthos/v3/lib/metrics"
    11  	"github.com/Jeffail/benthos/v3/lib/types"
    12  )
    13  
    14  //------------------------------------------------------------------------------
    15  
    16  func init() {
    17  	Constructors[TypeResource] = TypeSpec{
    18  		constructor: fromSimpleConstructor(NewResource),
    19  		Summary: `
    20  Resource is an input type that runs a resource input by its name.`,
    21  		Description: `
    22  This input allows you to reference the same configured input resource in multiple places, and can also tidy up large nested configs. For
    23  example, the config:
    24  
    25  ` + "```yaml" + `
    26  input:
    27    broker:
    28      inputs:
    29        - kafka:
    30            addresses: [ TODO ]
    31            topics: [ foo ]
    32            consumer_group: foogroup
    33        - gcp_pubsub:
    34            project: bar
    35            subscription: baz
    36  ` + "```" + `
    37  
    38  Could also be expressed as:
    39  
    40  ` + "```yaml" + `
    41  input:
    42    broker:
    43      inputs:
    44        - resource: foo
    45        - resource: bar
    46  
    47  input_resources:
    48    - label: foo
    49      kafka:
    50        addresses: [ TODO ]
    51        topics: [ foo ]
    52        consumer_group: foogroup
    53  
    54    - label: bar
    55      gcp_pubsub:
    56        project: bar
    57        subscription: baz
    58   ` + "```" + `
    59  
    60  You can find out more about resources [in this document.](/docs/configuration/resources)`,
    61  		Categories: []Category{
    62  			CategoryUtility,
    63  		},
    64  		config: docs.FieldComponent().HasType(docs.FieldTypeString).HasDefault(""),
    65  	}
    66  }
    67  
    68  //------------------------------------------------------------------------------
    69  
    70  // Resource is an input that wraps an input resource.
    71  type Resource struct {
    72  	mgr          types.Manager
    73  	name         string
    74  	log          log.Modular
    75  	mErrNotFound metrics.StatCounter
    76  }
    77  
    78  // NewResource returns a resource input.
    79  func NewResource(
    80  	conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
    81  ) (Type, error) {
    82  	if err := interop.ProbeInput(context.Background(), mgr, conf.Resource); err != nil {
    83  		return nil, err
    84  	}
    85  	return &Resource{
    86  		mgr:          mgr,
    87  		name:         conf.Resource,
    88  		log:          log,
    89  		mErrNotFound: stats.GetCounter("error_not_found"),
    90  	}, nil
    91  }
    92  
    93  //------------------------------------------------------------------------------
    94  
    95  // TransactionChan returns a transactions channel for consuming messages from
    96  // this input type.
    97  func (r *Resource) TransactionChan() (tChan <-chan types.Transaction) {
    98  	if err := interop.AccessInput(context.Background(), r.mgr, r.name, func(i types.Input) {
    99  		tChan = i.TransactionChan()
   100  	}); err != nil {
   101  		r.log.Debugf("Failed to obtain input resource '%v': %v", r.name, err)
   102  		r.mErrNotFound.Incr(1)
   103  	}
   104  	return
   105  }
   106  
   107  // Connected returns a boolean indicating whether this input is currently
   108  // connected to its target.
   109  func (r *Resource) Connected() (isConnected bool) {
   110  	if err := interop.AccessInput(context.Background(), r.mgr, r.name, func(i types.Input) {
   111  		isConnected = i.Connected()
   112  	}); err != nil {
   113  		r.log.Debugf("Failed to obtain input resource '%v': %v", r.name, err)
   114  		r.mErrNotFound.Incr(1)
   115  	}
   116  	return
   117  }
   118  
   119  // CloseAsync shuts down the processor and stops processing requests.
   120  func (r *Resource) CloseAsync() {
   121  }
   122  
   123  // WaitForClose blocks until the processor has closed down.
   124  func (r *Resource) WaitForClose(timeout time.Duration) error {
   125  	return nil
   126  }
   127  
   128  //------------------------------------------------------------------------------