forked from benthosdev/benthos
/
nats.go
62 lines (56 loc) · 2.06 KB
/
nats.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
package output
import (
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/internal/impl/nats/auth"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/output/writer"
"github.com/dafanshu/benthos/v3/lib/types"
"github.com/dafanshu/benthos/v3/lib/util/tls"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeNATS] = TypeSpec{
constructor: fromSimpleConstructor(NewNATS),
Summary: `
Publish to an NATS subject.`,
Description: `
This output will interpolate functions within the subject field, you
can find a list of functions [here](/docs/configuration/interpolation#bloblang-queries).
` + auth.Description(),
Async: true,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon(
"urls",
"A list of URLs to connect to. If an item of the list contains commas it will be expanded into multiple URLs.",
[]string{"nats://127.0.0.1:4222"},
[]string{"nats://username:password@127.0.0.1:4222"},
).Array(),
docs.FieldCommon("subject", "The subject to publish to.").IsInterpolated(),
docs.FieldString("headers", "Explicit message headers to add to messages.",
map[string]string{
"Content-Type": "application/json",
"Timestamp": `${!meta("Timestamp")}`,
},
).IsInterpolated().Map(),
docs.FieldCommon("max_in_flight", "The maximum number of messages to have in flight at a given time. Increase this to improve throughput."),
tls.FieldSpec(),
auth.FieldSpec(),
},
Categories: []Category{
CategoryServices,
},
}
}
// NewNATS creates a new NATS output type.
func NewNATS(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
w, err := writer.NewNATSV2(conf.NATS, mgr, log, stats)
if err != nil {
return nil, err
}
if conf.NATS.MaxInFlight == 1 {
return NewWriter(TypeNATS, w, log, stats)
}
return NewAsyncWriter(TypeNATS, conf.NATS.MaxInFlight, w, log, stats)
}
//------------------------------------------------------------------------------