diff --git a/cmd/terrarium-bot/http.go b/cmd/terrarium-bot/http.go index 36cad9b..36f583a 100644 --- a/cmd/terrarium-bot/http.go +++ b/cmd/terrarium-bot/http.go @@ -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 diff --git a/cmd/terrarium-bot/http_test.go b/cmd/terrarium-bot/http_test.go index 1a44dae..9d99a3b 100644 --- a/cmd/terrarium-bot/http_test.go +++ b/cmd/terrarium-bot/http_test.go @@ -1,10 +1,14 @@ package main import ( + "bytes" "fmt" + "log" "net/http" "net/http/httptest" + "os" "reflect" + "strings" "testing" ) @@ -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) } @@ -43,7 +47,7 @@ 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") } @@ -51,4 +55,25 @@ func TestSendRequest(t *testing.T) { 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) + }) } diff --git a/cmd/terrarium-bot/sensor.go b/cmd/terrarium-bot/sensor.go index fea9e1e..65f397a 100644 --- a/cmd/terrarium-bot/sensor.go +++ b/cmd/terrarium-bot/sensor.go @@ -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 diff --git a/cmd/terrarium-bot/switch.go b/cmd/terrarium-bot/switch.go index 903fd85..b616034 100644 --- a/cmd/terrarium-bot/switch.go +++ b/cmd/terrarium-bot/switch.go @@ -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 { @@ -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 { @@ -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 {