Skip to content

Commit

Permalink
Add support for retries in uptimerobot (#625)
Browse files Browse the repository at this point in the history
* Add support for retries in uptimerobot

* #minor bump
  • Loading branch information
MuneebAijaz authored Dec 24, 2024
1 parent 86a40ae commit 18239ff
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
vendor
.idea
\.vscode/
out
/IngressMonitorController
/build/_output/bin/
Expand Down
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {
"OPERATOR_NAMESPACE": "stakater-ingress-monitor-controller"
}
}

]
}
6 changes: 5 additions & 1 deletion pkg/http/httpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type HttpClient struct {
type HttpResponse struct {
StatusCode int
Bytes []byte
Header http.Header
}

func CreateHttpClient(url string) *HttpClient {
Expand Down Expand Up @@ -53,7 +54,10 @@ func (client *HttpClient) RequestWithHeaders(requestType string, body []byte, he
log.Error(nil, "got empty response")
}

httpResponse := HttpResponse{StatusCode: response.StatusCode}
httpResponse := HttpResponse{
StatusCode: response.StatusCode,
Header: response.Header,
}

defer response.Body.Close()
responseBytes, _ := io.ReadAll(response.Body)
Expand Down
32 changes: 32 additions & 0 deletions pkg/monitors/uptimerobot/uptime-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"strconv"
"strings"
"time"

endpointmonitorv1alpha1 "github.com/stakater/IngressMonitorController/v2/api/v1alpha1"
"github.com/stakater/IngressMonitorController/v2/pkg/config"
Expand Down Expand Up @@ -67,6 +68,17 @@ func (monitor *UpTimeMonitorService) GetByName(name string) (*models.Monitor, er
}

return nil, nil
} else if response.StatusCode == Http.StatusTooManyRequests {
log.Info("Too many requests, Monitor waiting for timeout: " + name)
retryAfter := response.Header.Get("Retry-After")
if retryAfter != "" {
seconds, err := strconv.Atoi(retryAfter)
if err == nil {
time.Sleep(time.Duration(seconds) * time.Second)
return monitor.GetByName(name) // Retry after the specified delay

}
}
}

errorString := "GetByName Request failed for name: " + name + ". Status Code: " + strconv.Itoa(response.StatusCode)
Expand Down Expand Up @@ -152,6 +164,16 @@ func (monitor *UpTimeMonitorService) Add(m models.Monitor) {
} else {
log.Info("Monitor couldn't be added: " + m.Name + ". Error: " + f.Error.Message)
}
} else if response.StatusCode == Http.StatusTooManyRequests {
log.Info("Too many requests, Monitor waiting for timeout: " + m.Name)
retryAfter := response.Header.Get("Retry-After")
if retryAfter != "" {
seconds, err := strconv.Atoi(retryAfter)
if err == nil {
time.Sleep(time.Duration(seconds) * time.Second)
monitor.Add(m) // Retry after the specified delay
}
}
} else {
log.Info("AddMonitor Request failed. Status Code: " + strconv.Itoa(response.StatusCode))
}
Expand All @@ -178,6 +200,16 @@ func (monitor *UpTimeMonitorService) Update(m models.Monitor) {
} else {
log.Info("Monitor couldn't be updated: " + m.Name + ". Error: " + f.Error.Message)
}
} else if response.StatusCode == Http.StatusTooManyRequests {
log.Info("Too many requests, Monitor waiting for timeout: " + m.Name)
retryAfter := response.Header.Get("Retry-After")
if retryAfter != "" {
seconds, err := strconv.Atoi(retryAfter)
if err == nil {
time.Sleep(time.Duration(seconds) * time.Second)
monitor.Update(m) // Retry after the specified delay
}
}
} else {
log.Info("UpdateMonitor Request failed. Status Code: " + strconv.Itoa(response.StatusCode))
}
Expand Down

0 comments on commit 18239ff

Please sign in to comment.