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

Allow configurable timeout for init amq connection #69

Merged
merged 1 commit into from
Jan 19, 2023
Merged
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
15 changes: 9 additions & 6 deletions v1/amqp/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import (
)

var (
instance *AMQP
retryTimeout = 500 * time.Millisecond
cancelTimeout = 300 * time.Second
instance *AMQP
retryTimeout = 500 * time.Millisecond
)

// AMQP exposes amqp api methods
Expand All @@ -40,11 +39,12 @@ type AMQP struct {
}

// GetAMQPInstance get event instance
func GetAMQPInstance(amqpHost string, dataIn <-chan *channel.DataChan, dataOut chan<- *channel.DataChan, closeCh <-chan struct{}) (*AMQP, error) {
ctx, cancel := context.WithTimeout(context.Background(), cancelTimeout)
func GetAMQPInstance(amqpHost string, dataIn <-chan *channel.DataChan, dataOut chan<- *channel.DataChan, closeCh <-chan struct{}, amqInitTimeout time.Duration) (*AMQP, error) {
ctx, cancel := context.WithTimeout(context.Background(), amqInitTimeout)
defer cancel()
var router *amqp1.Router
var err error
isFirstFail := true
for {
select {
case <-ctx.Done():
Expand All @@ -53,7 +53,10 @@ func GetAMQPInstance(amqpHost string, dataIn <-chan *channel.DataChan, dataOut c
}
router, err = amqp1.InitServer(amqpHost, dataIn, dataOut, closeCh)
if err != nil {
log.Info("retrying connecting to amqp.")
if isFirstFail {
log.Infof("retrying connecting to amqp every %s for %s", retryTimeout, amqInitTimeout)
isFirstFail = false
}
time.Sleep(retryTimeout)
continue
}
Expand Down
6 changes: 4 additions & 2 deletions v1/amqp/amqp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package amqp_test

import (
"testing"
"time"

"github.com/redhat-cne/sdk-go/pkg/channel"
api "github.com/redhat-cne/sdk-go/v1/amqp"
Expand All @@ -29,11 +30,12 @@ var (
in = make(chan *channel.DataChan)
out = make(chan *channel.DataChan)
close = make(chan struct{})
globalInstance, _ = api.GetAMQPInstance(s, in, out, close)
timeout = 1 * time.Second
globalInstance, _ = api.GetAMQPInstance(s, in, out, close, timeout)
)

func TestAPI_GetAPIInstance(t *testing.T) {
localInstance, err := api.GetAMQPInstance(s, in, out, close)
localInstance, err := api.GetAMQPInstance(s, in, out, close, timeout)
if err != nil {
t.Skipf("ampq.Dial(%#v): %v", localInstance, err)
}
Expand Down