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 support for retries in uptimerobot #625

Merged
merged 2 commits into from
Dec 24, 2024
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
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
Loading