nats

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package nats provides NATS and JetStream connection utilities with tracing integration.

It offers connection initialization with TLS/mTLS support, health checks, graceful shutdown, and distributed tracing via OpenTelemetry.

Index

Constants

This section is empty.

Variables

View Source
var ErrIncompleteMTLS = errors.New("mtls requires both cert_file and key_file")

ErrIncompleteMTLS is returned when mTLS config has cert_file but no key_file or vice versa.

View Source
var ErrURLRequired = errors.New("url is required")

ErrURLRequired is returned when URL is empty in Config.

Functions

func Connect

func Connect(ctx context.Context, cfg Config) (*nats.Conn, error)

Connect establishes a connection to the NATS server. It registers a shutdown handler to drain the connection gracefully.

func ConsumeWithTracing

func ConsumeWithTracing(ctx context.Context, in ConsumeInput) (jetstream.ConsumeContext, error)

ConsumeWithTracing starts consuming messages with automatic trace extraction. Each message handler receives a context with the extracted trace context.

func ExtractTraceContext

func ExtractTraceContext(ctx context.Context, msg jetstream.Msg) context.Context

ExtractTraceContext extracts trace context from a JetStream message. Use this when you need manual control over span creation.

func JetStreamPublish

JetStreamPublish sends a message to JetStream with trace context injection.

func Publish

func Publish(ctx context.Context, conn *nats.Conn, in PublishInput) error

Publish sends a message with trace context injection.

Types

type Config

type Config struct {
	// URL is the NATS server URL.
	// Required. Example: "nats://localhost:4222"
	URL string `koanf:"url"`

	// MaxReconnects is the maximum number of reconnection attempts.
	// -1 means unlimited reconnects.
	MaxReconnects int `koanf:"max_reconnects"`

	// ReconnectWait is the time to wait between reconnection attempts.
	ReconnectWait time.Duration `koanf:"reconnect_wait"`

	// ReconnectJitter is the maximum random jitter added to reconnect wait.
	ReconnectJitter time.Duration `koanf:"reconnect_jitter"`

	// ConnectTimeout is the timeout for initial connection.
	ConnectTimeout time.Duration `koanf:"connect_timeout"`

	// DrainTimeout is the timeout for graceful drain during shutdown.
	DrainTimeout time.Duration `koanf:"drain_timeout"`

	// TLS holds TLS/mTLS configuration. Optional.
	TLS *TLSConfig `koanf:"tls"`
}

Config holds configuration for NATS connection.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible default values. URL is required and must be set by the caller.

func (Config) Validate

func (c Config) Validate() error

Validate checks the configuration for errors.

type ConsumeInput

type ConsumeInput struct {
	Consumer jetstream.Consumer
	Handler  func(ctx context.Context, msg jetstream.Msg) error
	Options  []jetstream.PullConsumeOpt
}

ConsumeInput holds parameters for consumer operations with tracing.

type HealthChecker

type HealthChecker struct {
	// contains filtered or unexported fields
}

HealthChecker checks NATS connection status. Implements grpchealth.HealthChecker interface.

func NewHealthChecker

func NewHealthChecker(conn *nats.Conn) *HealthChecker

NewHealthChecker creates a health checker for the given connection.

func (*HealthChecker) Check

func (c *HealthChecker) Check(_ context.Context) bool

Check returns true if the connection is active.

type JetStreamPublishInput

type JetStreamPublishInput struct {
	Subject string
	Data    []byte
	Headers nats.Header
	MsgID   string // optional, for deduplication
}

JetStreamPublishInput holds parameters for JetStream publish operations.

type PublishInput

type PublishInput struct {
	Subject string
	Data    []byte
	Headers nats.Header
}

PublishInput holds parameters for Publish operations.

type TLSConfig

type TLSConfig struct {
	// CAFile is the path to the CA certificate for server verification.
	CAFile string `koanf:"ca_file"`

	// SkipVerify disables server certificate verification.
	// Default: false (verification enabled).
	SkipVerify bool `koanf:"skip_verify"`

	// CertFile is the path to the client certificate for mTLS.
	CertFile string `koanf:"cert_file"`

	// KeyFile is the path to the client private key for mTLS.
	KeyFile string `koanf:"key_file"`
}

TLSConfig holds TLS and mTLS configuration.

Jump to

Keyboard shortcuts

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