• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    Go
  • License
    MIT License
  • Created about 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight library that handles RabbitMQ auto-reconnect and publishing retry routine for you.

PkgGoDev rabbitroutine Go Report Card codecov

Rabbitmq Failover Routine

Lightweight library that handles RabbitMQ auto-reconnect and publishing retry routine for you. The library is designed to save the developer from the headache when working with RabbitMQ.

rabbitroutine solves your RabbitMQ reconnection problems:

Usage

go get github.com/furdarius/rabbitroutine

Consuming

You need to implement Consumer and register it with StartConsumer or with StartMultipleConsumers. When connection is established (at first time or after reconnect) Declare method is called. It can be used to declare required RabbitMQ entities (consumer example).

Usage example:

// Consumer declares your own RabbitMQ consumer implementing rabbitroutine.Consumer interface.
type Consumer struct {}
func (c *Consumer) Declare(ctx context.Context, ch *amqp.Channel) error {}
func (c *Consumer) Consume(ctx context.Context, ch *amqp.Channel) error {}

url := "amqp://guest:[email protected]:5672/"

conn := rabbitroutine.NewConnector(rabbitroutine.Config{
    // How long to wait between reconnect
    Wait: 2 * time.Second,
})

ctx := context.Background()

go func() {
    err := conn.Dial(ctx, url)
    if err != nil {
    	log.Println(err)
    }
}()

consumer := &Consumer{}
go func() {
    err := conn.StartConsumer(ctx, consumer)
    if err != nil {
        log.Println(err)
    }
}()

Full example demonstrates messages consuming

Publishing

For publishing FireForgetPublisher and EnsurePublisher implemented. Both of them can be wrapped with RetryPublisher to repeat publishing on errors and mitigate short-term network problems.

Usage example:

ctx := context.Background()

url := "amqp://guest:[email protected]:5672/"

conn := rabbitroutine.NewConnector(rabbitroutine.Config{
    // How long wait between reconnect
    Wait: 2 * time.Second,
})

pool := rabbitroutine.NewPool(conn)
ensurePub := rabbitroutine.NewEnsurePublisher(pool)
pub := rabbitroutine.NewRetryPublisher(
    ensurePub,
    rabbitroutine.PublishMaxAttemptsSetup(16),
    rabbitroutine.PublishDelaySetup(rabbitroutine.LinearDelay(10*time.Millisecond)),
)

go conn.Dial(ctx, url)

err := pub.Publish(ctx, "myexch", "myqueue", amqp.Publishing{Body: []byte("message")})
if err != nil {
    log.Println("publish error:", err)
}

Full example demonstrates messages publishing

Contributing

Pull requests are very much welcomed. Create your pull request, make sure a test or example is included that covers your change and your commits represent coherent changes that include a reason for the change.

To run the integration tests, make sure you have RabbitMQ running on any host

docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.13-management

Then export the environment variable AMQP_URL=amqp://host/ and run go test -tags integration.

AMQP_URL=amqp://guest:[email protected]:5672/ go test -v -race -cpu=1,2 -tags integration -timeout 5s

Use golangci-lint to check code with linters:

golangci-lint run ./...

More Repositories

1

oidconnect-laravel

The OpenIDConnect Laravel package is meant to provide you an opportunity to easily authenticate users using OpenID Connect protocol.
PHP
36
star
2

pgqgo

Small, user-friendly library for consuming PGQ from Go.
Go
12
star
3

jwtee

JWTee - Fast and Flexible library for those who do not want to lose the full power of strong typing when working with JWT in Go
Go
6
star
4

ansible-gitlab

Role for starting Gitlab Omnibus in Docker container
5
star
5

ansible-gitlab-runner

Install and register gitlab-runner on remote machine
5
star
6

steamprotocol

Steamprotocol is library, that can be used to interoperate with Valve's Steam network.
Go
4
star
7

dotfiles-deprecated

My personal dotfiles
Shell
3
star
8

dotfiles

Shell
2
star
9

gouter

Simple, but pure functional, implementation of the Golang HTTP router based on Radix Tree
Go
2
star
10

talk-go-tensorflow

Step by step tutorial to work with Tensorflow SavedModel from Go
Go
2
star
11

pgqexperiment

Postgres Skytools3 PGQ usage experiment
Shell
2
star
12

date

A package to help you work with dates in Go.
Go
2
star
13

sudoku-recognition

Implementation of sudoku recognition on photo and solving.
C++
1
star
14

bower-docker

Lightweight Docker image with stable verison of Bower
1
star
15

gulp-docker

Lightweight Docker image with stable verison of Gulp for using in your build workflow
1
star
16

tcmalloc-example

Hello World Bazel build configuration for Google TCMalloc
C++
1
star
17

ansible-upgrade

Upgrade software on remote machine
1
star
18

gofalsesharing

C
1
star
19

npm-docker

Docker image: last version of npm package manager
1
star
20

pghagrouptest

Set of scripts to automate deployment of Postgres HA group (master + standby) for test only purposes.
Go
1
star