Skip to content

Commit

Permalink
improve SendRequest func and fix some bugs (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelm41 authored May 22, 2023
1 parent 98328d5 commit 86f048c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions znats/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@ import (
"time"
)

const RequestRetries = 5

func (c *ComponentNats) SendRequest(topic *Topic, data []byte, timeout time.Duration) (error, []byte) {
func (c *ComponentNats) SendRequest(topic *Topic, data []byte, reqRetry int, reqTimeout time.Duration, waitInterval time.Duration) (error, []byte) {
var out []byte
for i := 0; i < RequestRetries; i++ {
response, err := c.NatsConn.Request(topic.FullRoute(), data, timeout)
for i := 0; i < reqRetry; i++ {
response, err := c.NatsConn.Request(topic.FullRoute(), data, reqTimeout)

if err != nil {
if err == nats.ErrTimeout {
zap.S().Errorf("Request to topic '%s' timeout, retrying...", topic.FullRoute())
time.Sleep(5 * time.Second)
time.Sleep(waitInterval)
continue
} else {
return err, nil
}
}

out = response.Data
break
}

return nil, out
}

func (c *ComponentNats) WaitTopicAndSendRequest(topic *Topic, data []byte, timeout time.Duration) (error, []byte) {
func (c *ComponentNats) WaitTopicAndSendRequest(topic *Topic, data []byte, reqRetry int, reqTimeout time.Duration, sleepInterval time.Duration) (error, []byte) {
c.WaitForTopicToExist(topic)
return c.SendRequest(topic, data, timeout)
return c.SendRequest(topic, data, reqRetry, reqTimeout, sleepInterval)
}

0 comments on commit 86f048c

Please sign in to comment.