Skip to content

Commit

Permalink
resolve #33 (#34)
Browse files Browse the repository at this point in the history
Added cluster dsn and cluster connection
  • Loading branch information
doranych authored Feb 21, 2021
1 parent 33945af commit 0d00eb3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
vendor/
go.sum
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Supported configuration tags:
Available options:

```yaml
dsn: "amqp://login:password@host:port/virtual_host"
dsn: "amqp://login:password@host:port/virtual_host" # Use comma separated list for cluster connection
reconnect_delay: 5s # Interval between connection tries. Check https://golang.org/pkg/time/#ParseDuration for details.
test_mode: false # Switches library to use mocked broker. Defaults to false.
exchanges:
Expand Down
10 changes: 9 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mq

import (
"strings"
"time"

"github.com/streadway/amqp"
Expand All @@ -24,14 +25,21 @@ type Config struct {
Queues Queues `mapstructure:"queues" json:"queues" yaml:"queues"`
Producers Producers `mapstructure:"producers" json:"producers" yaml:"producers"`
Consumers Consumers `mapstructure:"consumers" json:"consumers" yaml:"consumers"`
dsnList []string
}

// Traverses the config tree and fixes option keys name.
func (config Config) normalize() {
func (config *Config) normalize() {
config.Exchanges.normalize()
config.Queues.normalize()
config.Producers.normalize()
config.Consumers.normalize()
config.dsnClusterParse()
}

// Parse DSN to cluster list
func (config *Config) dsnClusterParse() {
config.dsnList = strings.Split(config.DSN, ",")
}

// Exchanges describes configuration list for exchanges.
Expand Down
16 changes: 14 additions & 2 deletions mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package mq
import (
"fmt"
"net"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -55,6 +56,10 @@ type mq struct {
consumers *consumersRegistry
producers *producersRegistry
reconnectStatus int32 // Defines whether client is trying to reconnect or not.
cluster struct {
sync.Once
currentNode int32
}
}

// New initializes AMQP connection to the message broker
Expand Down Expand Up @@ -169,11 +174,18 @@ func (mq *mq) connect() error {
}

func (mq *mq) createConnection() (conn, error) {
mq.cluster.Do(func() { atomic.StoreInt32(&mq.cluster.currentNode, -1) })
atomic.AddInt32(&mq.cluster.currentNode, 1)
if int(mq.cluster.currentNode) >= len(mq.config.dsnList) {
atomic.StoreInt32(&mq.cluster.currentNode, 0)
}
dsn := mq.config.dsnList[mq.cluster.currentNode]

if brokerIsMocked || mq.config.TestMode {
return amqptest.Dial(mq.config.DSN)
return amqptest.Dial(dsn)
}

return amqp.Dial(mq.config.DSN)
return amqp.Dial(dsn)
}

// Register close handler.
Expand Down
37 changes: 37 additions & 0 deletions mq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,43 @@ func TestMq_New_InvalidConsumerConfiguration(t *testing.T) {
}
}

func Test_mq_createConnection(t *testing.T) {
cases := []struct {
name string
dsn string
checkList []string
}{
{name: "single dsn", dsn: dsnForTests, checkList: []string{dsnForTests}},
{name: "cluster dsn", dsn: "amqp://guest:guest@localhost:5672/1,amqp://guest:guest@localhost:5672/2,amqp://guest:guest@localhost:5672/3",
checkList: []string{"amqp://guest:guest@localhost:5672/1", "amqp://guest:guest@localhost:5672/2", "amqp://guest:guest@localhost:5672/3", "amqp://guest:guest@localhost:5672/1"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
cfg := newDefaultConfig()
cfg.DSN = tc.dsn
cfg.TestMode = true
cfg.normalize()

mq := &mq{
config: cfg,
errorChannel: make(chan error),
internalErrorChannel: make(chan error),
consumers: newConsumersRegistry(len(cfg.Consumers)),
producers: newProducersRegistry(len(cfg.Producers)),
}
defer mq.Close()
for _, dsn := range tc.checkList {
_, _ = mq.createConnection()
curBroker := mq.config.dsnList[mq.cluster.currentNode]
if curBroker != dsn {
t.Errorf("createConnection() current broker %v, expected broker %v", curBroker, dsn)
return
}
}
})
}
}

func assertNoMqError(t *testing.T, mq MQ) {
select {
case err := <-mq.Error():
Expand Down

0 comments on commit 0d00eb3

Please sign in to comment.