github.com/Jeffail/benthos/v3@v3.65.0/lib/output/nats.go (about)

     1  package output
     2  
     3  import (
     4  	"github.com/Jeffail/benthos/v3/internal/docs"
     5  	"github.com/Jeffail/benthos/v3/internal/impl/nats/auth"
     6  	"github.com/Jeffail/benthos/v3/lib/log"
     7  	"github.com/Jeffail/benthos/v3/lib/metrics"
     8  	"github.com/Jeffail/benthos/v3/lib/output/writer"
     9  	"github.com/Jeffail/benthos/v3/lib/types"
    10  	"github.com/Jeffail/benthos/v3/lib/util/tls"
    11  )
    12  
    13  //------------------------------------------------------------------------------
    14  
    15  func init() {
    16  	Constructors[TypeNATS] = TypeSpec{
    17  		constructor: fromSimpleConstructor(NewNATS),
    18  		Summary: `
    19  Publish to an NATS subject.`,
    20  		Description: `
    21  This output will interpolate functions within the subject field, you
    22  can find a list of functions [here](/docs/configuration/interpolation#bloblang-queries).
    23  
    24  ` + auth.Description(),
    25  		Async: true,
    26  		FieldSpecs: docs.FieldSpecs{
    27  			docs.FieldCommon(
    28  				"urls",
    29  				"A list of URLs to connect to. If an item of the list contains commas it will be expanded into multiple URLs.",
    30  				[]string{"nats://127.0.0.1:4222"},
    31  				[]string{"nats://username:password@127.0.0.1:4222"},
    32  			).Array(),
    33  			docs.FieldCommon("subject", "The subject to publish to.").IsInterpolated(),
    34  			docs.FieldString("headers", "Explicit message headers to add to messages.",
    35  				map[string]string{
    36  					"Content-Type": "application/json",
    37  					"Timestamp":    `${!meta("Timestamp")}`,
    38  				},
    39  			).IsInterpolated().Map(),
    40  			docs.FieldCommon("max_in_flight", "The maximum number of messages to have in flight at a given time. Increase this to improve throughput."),
    41  			tls.FieldSpec(),
    42  			auth.FieldSpec(),
    43  		},
    44  		Categories: []Category{
    45  			CategoryServices,
    46  		},
    47  	}
    48  }
    49  
    50  // NewNATS creates a new NATS output type.
    51  func NewNATS(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
    52  	w, err := writer.NewNATSV2(conf.NATS, mgr, log, stats)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	if conf.NATS.MaxInFlight == 1 {
    57  		return NewWriter(TypeNATS, w, log, stats)
    58  	}
    59  	return NewAsyncWriter(TypeNATS, conf.NATS.MaxInFlight, w, log, stats)
    60  }
    61  
    62  //------------------------------------------------------------------------------