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

Add retry on http requests #7

Merged
merged 1 commit into from
Mar 30, 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: 13 additions & 2 deletions cmd/terrarium-bot/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@ import (
"log"
"net/http"
"net/http/httputil"
"time"
)

func SendRequest(url string, insecure bool) (map[string]interface{}, int, error) {
func SendRequest(url string, insecure bool, retries int) (map[string]interface{}, int, error) {
result := map[string]interface{}{}
var resp *http.Response
var err error

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}
client := &http.Client{Transport: tr}

resp, err := client.Get(url)
// retry x times if an error occurs, sleep 1 second each time
for i := 0; i < retries; i++ {
Debug("Request attempt %v/%v", i+1, retries)
resp, err = client.Get(url)
if err == nil && resp.StatusCode == 200 {
continue
}
time.Sleep(1 * time.Second)
}
if err != nil {
log.Println(err)
return nil, 0, err
Expand Down
29 changes: 27 additions & 2 deletions cmd/terrarium-bot/http_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package main

import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
"testing"
)

Expand All @@ -28,7 +32,7 @@ func TestSendRequest(t *testing.T) {
defer server.Close()

// Send request to mocked server URL
res, respCode, err := SendRequest(server.URL, false)
res, respCode, err := SendRequest(server.URL, false, 1)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
Expand All @@ -43,12 +47,33 @@ func TestSendRequest(t *testing.T) {
})

t.Run("Invalid URL", func(t *testing.T) {
_, respCode, err := SendRequest("invalid_url", false)
_, respCode, err := SendRequest("invalid_url", false, 1)
if err == nil {
t.Error("Expected error, but got nil")
}
if respCode != 0 {
t.Fatalf("Expected response code 0, got %v", respCode)
}
})

t.Run("Invalid URL with 3 retries", func(t *testing.T) {
config.Debug = true
var buf bytes.Buffer
log.SetOutput(&buf)

_, respCode, err := SendRequest("invalid_url", false, 3)
if err == nil {
t.Error("Expected error, but got nil")
}
if respCode != 0 {
t.Fatalf("Expected response code 0, got %v", respCode)
}

if got := buf.String(); !strings.Contains(got, "3/3") {
t.Errorf("Expected 3 retries: %q", got)
}

config.Debug = false
log.SetOutput(os.Stderr)
})
}
2 changes: 1 addition & 1 deletion cmd/terrarium-bot/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *Sensor) GetValue() int {
}

func (s *Sensor) getSensorValue() int {
r, respCode, err := SendRequest(s.Url, s.Insecure)
r, respCode, err := SendRequest(s.Url, s.Insecure, 3)
if err != nil {
log.Println(err)
return 0
Expand Down
6 changes: 3 additions & 3 deletions cmd/terrarium-bot/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *Switch) getStatus() string {
return s.State
}

r, respCode, err := SendRequest(s.StatusUrl, s.Insecure)
r, respCode, err := SendRequest(s.StatusUrl, s.Insecure, 3)
if err != nil || respCode != 200 {
log.Printf("Switch Offline: %s", s.Id)
for _, n := range config.Notification {
Expand Down Expand Up @@ -121,7 +121,7 @@ func (s *Switch) TurnOn(For string, Reason string) {

s.SetLastAction()
if !config.DryRun {
_, respCode, err := SendRequest(s.On, s.Insecure)
_, respCode, err := SendRequest(s.On, s.Insecure, 3)
if err != nil || respCode != 200 {
log.Printf("Switch Offline: %s", s.Id)
for _, n := range config.Notification {
Expand All @@ -146,7 +146,7 @@ func (s *Switch) TurnOff(reason string) {
}
s.SetLastAction()
if !config.DryRun {
_, respCode, err := SendRequest(s.Off, s.Insecure)
_, respCode, err := SendRequest(s.Off, s.Insecure, 3)
if err != nil || respCode != 200 {
log.Printf("Switch Offline: %s", s.Id)
for _, n := range config.Notification {
Expand Down