houseofcat / turbocookedrabbit

A user friendly RabbitMQ library written in Golang.
MIT License
107 stars 20 forks source link

Handler for reconnection errors #34

Closed tiggerite closed 2 years ago

tiggerite commented 2 years ago

Currently there is no way to handle when reconnection is failing, and there is no feedback for logs either for Splunk logging and so forth. Even this wouldn't really be enough on its own as without a handling mechanism, it's not possible to write metrics for these errors to enable alerting and so on.

I've carefully crafted this PR to allow for this extra handler, which can be hooked into by first creating a ConnectionPool via the new NewConnectionPoolWithUnhealthyHandler or NewConnectionPoolWithHandlers "constructors" and then passing this into the new NewRabbitServiceWithConnectionPool "constructor" for the rabbit service. This way, no existing underlying logic changes and there are no breaking API changes either.

Simple example of the latter in action.

        connectionPool, err := tcr.NewConnectionPoolWithHandlers(poolConfig, processError, processUnhealthy)
        if err != nil {
            fmt.Printf("error during setup connection pool: %s", err.Error())
            panic(err)
        }

        var seasoning = &tcr.RabbitSeasoning{
            PoolConfig:      poolConfig,
            ConsumerConfigs: consumerConfigs,
            PublisherConfig: publisherConfig,
            EncryptionConfig:  &tcr.EncryptionConfig{},
            CompressionConfig: &tcr.CompressionConfig{},
        }

        rs, err := tcr.NewRabbitServiceWithConnectionPool(connectionPool, seasoning, "", "", nil, nil)
func processError(err error) {
    fmt.Printf("%s: Rabbit consumer - Error: %s\r\n", time.Now(), err)
}

func processUnhealthy(err error) {
    fmt.Printf("%s: Rabbit connection unhealthy - Error: %s\r\n", time.Now(), err)
}
houseofcat commented 2 years ago

Thanks for your enthusiasm :)

Been really busy with life and I have finally got to resetting up Golang/Erlang/VsCode/RabbitMQ on my home computer.