Skip to content

Commit

Permalink
Merge pull request #420 from SparkleBo/feature-bus-tls
Browse files Browse the repository at this point in the history
Add bus tls config
  • Loading branch information
fenngwd authored Aug 26, 2022
2 parents 3157fb5 + 94fff5d commit 13d7ba3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/gobay/pkged.go

Large diffs are not rendered by default.

27 changes: 21 additions & 6 deletions extensions/busext/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package busext

import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -96,9 +97,17 @@ func (b *BusExt) Init(app *gobay.Application) error {
b.pushFunc = b.doPush
b.notifyChanBlock = make(chan error)

var tlsConfig *tls.Config
if b.config.GetBool("tls") {
if err := b.config.UnmarshalKey("TLSConfig", &tlsConfig); err != nil {
b.ErrorLogger.Printf("unmarshal TLSConfig failed: %v\n", err)
return err
}
}

b.mocked = b.config.GetBool("mocked")
if !b.mocked {
go b.handleReconnect(b.brokerUrl)
go b.handleReconnect(b.brokerUrl, tlsConfig)
} else {
b.isReady = true
}
Expand Down Expand Up @@ -295,12 +304,12 @@ func (b *BusExt) Consume() error {
return nil
}

func (b *BusExt) handleReconnect(brokerUrl string) {
func (b *BusExt) handleReconnect(brokerUrl string, tlsConfig *tls.Config) {
for {
b.isReady = false
log.Printf("Attempting to connect to %v\n", brokerUrl)
log.Printf("Attempting to connect to %v tlsConfig: %v\n", brokerUrl, tlsConfig)

conn, err := b.connect(brokerUrl)
conn, err := b.connect(brokerUrl, tlsConfig)

if err != nil {
b.ErrorLogger.Printf("Failed to connect: %v. Retrying...\n", err)
Expand All @@ -318,8 +327,14 @@ func (b *BusExt) handleReconnect(brokerUrl string) {
}
}

func (b *BusExt) connect(brokerUrl string) (*amqp.Connection, error) {
conn, err := amqp.Dial(brokerUrl)
func (b *BusExt) connect(brokerUrl string, tlsConfig *tls.Config) (*amqp.Connection, error) {
var conn *amqp.Connection
var err error
if tlsConfig != nil {
conn, err = amqp.DialTLS(brokerUrl, tlsConfig)
} else {
conn, err = amqp.Dial(brokerUrl)
}

if err != nil {
return nil, err
Expand Down

0 comments on commit 13d7ba3

Please sign in to comment.