Categorygithub.com/samuelkuklis/go-rabbitmq
modulepackage
0.6.4
Repository: https://github.com/samuelkuklis/go-rabbitmq.git
Documentation: pkg.go.dev

# README

go-rabbitmq

Wrapper of streadway/amqp that provides reconnection logic and sane defaults. Hit the project with a star if you find it useful ⭐

Supported by Qvault

Deploy

Motivation

Streadway's AMQP library is currently the most robust and well-supported Go client I'm aware of. It's a fantastic option and I recommend starting there and seeing if it fulfills your needs. Their project has made an effort to stay within the scope of the AMQP protocol, as such, no reconnection logic and few ease-of-use abstractions are provided.

The goal with go-rabbitmq is to still provide most all of the nitty-gritty functionality of AMQP, but to make it easier to work with via a higher-level API. Particularly:

  • Automatic reconnection
  • Multithreaded consumers via a handler function
  • Reasonable defaults
  • Flow control handling

⚙️ Installation

Outside of a Go module:

go get github.com/samuelkuklis/go-rabbitmq

🚀 Quick Start Consumer

Default options

consumer, err := rabbitmq.NewConsumer("amqp://user:pass@localhost")
if err != nil {
    log.Fatal(err)
}
err = consumer.StartConsuming(
    func(d rabbitmq.Delivery) bool {
        log.Printf("consumed: %v", string(d.Body))
        // true to ACK, false to NACK
        return true
    },
    "my_queue",
    []string{"routing_key1", "routing_key2"}
)
if err != nil {
    log.Fatal(err)
}

With options

consumer, err := rabbitmq.NewConsumer(
    "amqp://user:pass@localhost",
    rabbitmq.WithConsumerOptionsLogging,
)
if err != nil {
    log.Fatal(err)
}
err = consumer.StartConsuming(
    func(d rabbitmq.Delivery) bool {
        log.Printf("consumed: %v", string(d.Body))
        // true to ACK, false to NACK
        return true
    },
    "my_queue",
    []string{"routing_key1", "routing_key2"},
    rabbitmq.WithConsumeOptionsConcurrency(10),
    rabbitmq.WithConsumeOptionsQueueDurable,
    rabbitmq.WithConsumeOptionsQuorum,
)
if err != nil {
    log.Fatal(err)
}

🚀 Quick Start Publisher

Default options

publisher, returns, err := rabbitmq.NewPublisher("amqp://user:pass@localhost")
if err != nil {
    log.Fatal(err)
}
err = publisher.Publish([]byte("hello, world"), []string{"routing_key"})
if err != nil {
    log.Fatal(err)
}

With options

publisher, returns, err := rabbitmq.NewPublisher(
    "amqp://user:pass@localhost",
    // can pass nothing for no logging
    rabbitmq.WithPublisherOptionsLogging,
)
if err != nil {
    log.Fatal(err)
}
err = publisher.Publish(
    []byte("hello, world"),
    []string{"routing_key"},
    // leave blank for defaults
    rabbitmq.WithPublishOptionsContentType("application/json"),
    rabbitmq.WithPublishOptionsMandatory,
    rabbitmq.WithPublishOptionsPersistentDelivery,
)
if err != nil {
    log.Fatal(err)
}

go func() {
    for r := range returns {
        log.Printf("message returned from server: %s", string(r.Body))
    }
}()

💬 Contact

Twitter Follow

Submit an issue (above in the issues tab)

Transient Dependencies

My goal is to keep dependencies limited to 1, github.com/streadway/amqp.

👏 Contributing

I love help! Contribute by forking the repo and opening pull requests. Please ensure that your code passes the existing tests and linting, and write tests to test your changes if applicable.

All pull requests should be submitted to the main branch.

# Packages

No description provided by the author

# Functions

NewConsumer returns a new Consumer connected to the given rabbitmq server.
No description provided by the author
NewPublisher returns a new publisher with an open channel to the cluster.
No description provided by the author
WithConsumeOptionsBindingExchangeArgs returns a function that sets the binding exchange arguments that are specific to the server's implementation of the exchange.
WithConsumeOptionsBindingExchangeAutoDelete returns a function that sets the binding exchange autoDelete flag.
WithConsumeOptionsBindingExchangeDurable returns a function that sets the binding exchange durable flag.
WithConsumeOptionsBindingExchangeInternal returns a function that sets the binding exchange internal flag.
WithConsumeOptionsBindingExchangeKind returns a function that sets the binding exchange kind/type.
WithConsumeOptionsBindingExchangeName returns a function that sets the exchange name the queue will be bound to.
WithConsumeOptionsBindingExchangeNoWait returns a function that sets the binding exchange noWait flag.
WithConsumeOptionsBindingNoWait sets the bindings to nowait, which means if the queue can not be bound the channel will not be closed with an error.
WithConsumeOptionsConcurrency returns a function that sets the concurrency, which means that many goroutines will be spawned to run the provided handler on messages.
WithConsumeOptionsConsumerExclusive sets the consumer to exclusive, which means the server will ensure that this is the sole consumer from this queue.
WithConsumeOptionsConsumerName returns a function that sets the name on the server of this consumer if unset a random name will be given.
WithConsumeOptionsConsumerNoWait sets the consumer to nowait, which means it does not wait for the server to confirm the request and immediately begin deliveries.
WithConsumeOptionsQOSGlobal sets the qos on the channel to global, which means these QOS settings apply to ALL existing and future consumers on all channels on the same connection.
WithConsumeOptionsQOSPrefetch returns a function that sets the prefetch count, which means that many messages will be fetched from the server in advance to help with throughput.
WithConsumeOptionsQueueAutoDelete sets the queue to auto delete, which means it will be deleted when there are no more conusmers on it.
WithConsumeOptionsQueueDurable sets the queue to durable, which means it won't be destroyed when the server restarts.
WithConsumeOptionsQueueExclusive sets the queue to exclusive, which means it's are only accessible by the connection that declares it and will be deleted when the connection closes.
WithConsumeOptionsQueueNoWait sets the queue to nowait, which means the queue will assume to be declared on the server.
WithConsumeOptionsQuorum sets the queue a quorum type, which means multiple nodes in the cluster will have the messages distributed amongst them for higher reliability.
WithConsumerOptionsLogger sets logging to a custom interface.
WithConsumerOptionsLogging sets a logger to log to stdout.
WithPublisherOptionsLogger sets logging to a custom interface.
WithPublisherOptionsLogging sets logging to true on the consumer options.
WithPublishOptionsContentType returns a function that sets the content type, i.e.
WithPublishOptionsExchange returns a function that sets the exchange to publish to.
WithPublishOptionsExpiration returns a function that sets the expiry/TTL of a message.
WithPublishOptionsHeaders returns a function that sets message header values, i.e.
WithPublishOptionsImmediate makes the publishing immediate, which means when a consumer is not available to immediately handle the new message, a message will be sent back on the returns channel for you to handle.
WithPublishOptionsMandatory makes the publishing mandatory, which means when a queue is not bound to the routing key a message will be sent back on the returns channel for you to handle.
WithPublishOptionsPersistentDelivery sets the message to persist.

# Constants

DeliveryMode.
DeliveryMode.

# Structs

BindingExchangeOptions are used when binding to an exchange.
ConsumeOptions are used to describe how a new consumer will be created.
Consumer allows you to create and connect to queues for data consumption.
ConsumerOptions are used to describe a consumer's configuration.
Delivery captures the fields for a previously delivered message resident in a queue to be delivered by the server to a consumer from Channel.Consume or Channel.Get.
Publisher allows you to publish messages safely across an open connection.
PublisherOptions are used to describe a publisher's configuration.
PublishOptions are used to control how data is published.
Return captures a flattened struct of fields returned by the server when a Publishing is unable to be delivered either due to the `mandatory` flag set and no route found, or `immediate` flag set and no free consumer.

# Interfaces

Logger is the interface to send logs to.

# Type aliases

Table stores user supplied fields of the following types: bool byte float32 float64 int int16 int32 int64 nil string time.Time amqp.Decimal amqp.Table []byte []interface{} - containing above types Functions taking a table will immediately fail when the table contains a value of an unsupported type.