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

Fix amq router race condition #59

Merged
merged 1 commit into from
Dec 2, 2022
Merged
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
57 changes: 38 additions & 19 deletions v1/amqp/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,58 @@
package amqp

import (
"context"
"sync"
"time"

"github.com/Azure/go-amqp"

cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/redhat-cne/sdk-go/pkg/channel"
"github.com/redhat-cne/sdk-go/pkg/errorhandler"
amqp1 "github.com/redhat-cne/sdk-go/pkg/protocol/amqp"
log "github.com/sirupsen/logrus"
)

var (
instance *AMQP
once sync.Once
instance *AMQP
retryTimeout = 500 * time.Millisecond
cancelTimeout = 30 * time.Second
)

//AMQP exposes amqp api methods
// AMQP exposes amqp api methods
type AMQP struct {
Router *amqp1.Router
}

//GetAMQPInstance get event instance
// GetAMQPInstance get event instance
func GetAMQPInstance(amqpHost string, dataIn <-chan *channel.DataChan, dataOut chan<- *channel.DataChan, closeCh <-chan struct{}) (*AMQP, error) {
once.Do(func() {
router, err := amqp1.InitServer(amqpHost, dataIn, dataOut, closeCh)
if err == nil {
instance = &AMQP{
Router: router,
}
ctx, cancel := context.WithTimeout(context.Background(), cancelTimeout)
defer cancel()
var router *amqp1.Router
var err error
for {
select {
case <-ctx.Done():
return nil, errorhandler.AMQPConnectionError{}
default:
}
})
router, err = amqp1.InitServer(amqpHost, dataIn, dataOut, closeCh)
if err != nil {
log.Info("retrying connecting to amqp.")
time.Sleep(retryTimeout)
continue
}

instance = &AMQP{
Router: router,
}
break
}
if instance == nil || instance.Router == nil {
return nil, errorhandler.AMQPConnectionError{Desc: "amqp connection error"}
return nil, errorhandler.AMQPConnectionError{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removed description ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text might be used for parsing some where

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AMQPConnectionError itself already has description.
Before removing description:

ERRO[0000] error starting amqp at amqp:localhost:5672 error: amqp connection error amqp connection error
WARN[0000] requires QPID router installed to function fully amqp connection error amqp connection error

After removing description:

ERRO[0000] error starting amqp at amqp:localhost:5672 error: amqp connection error
WARN[0000] requires QPID router installed to function fully amqp connection error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

}

if instance.Router.Client == nil {
client, err := instance.Router.NewClient(amqpHost, []amqp.ConnOption{})
if err != nil {
Expand All @@ -58,12 +77,12 @@ func GetAMQPInstance(amqpHost string, dataIn <-chan *channel.DataChan, dataOut c
return instance, nil
}

//Start start amqp processors
// Start start amqp processors
func (a *AMQP) Start(wg *sync.WaitGroup) {
go instance.Router.QDRRouter(wg)
}

//NewSender - create new sender independent of the framework
// NewSender - create new sender independent of the framework
func NewSender(hostName string, port int, address string) (*amqp1.Protocol, error) {
return amqp1.NewSender(hostName, port, address)
}
Expand All @@ -73,7 +92,7 @@ func NewReceiver(hostName string, port int, address string) (*amqp1.Protocol, er
return amqp1.NewReceiver(hostName, port, address)
}

//DeleteSender send publisher address information on a channel to delete its sender object
// DeleteSender send publisher address information on a channel to delete its sender object
func DeleteSender(inChan chan<- *channel.DataChan, address string) {
// go ahead and create QDR to this address
inChan <- &channel.DataChan{
Expand All @@ -83,7 +102,7 @@ func DeleteSender(inChan chan<- *channel.DataChan, address string) {
}
}

//CreateSender send publisher address information on a channel to create it's sender object
// CreateSender send publisher address information on a channel to create it's sender object
func CreateSender(inChan chan<- *channel.DataChan, address string) {
// go ahead and create QDR to this address
inChan <- &channel.DataChan{
Expand All @@ -93,7 +112,7 @@ func CreateSender(inChan chan<- *channel.DataChan, address string) {
}
}

//DeleteListener send subscription address information on a channel to delete its listener object
// DeleteListener send subscription address information on a channel to delete its listener object
func DeleteListener(inChan chan<- *channel.DataChan, address string) {
// go ahead and create QDR listener to this address
inChan <- &channel.DataChan{
Expand All @@ -103,7 +122,7 @@ func DeleteListener(inChan chan<- *channel.DataChan, address string) {
}
}

//CreateListener send subscription address information on a channel to create its listener object
// CreateListener send subscription address information on a channel to create its listener object
func CreateListener(inChan chan<- *channel.DataChan, address string) {
// go ahead and create QDR listener to this address
inChan <- &channel.DataChan{
Expand All @@ -113,7 +132,7 @@ func CreateListener(inChan chan<- *channel.DataChan, address string) {
}
}

//CreateNewStatusListener send status address information on a channel to create its listener object
// CreateNewStatusListener send status address information on a channel to create its listener object
func CreateNewStatusListener(inChan chan<- *channel.DataChan, address string,
onReceiveOverrideFn func(e cloudevents.Event, dataChan *channel.DataChan) error,
processEventFn func(e interface{}) error) {
Expand Down