publisher

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyConfig       = errors.New("empty config")
	ErrAddrMissing       = errors.New("missing required field 'Addr'")
	ErrExchangeMissing   = errors.New("missing required field 'Exchange'")
	ErrCredentialMissing = errors.New("missing RabbitMQ credentials in environment")
	ErrConnectionFailed  = errors.New("failed to connect to RabbitMQ")
	ErrChannelFailed     = errors.New("failed to open channel")
	ErrExchangeDeclare   = errors.New("failed to declare exchange")
)

Error variables representing different failure scenarios.

View Source
var ChannelFunc = func(conn *amqp091.Connection) (Channel, error) {
	return conn.Channel()
}

ChannelFunc is a function variable used to open a channel on the given RabbitMQ connection.

View Source
var DialFunc = amqp091.Dial

DialFunc is a function variable used to establish a connection to RabbitMQ.

Functions

func GetConnURL

func GetConnURL(cfg *Config) (string, error)

GetConnURL constructs the RabbitMQ connection URL using the config and environment credentials.

func Validate

func Validate(cfg *Config) error

Validate checks whether the provided Config is valid for connecting to RabbitMQ.

Types

type Channel

type Channel interface {
	PublishWithContext(ctx context.Context, exchange, key string, mandatory, immediate bool, msg amqp091.Publishing) error
	ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp091.Table) error
	Close() error
}

Channel defines the interface for publishing messages to RabbitMQ.

type Config

type Config struct {
	Addr       string
	Exchange   string
	RoutingKey string
	Durable    bool
	UseTLS     bool
}

Config holds the configuration required to establish a connection with RabbitMQ.

type Publisher

type Publisher struct {
	Conn    *amqp091.Connection
	Channel Channel
	Config  *Config
}

Publisher manages the RabbitMQ connection and channel to publish messages.

func New

func New(cfg *Config) (*Publisher, func() error, error)

New initializes a new Publisher with the given config, opens a connection, channel, and declares the exchange. Returns the publisher and a cleanup function.

func (*Publisher) Publish

func (p *Publisher) Publish(ctx context.Context, routingKey string, msg []byte) error

Publish sends a message to the configured RabbitMQ exchange with the specified routing key. If routingKey is empty, the default routing key from Config is used.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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