nats

package
v0.6.21 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package nats is a NATS connection as a dependency: a configuration section, a connection that never gives up (reconnecting with jitter, no buffering while disconnected, transitions logged), a status probe. The SDK's own event bus connects through Connect too.

The platform's events travel on the bus of the SDK block (BACKPLANE_NATS_*); this is for a NATS of the service's own — one backplane does not see:

type Config struct {
    config.Backplane `json:"backplane"`
    Realtime nats.Config `json:"realtime"` // CALLS_REALTIME_URL, ...
}

rt := deps.NewDependency(root, nats.New(cfg.Realtime), deps.Name("realtime"))
js, err := jetstream.New(rt.Get())

Index

Constants

View Source
const (
	DefaultReconnectWait   = 2 * time.Second
	DefaultReconnectJitter = time.Second
)

Defaults of Options.

Variables

View Source
var (
	// ErrNoURL: the section has no URL.
	ErrNoURL = errors.New("nats: url is required")
	// ErrDisconnected is what the probe reports while the connection is
	// down.
	ErrDisconnected = errors.New("nats: not connected")
)

Errors.

Functions

func Connect

func Connect(cfg Config, o Options) (*natsgo.Conn, error)

Connect connects without blocking on an unreachable server: the connection keeps trying in the background, for ever. While disconnected a publish fails instead of buffering.

func New

func New(cfg Config) deps.Provider[*natsgo.Conn]

New is the provider of the connection: Provide connects and waits for the first connection, the probe reports the status, Close drains nothing and closes.

Types

type Config

type Config struct {
	// URL is one server or a comma-separated list.
	URL config.Secret `json:"url,omitempty"`
	// Creds is the content of a .creds file (user JWT and seed).
	Creds config.Secret `json:"creds,omitempty"`
	TLS   config.TLS    `json:"tls"`
}

Config is the connection's section.

func (Config) Enabled

func (c Config) Enabled() bool

Enabled reports whether a URL is set.

func (Config) Validate

func (c Config) Validate() error

Validate checks what the schema cannot: the URL is set, the credentials and the TLS material parse.

type Options

type Options struct {
	// Name of the client as the server lists it.
	Name string
	Log  *xlog.Logger
	// OnStatus is told every change of connectivity.
	OnStatus func(connected bool)
	// OnClosed runs once the connection is closed for good.
	OnClosed func()
	// Pause between reconnection attempts; zero is the default.
	ReconnectWait, ReconnectJitter time.Duration
}

Options of Connect beyond the section.

Jump to

Keyboard shortcuts

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