Skip to content

Commit

Permalink
add backoff retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMwita committed May 23, 2024
1 parent 3c80c6b commit e84bfe3
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/sms/sms_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<<uint(retry)) * time.Second
time.Sleep(delay)
}

return SmsSenderResponse{}, fmt.Errorf("max retries reached")
}

0 comments on commit e84bfe3

Please sign in to comment.