From e84bfe3ab6b88666818de353ad28e2333a3dec0e Mon Sep 17 00:00:00 2001 From: MikeMwita Date: Thu, 23 May 2024 23:29:05 +0300 Subject: [PATCH] add backoff retry logic --- pkg/sms/sms_sender.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/sms/sms_sender.go b/pkg/sms/sms_sender.go index 067c8d5..1b6626e 100644 --- a/pkg/sms/sms_sender.go +++ b/pkg/sms/sms_sender.go @@ -3,10 +3,12 @@ package sms import ( "encoding/json" "fmt" - "github.com/google/uuid" "net/http" "net/url" "strings" + "time" + + "github.com/google/uuid" ) type SmsSender struct { @@ -141,3 +143,17 @@ func (s *SmsSender) SendSMS() (SmsSenderResponse, error) { return smsSenderResponse, fmt.Errorf("status code: %d", res.StatusCode) } +// Retry sends an SMS with exponential backoff +func (s *SmsSender) RetrySendSMS(maxRetries int) (SmsSenderResponse, error) { + for retry := 0; retry < maxRetries; retry++ { + response, err := s.SendSMS() + if err == nil { + return response, nil + } + + delay := time.Duration(1<