natsext

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 6 Imported by: 6

README

Core NATS Extensions

License Go Reference Build Status Go Report Card

Core NATS Extensions is a set of utilities providing additional features to Core NATS component of nats.go client.

Installation

go get github.com/synadia-io/orbit.go/natsext

Utilities

RequestMany

RequestMany is a utility that allows you to send a single request and await multiple responses. This allows you to implement various patterns like scatter-gather or streaming responses.

Responses are returned in an iterator, which you can range over to receive messages. When a termination condition is met, the iterator is closed (and no error is returned).

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
msgs, err := natsext.RequestMany(ctx, nc, "subject", []byte("request data"))
if err != nil {
    // handle error
}
for msg, err := range msgs {
    if err != nil {
        // handle error
    }
    fmt.Println(string(msg.Data))
}

Alternatively, use RequestManyMsg to send a nats.Msg request:

msg := &nats.Msg{
    Subject: "subject",
    Data:    []byte("request data"),
    Header:  nats.Header{
        "Key": []string{"Value"},
    },
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
iter, err := natsext.RequestManyMsg(ctx, nc, msg)
if err != nil {
    // handle error
}
// gather responses
Configuration

Timeout and cancellation are handled by the context passed to RequestMany and RequestManyMsg. In addition, you can configure the following options:

  • RequestManyStall: Sets the stall timer, useful in scatter-gather scenarios where subsequent responses are expected within a certain timeframe.
  • RequestManyMaxMessages: Sets the maximum number of messages to receive.
  • RequestManySentinel: Stops receiving messages once a sentinel message is received.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultSentinel

func DefaultSentinel(msg *nats.Msg) bool

DefaultSentinel is a sentinel function that stops receiving messages once a message with an empty payload is received.

func RequestMany

func RequestMany(ctx context.Context, nc *nats.Conn, subject string, data []byte, opts ...RequestManyOpt) (iter.Seq2[*nats.Msg, error], error)

RequestMany will send a request payload and return an iterator to receive multiple responses. If context timeout is not set, the number of messages received is constrained by the client's timeout.

Use the RequestManyOpt functions to further configure this method's behavior. - RequestManyStall sets the stall timer, which can be used in scatter-gather scenarios where subsequent responses are expected to arrive within a certain time frame. - RequestManyMaxMessages sets the maximum number of messages to receive. - RequestManySentinel stops returning responses once a message for which the provided function returns true.

func RequestManyMsg

func RequestManyMsg(ctx context.Context, nc *nats.Conn, msg *nats.Msg, opts ...RequestManyOpt) (iter.Seq2[*nats.Msg, error], error)

RequestManyMsg will send a Msg request and return an iterator to receive multiple responses. If context timeout is not set, the number of messages received is constrained by the client's timeout.

Use the RequestManyOpt functions to further configure this method's behavior. - RequestManyStall sets the stall timer, which can be used in scatter-gather scenarios where subsequent responses are expected to arrive within a certain time frame. - RequestManyMaxMessages sets the maximum number of messages to receive. - RequestManySentinel stops returning responses once a message for which the provided function returns true.

Types

type RequestManyOpt

type RequestManyOpt func(*requestManyOpts) error

RequestManyOpt is a function that can be used to configure the behavior of the RequestMany function.

func RequestManyMaxMessages

func RequestManyMaxMessages(count int) RequestManyOpt

RequestManyMaxMessages sets the maximum number of messages to receive.

func RequestManySentinel

func RequestManySentinel(f func(*nats.Msg) bool) RequestManyOpt

RequestManySentinel is a function that can be used to stop receiving messages once a sentinel message is received. A sentinel message is a message for which the provided function returns true.

func RequestManyStall

func RequestManyStall(stall time.Duration) RequestManyOpt

RequestManyStall sets the stall timer, which can be used in scatter-gather scenarios where subsequent responses are expected to arrive within a certain time frame.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL