Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cluster support via Resolver #156

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions connection.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
package rabbitmq

import (
"math/rand"

amqp "github.com/rabbitmq/amqp091-go"
"github.com/wagslane/go-rabbitmq/internal/connectionmanager"
)

type Resolver = connectionmanager.Resolver

type StaticResolver struct {
urls []string
shuffe bool
}

func (r *StaticResolver) Resolve() ([]string, error) {
// TODO: move to slices.Clone when supported Go versions > 1.21
var urls []string
urls = append(urls, r.urls...)

if r.shuffe {
rand.Shuffle(len(urls), func(i, j int) {
urls[i], urls[j] = urls[j], urls[i]
})
}
return urls, nil
}

func NewStaticResolver(urls []string, shuffle bool) *StaticResolver {
return &StaticResolver{urls: urls}
}

// Conn manages the connection to a rabbit cluster
// it is intended to be shared across publishers and consumers
type Conn struct {
Expand All @@ -22,14 +48,18 @@ type Conn struct {
type Config amqp.Config

// NewConn creates a new connection manager
func NewConn(url string, optionFuncs ...func(*ConnectionOptions)) (*Conn, error) {
func NewConn(url string, opts ...func(*ConnectionOptions)) (*Conn, error) {
return NewClusterConn(NewStaticResolver([]string{url}, false), opts...)
}

func NewClusterConn(resolver Resolver, opts ...func(*ConnectionOptions)) (*Conn, error) {
defaultOptions := getDefaultConnectionOptions()
options := &defaultOptions
for _, optionFunc := range optionFuncs {
optionFunc(options)
for _, optFn := range opts {
optFn(options)
}

manager, err := connectionmanager.NewConnectionManager(url, amqp.Config(options.Config), options.Logger, options.ReconnectInterval)
manager, err := connectionmanager.NewConnectionManager(resolver, amqp.Config(options.Config), options.Logger, options.ReconnectInterval)
if err != nil {
return nil, err
}
Expand Down
25 changes: 25 additions & 0 deletions examples/cluster/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"log"

rabbitmq "github.com/wagslane/go-rabbitmq"
)

func main() {
resolver := rabbitmq.NewStaticResolver(
[]string{
"amqp://guest:guest@host1",
"amqp://guest:guest@host2",
"amqp://guest:guest@host3",
},
false, /* shuffle */
)

conn, err := rabbitmq.NewClusterConn(resolver)
if err != nil {
log.Fatal(err)
}
defer conn.Close()

}
40 changes: 34 additions & 6 deletions internal/connectionmanager/connection_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package connectionmanager

import (
"errors"
"fmt"
"sync"
"time"

Expand All @@ -9,10 +11,14 @@ import (
"github.com/wagslane/go-rabbitmq/internal/logger"
)

type Resolver interface {
Resolve() ([]string, error)
}

// ConnectionManager -
type ConnectionManager struct {
logger logger.Logger
url string
resolver Resolver
connection *amqp.Connection
amqpConfig amqp.Config
connectionMux *sync.RWMutex
Expand All @@ -22,15 +28,36 @@ type ConnectionManager struct {
dispatcher *dispatcher.Dispatcher
}

// dial will attempt to connect to the a list of urls in the order they are
// given.
func dial(log logger.Logger, resolver Resolver, conf amqp.Config) (*amqp.Connection, error) {
urls, err := resolver.Resolve()
if err != nil {
return nil, fmt.Errorf("error resolving amqp server urls: %w", err)
}

var errs []error
for _, url := range urls {
conn, err := amqp.DialConfig(url, amqp.Config(conf))
if err == nil {
return conn, err
}
log.Warnf("failed to connect to amqp server %s: %v", url, err)
errs = append(errs, err)
}
return nil, errors.Join(errs...)
}

// NewConnectionManager creates a new connection manager
func NewConnectionManager(url string, conf amqp.Config, log logger.Logger, reconnectInterval time.Duration) (*ConnectionManager, error) {
conn, err := amqp.DialConfig(url, amqp.Config(conf))
func NewConnectionManager(resolver Resolver, conf amqp.Config, log logger.Logger, reconnectInterval time.Duration) (*ConnectionManager, error) {
conn, err := dial(log, resolver, amqp.Config(conf))
if err != nil {
return nil, err
}

connManager := ConnectionManager{
logger: log,
url: url,
resolver: resolver,
connection: conn,
amqpConfig: conf,
connectionMux: &sync.RWMutex{},
Expand Down Expand Up @@ -125,7 +152,8 @@ func (connManager *ConnectionManager) reconnectLoop() {
func (connManager *ConnectionManager) reconnect() error {
connManager.connectionMux.Lock()
defer connManager.connectionMux.Unlock()
newConn, err := amqp.DialConfig(connManager.url, amqp.Config(connManager.amqpConfig))

conn, err := dial(connManager.logger, connManager.resolver, amqp.Config(connManager.amqpConfig))
if err != nil {
return err
}
Expand All @@ -134,6 +162,6 @@ func (connManager *ConnectionManager) reconnect() error {
connManager.logger.Warnf("error closing connection while reconnecting: %v", err)
}

connManager.connection = newConn
connManager.connection = conn
return nil
}