From 86f048cb8866770024a130ff62257804dcbf1307 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Mon, 22 May 2023 14:55:18 -0300 Subject: [PATCH] improve SendRequest func and fix some bugs (#8) --- znats/request.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/znats/request.go b/znats/request.go index 774c66a..66bb617 100644 --- a/znats/request.go +++ b/znats/request.go @@ -6,17 +6,15 @@ 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 @@ -24,12 +22,13 @@ func (c *ComponentNats) SendRequest(topic *Topic, data []byte, timeout time.Dura } 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) }