rabbitmq

package module
v0.0.0-...-521c9ed Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 14 Imported by: 0 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Status

This library has been migrated to dev_forum-lib. This repository is archived and no longer maintained.

dev-forum_rabbitmq

GoDoc Go Report Card GitHub License

RabbitMQ library made on top of rabbitmq/amqp091-go for dev-forum project.

Usage

NewRabbitMQ returns a new initialized connection struct. It will manage the active connection in the background. Connection should be closed in order to shut it down gracefully.

func example() {
	user := "guest"
	pass := "guest"
	host := "localhost"
	port := "5672"
	consumer := "user-service" //  Unique name for each consumer used to sign messages.

	// You can specify your own config or use DefaultConfig() instead.
	config := Config{
		QueueSize:         100,
		MaxWorkers:        50,
		ReconnectInterval: time.Second * 2,
		MaxRequests:       5,
		ClearInterval:     time.Second * 5,
		ClosedTimeout:     time.Second * 5,
	}

	// Logger and tracer are optional.
	rabbit := rabbitmq.NewRabbitMQ(consumer, user, pass, host, port, config, WithLogger(customLogger))
	defer rabbit.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFullQueue = errors.New("publish queue is full")

Functions

func ExtractMessageHeaders

func ExtractMessageHeaders(ctx context.Context, headers map[string]string) context.Context

ExtractAMQPHeaders extracts the trace data from the header and puts it into the returned context.

Types

type Config

type Config struct {
	QueueSize         int           // Max number of messages internally queued for publishing.
	MaxWorkers        int           // Max number of concurrent workers per operation type.
	ReconnectInterval time.Duration // Time between reconnect attempts.

	// Settings for the internal circuit breaker.
	MaxRequests   uint32        // Number of requests allowed to half-open state.
	ClearInterval time.Duration // Time after which failed calls count is cleared.
	ClosedTimeout time.Duration // Time after which closed state becomes half-open.
}

func DefaultConfig

func DefaultConfig() Config

type ContentType

type ContentType string
const (
	ContentTypeJson ContentType = "application/json"
	ContentTypeText ContentType = "text/plain"
)

type Logger

type Logger interface {
	Log(ctx context.Context, msg string, keyvals ...interface{})
}

type Message

type Message struct {
	Route

	Body        []byte
	ContentType ContentType
	Timestamp   time.Time
	Headers     map[string]string
}

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithLogger

func WithLogger(logger Logger) Option

func WithTracer

func WithTracer(tracer trace.Tracer) Option

type RabbitMQ

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

func NewRabbitMQ

func NewRabbitMQ(consumer, user, pass, host, port string, config Config, opts ...Option) *RabbitMQ

NewRabbitMQ returns a new initialized connection struct. It will manage the active connection in the background. Connection should be closed in order to shut it down gracefully.

func example() {
	user := "guest"
	pass := "guest"
	host := "localhost"
	port := "5672"
	consumer := "user-service" //  Unique name for each consumer used to sign messages.

	var customLogger Logger

	// You can specify your own config or use rabbitmq.DefaultConfig() instead.
	config := Config{
		QueueSize:         100,
		MaxWorkers:        50,
		ReconnectInterval: time.Second * 2,
		MaxRequests:       5,
		ClearInterval:     time.Second * 5,
		ClosedTimeout:     time.Second * 5,
	}

	// Logger and tracer are optional.
	rabbit := rabbitmq.NewRabbitMQ(consumer, user, pass, host, port, config, WithLogger(customLogger))
	defer rabbit.Close()
}

func (*RabbitMQ) Close

func (mq *RabbitMQ) Close() error

Close closes active connection gracefully.

func (*RabbitMQ) Consume

func (mq *RabbitMQ) Consume(ctx context.Context, command string, route Route) (<-chan Message, error)

func (*RabbitMQ) Enqueue

func (mq *RabbitMQ) Enqueue(msg Message) error

enqueue appends a message to the publishQueue and return a non-nil error if the queue is full.

func (*RabbitMQ) Publish

func (mq *RabbitMQ) Publish(ctx context.Context, msg Message) error

type Route

type Route struct {
	ExchangeName string
	ExchangeType string
	RoutingKey   string
}

Jump to

Keyboard shortcuts

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