Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var NoLogging = func(_ string, _ ...interface{}) {}
NoLogging is the default LogFunc. It logs nothing.
Functions ¶
This section is empty.
Types ¶
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection is the main entry point for the library. It is used to create Publishers and Subscribers. It is also used to close the connection to the NATS server/ cluster.
func Connect ¶
func Connect(servers []string, options ...Option) (*Connection, error)
Connect returns Connection to a NATS server/ cluster and enables Publisher and Subscriber creation.
func MustConnectToNATS ¶ added in v0.12.0
func MustConnectToNATS(config *Config, logger *slog.Logger) *Connection
MustConnectToNATS to NATS Server. This function panics if the connection could not be established. servers: List of NATS servers in the form of "nats://<user:password>@<host>:<port>" logger: an optional slog.Logger instance
func (*Connection) Close ¶ added in v0.10.0
func (c *Connection) Close() error
Close closes the NATS Connection and drains all subscriptions.
func (*Connection) NewPublisher ¶
func (c *Connection) NewPublisher(args PublisherArgs) (*Publisher, error)
NewPublisher creates a new Publisher that publishes to a NATS stream.
func (*Connection) NewSubscriber ¶
func (c *Connection) NewSubscriber(args SubscriberArgs) (*Subscriber, error)
NewSubscriber creates a new Subscriber that subscribes to a NATS stream.
type LogFunc ¶ added in v0.10.0
type LogFunc func(format string, a ...interface{})
LogFunc is a generic logging function to incorporate the logging of the library into the application. It can be set via the Option of a Connection using WithLogger(l LogFunc).
type Msg ¶ added in v0.10.0
type Msg struct { // Subject represents the destination subject name, like "PRODUCTS.new" Subject string // Reply represents an optional subject name where a reply message should be sent to. // This value is just distributed, whether the response is sent to the specified subject depends on the Subscriber. Reply string // MsgID represents a unique value for the message, like a hash value of Data. // Semantically equal messages must lead to the same MsgID at any time. // E.g. two messages with the same Data must have the same MsgID. // // The MsgID is used for deduplication. MsgID string // Data represents the raw byte data to send. The data is sent as-is. Data []byte // Header represents the optional Header for the message. Header Header }
Msg contains the arguments publishing a new message. By using a struct we are open for adding new arguments in the future and the caller can omit arguments where the default value is OK.
type MsgHandler ¶
MsgHandler is the type of function the Subscriber has to implement to process an incoming message.
type Option ¶ added in v0.10.0
type Option func(*Connection)
Option is an optional configuration argument for the Connect() function.
func WithLogger ¶ added in v0.10.0
WithLogger sets the logger using the generic LogFunc function. This option can be passed in the Connect function. Without this option, the default LogFunc is a nop function.
type Publisher ¶
type Publisher struct {
// contains filtered or unexported fields
}
Publisher is a NATS publisher that publishes to a NATS stream.
type PublisherArgs ¶ added in v0.10.0
type PublisherArgs struct { // StreamName is the name of the stream like "PRODUCTS" or "ORDERS". // If it does not exist, the stream will be created. StreamName string }
PublisherArgs contains the arguments for creating a new Publisher. By using a struct we are open for adding new arguments in the future and the caller can omit arguments where the default value is OK.
type Subscriber ¶
type Subscriber struct {
// contains filtered or unexported fields
}
Subscriber subscribes to a NATS consumer and pulls messages to handle by MsgHandler.
func (*Subscriber) Start ¶ added in v0.10.0
func (s *Subscriber) Start(handler MsgHandler) (err error)
Start subscribes to the NATS consumer and starts a go-routine that handles pulled messages.
func (*Subscriber) Stop ¶ added in v0.10.0
func (s *Subscriber) Stop() error
Stop unsubscribes the consumer from the NATS stream.
type SubscriberArgs ¶ added in v0.10.0
type SubscriberArgs struct { // ConsumerName contains the name of the consumer. By default, this should be the // name of the service. ConsumerName string // Subject defines which subjects of the stream should be subscribed. // Examples: // "ORDERS.new" -> subscribe subject "new" of stream "ORDERS" // "ORDERS.>" -> subscribe all subjects in any level of stream "ORDERS". // "ORDERS.*" -> subscribe all direct subjects of stream "ORDERS", like "ORDERS.new", "ORDERS.processed", // but not "ORDERS.new.error". Subject string // Mode defines the constraints of the subscription. Default is MultipleSubscribersAllowed. // See SubscriptionMode for details. Mode SubscriptionMode }
SubscriberArgs contains the arguments for creating a new Subscriber. By using a struct we are open for adding new arguments in the future and the caller can omit arguments where the default value is OK.
type SubscriptionMode ¶
type SubscriptionMode int
SubscriptionMode defines how the consumer and its Subscriber are configured. This mode must be set accordingly to the use-case. If the order of messages should be strictly ordered, SingleSubscriberStrictMessageOrder should be used. If the message order is not important, but horizontal scaling is, use MultipleSubscribersAllowed.
const ( // MultipleSubscribersAllowed mode (default) enables multiple Subscriber of one consumer for horizontal scaling. // The message order cannot be guaranteed when messages get NAKed/ MsgHandler for message returns error. MultipleSubscribersAllowed SubscriptionMode = iota // SingleSubscriberStrictMessageOrder mode enables strict order of messages. If messages get NAKed/ MsgHandler for // message returns error, the Subscriber of consumer will retry the failed message until resolved. This blocks the // entire consumer, so that horizontal scaling is not effectively possible. SingleSubscriberStrictMessageOrder )