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

StatusCake: Handling rate limits when working with Monitors #617

Merged
merged 1 commit into from
Dec 4, 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
43 changes: 38 additions & 5 deletions pkg/monitors/statuscake/statuscake-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"os"
"strconv"
"strings"
"time"

"golang.org/x/time/rate"
logf "sigs.k8s.io/controller-runtime/pkg/log"

statuscake "github.com/StatusCakeDev/statuscake-go"
Expand All @@ -23,6 +25,7 @@ import (
)

var log = logf.Log.WithName("statuscake-monitor")
var rateLimiter = rate.NewLimiter(5, 1) // Allow 5 requests per second

// StatusCakeMonitorService is the service structure for StatusCake
type StatusCakeMonitorService struct {
Expand Down Expand Up @@ -288,7 +291,7 @@ func (service *StatusCakeMonitorService) GetByID(id string) (*models.Monitor, er
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", service.apiKey))

resp, err := service.client.Do(req)
resp, err := service.doRequest(req)
if err != nil {
log.Error(err, "Unable to retrieve monitor")
return nil, err
Expand Down Expand Up @@ -317,6 +320,36 @@ func (service *StatusCakeMonitorService) GetByID(id string) (*models.Monitor, er
return nil, errors.New("GetByID Request failed")
}

// doRequest function to handle requests to StatusCake and handle ratelimits.
func (service *StatusCakeMonitorService) doRequest(req *http.Request) (*http.Response, error) {
// Wait for the rate limiter to allow a request
err := rateLimiter.Wait(req.Context())
if err != nil {
log.Error(err, "Rate limiter wait failed")
return nil, err
}

resp, err := service.doRequest(req)
if err != nil {
log.Error(err, "HTTP request failed")
return nil, err
}

// Handle rate-limiting responses (HTTP 429)
if resp.StatusCode == http.StatusTooManyRequests {
retryAfter := resp.Header.Get("Retry-After")
if retryAfter != "" {
seconds, err := strconv.Atoi(retryAfter)
if err == nil {
time.Sleep(time.Duration(seconds) * time.Second)
return service.doRequest(req) // Retry after the specified delay
}
}
}

return resp, nil
}

// GetAll function will fetch all monitors
func (service *StatusCakeMonitorService) GetAll() []models.Monitor {
var StatusCakeMonitorData []StatusCakeMonitorData
Expand Down Expand Up @@ -355,7 +388,7 @@ func (service *StatusCakeMonitorService) fetchMonitors(page int) *StatusCakeMoni
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", service.apiKey))

resp, err := service.client.Do(req)
resp, err := service.doRequest(req)
if err != nil {
log.Error(err, "Unable to retrieve monitor")
return nil
Expand Down Expand Up @@ -396,7 +429,7 @@ func (service *StatusCakeMonitorService) Add(m models.Monitor) {
return
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", service.apiKey))
resp, err := service.client.Do(req)
resp, err := service.doRequest(req)
if err != nil {
log.Error(err, "Unable to make HTTP call")
return
Expand Down Expand Up @@ -430,7 +463,7 @@ func (service *StatusCakeMonitorService) Update(m models.Monitor) {
return
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", service.apiKey))
resp, err := service.client.Do(req)
resp, err := service.doRequest(req)
if err != nil {
log.Error(err, "Unable to make HTTP call")
return
Expand Down Expand Up @@ -464,7 +497,7 @@ func (service *StatusCakeMonitorService) Remove(m models.Monitor) {
return
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", service.apiKey))
resp, err := service.client.Do(req)
resp, err := service.doRequest(req)
if err != nil {
log.Error(err, "Unable to make HTTP call")
return
Expand Down
Loading