Giter Site home page Giter Site logo

furdarius / rabbitroutine Goto Github PK

View Code? Open in Web Editor NEW
106.0 3.0 16.0 1.68 MB

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

Home Page: https://pkg.go.dev/github.com/furdarius/rabbitroutine

License: MIT License

Go 100.00%
rabbitmq amqp failover go

rabbitroutine's Introduction

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 ./...

rabbitroutine's People

Contributors

kslr avatar mengdj avatar pedroreys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rabbitroutine's Issues

Compile error

Build like this:

SET CGO_ENABLED=0
SET GOOS=windows
SET GOARCH=386
go build

Got an error.
.\connector.go:45:25: constant 9223372036854775807 overflows uint.\connector.go:45:25: constant 9223372036854775807 overflows uint

it should be like this in line 45:
cfg.ReconnectAttempts = math.MaxUint32

question: gracefull shutdown for consumer

i checked examples, test files, etc. but not found how to correctly make a gracefull shutdown.

how i see it should be (may be i am wrong):

  1. stop receiving new tasks in channel
  2. wait to processing tasks finished
  3. close channel
  4. close connection

could you provide an example to do it in right way?

questions about reconnect behavior

hey! my team is working on a project using rabbitroutine (which has been great) and have a few questions about reconnect behavior that i can't find documentation for and can't grok from the code.

// Config stores reconnect options.
type Config struct {
// Max reconnect attempts.
ReconnectAttempts uint
// How long to wait between reconnect.
Wait time.Duration
}

  1. Is ReconnectAttempts the number of times rabbitroutine will attempt to reconnect to rabbit throughout the connector's lifetime or the number of times rabbitroutine will attempt to reconnect when a connection is lost
    • e.g. connection is lost and reconnects after 10 attempts, then an hour later connection is lost and reconnects after 20 attempts, max reconnects is 25 attempts, will that fail as it's a total of 10+20 total reconnect attempts?
    • i'm assuming the latter, just want to confirm
  2. What is the behavior when all attempts have been exhausted?
    • we're currently just using rabbitroutine publishers, but curious about behavior on both publisher and consumer
  3. What is the behavior while the connector is attempting to reconnect?
    • will .Publish return an err? will .Publish block while reconnecting?

Unable to close the connection manually

Hi team , I'm unable to close the rabbit connection , if I do so with the help of the channel , but it reconnects the connection again. Is there any way to close the connection on demand gracefully

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.