From 18239ff619b991b1f6a2917f8e2f772f1badd9d8 Mon Sep 17 00:00:00 2001 From: Muneeb Aijaz <43588696+MuneebAijaz@users.noreply.github.com> Date: Wed, 25 Dec 2024 03:14:49 +0500 Subject: [PATCH] Add support for retries in uptimerobot (#625) * Add support for retries in uptimerobot * #minor bump --- .gitignore | 1 - .vscode/launch.json | 19 +++++++++++++ pkg/http/httpClient.go | 6 +++- pkg/monitors/uptimerobot/uptime-monitor.go | 32 ++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.gitignore b/.gitignore index c8b4f57b..3ab3eac2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ vendor .idea -\.vscode/ out /IngressMonitorController /build/_output/bin/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..1154f21e --- /dev/null +++ b/.vscode/launch.json @@ -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" + } + } + + ] +} \ No newline at end of file diff --git a/pkg/http/httpClient.go b/pkg/http/httpClient.go index 334441a1..1ae626ce 100644 --- a/pkg/http/httpClient.go +++ b/pkg/http/httpClient.go @@ -17,6 +17,7 @@ type HttpClient struct { type HttpResponse struct { StatusCode int Bytes []byte + Header http.Header } func CreateHttpClient(url string) *HttpClient { @@ -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) diff --git a/pkg/monitors/uptimerobot/uptime-monitor.go b/pkg/monitors/uptimerobot/uptime-monitor.go index 9a3cac96..838a67d7 100644 --- a/pkg/monitors/uptimerobot/uptime-monitor.go +++ b/pkg/monitors/uptimerobot/uptime-monitor.go @@ -9,6 +9,7 @@ import ( "reflect" "strconv" "strings" + "time" endpointmonitorv1alpha1 "github.com/stakater/IngressMonitorController/v2/api/v1alpha1" "github.com/stakater/IngressMonitorController/v2/pkg/config" @@ -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) @@ -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)) } @@ -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)) }