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

Prevent rate limiting by StatusCake #5

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
18 changes: 17 additions & 1 deletion pkg/monitors/statuscake/statuscake-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"strconv"
"strings"
"time"

logf "sigs.k8s.io/controller-runtime/pkg/log"

Expand All @@ -23,6 +24,8 @@ import (
)

var log = logf.Log.WithName("statuscake-monitor")
var cachedMonitors []models.Monitor


// StatusCakeMonitorService is the service structure for StatusCake
type StatusCakeMonitorService struct {
Expand Down Expand Up @@ -328,6 +331,9 @@ func (service *StatusCakeMonitorService) GetByID(id string) (*models.Monitor, er

// GetAll function will fetch all monitors
func (service *StatusCakeMonitorService) GetAll() []models.Monitor {
if len(cachedMonitors) > 0 {
return cachedMonitors
}
var StatusCakeMonitorData []StatusCakeMonitorData
page := 1
for {
Expand All @@ -342,7 +348,8 @@ func (service *StatusCakeMonitorService) GetAll() []models.Monitor {
}
page += 1
}
return StatusCakeMonitorMonitorsToBaseMonitorsMapper(StatusCakeMonitorData)
cachedMonitors = StatusCakeMonitorMonitorsToBaseMonitorsMapper(StatusCakeMonitorData)
return cachedMonitors
}

func (service *StatusCakeMonitorService) fetchMonitors(page int) *StatusCakeMonitor {
Expand Down Expand Up @@ -391,6 +398,7 @@ func (service *StatusCakeMonitorService) fetchMonitors(page int) *StatusCakeMoni

// Add will create a new Monitor
func (service *StatusCakeMonitorService) Add(m models.Monitor) {
cachedMonitors = []models.Monitor{}
u, err := url.Parse(service.url)
if err != nil {
log.Error(err, "Unable to Parse monitor URL")
Expand Down Expand Up @@ -421,10 +429,13 @@ func (service *StatusCakeMonitorService) Add(m models.Monitor) {
log.Error(nil, "Insert Request failed for name: "+m.Name+" with status code "+strconv.Itoa(resp.StatusCode))
log.Error(nil, string(bodyBytes))
}
// Take 1 second pause to prevent being rate limited
time.Sleep(1 * time.Second)
}

// Update will update an existing Monitor
func (service *StatusCakeMonitorService) Update(m models.Monitor) {
cachedMonitors = []models.Monitor{}
u, err := url.Parse(service.url)
if err != nil {
log.Error(err, "Unable to Parse monitor URL")
Expand Down Expand Up @@ -455,10 +466,13 @@ func (service *StatusCakeMonitorService) Update(m models.Monitor) {
log.Error(nil, "Update Request failed for name: "+m.Name+" with status code "+strconv.Itoa(resp.StatusCode))
log.Error(nil, string(bodyBytes))
}
// Take 1 second pause to prevent being rate limited
time.Sleep(1 * time.Second)
}

// Remove will delete an existing Monitor
func (service *StatusCakeMonitorService) Remove(m models.Monitor) {
cachedMonitors = []models.Monitor{}
u, err := url.Parse(service.url)
if err != nil {
log.Error(err, "Unable to Parse monitor URL")
Expand Down Expand Up @@ -489,4 +503,6 @@ func (service *StatusCakeMonitorService) Remove(m models.Monitor) {
log.Error(nil, fmt.Sprintf("Delete Request failed for Monitor: %s with id: %s", m.Name, m.ID))
}
}
// Take 1 second pause to prevent being rate limited
time.Sleep(1 * time.Second)
}
Loading