Skip to content

Commit

Permalink
set default limits in defaults.go and use them in controller
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed Sep 23, 2024
1 parent 143e56f commit c256d9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
13 changes: 7 additions & 6 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func (r *LinodeMachineReconciler) apiResponseRatelimitCounter(resp *resty.Respon
postReqCtr, exists := tokenMap[r.LinodeClientConfig.Token]
if !exists {
postReqCtr = &PostRequestCounter{
reqRemaining: 10,
reqRemaining: reconciler.DefaultPOSTRequestLimit,
refreshTime: 0,
}
tokenMap[r.LinodeClientConfig.Token] = postReqCtr
Expand All @@ -554,23 +554,24 @@ func isPOSTLimitReached(token string, logger logr.Logger) bool {
postReqCtr, exists := tokenMap[token]
if !exists {
postReqCtr = &PostRequestCounter{
reqRemaining: 10,
reqRemaining: reconciler.DefaultPOSTRequestLimit,
refreshTime: 0,
}
tokenMap[token] = postReqCtr
}

logger.Info(fmt.Sprintf("Requests Remaining: %v, Refresh Time: %v, currentTime: %v", postReqCtr.reqRemaining, postReqCtr.refreshTime, time.Now().Unix()))
if postReqCtr.reqRemaining == 5 || postReqCtr.reqRemaining == 0 {
if postReqCtr.reqRemaining == reconciler.SecondaryPOSTRequestLimit || postReqCtr.reqRemaining == 0 {
actualRefreshTime := postReqCtr.refreshTime
if postReqCtr.reqRemaining == 5 {
actualRefreshTime = postReqCtr.refreshTime - 15
if postReqCtr.reqRemaining == reconciler.SecondaryPOSTRequestLimit {
actualRefreshTime = postReqCtr.refreshTime - int(reconciler.SecondaryLinodeTooManyPOSTRequestsErrorRetryDelay.Seconds())
}
if time.Now().Unix() <= int64(actualRefreshTime) {
logger.Info("Cannot make more requests as max requests have been made. Waiting and retrying ...")
return true
} else if postReqCtr.reqRemaining == 0 {
postReqCtr.reqRemaining = 10
// reset limits, set max allowed POST requests to default max
postReqCtr.reqRemaining = reconciler.DefaultPOSTRequestLimit
}
}
return false
Expand Down
4 changes: 1 addition & 3 deletions controller/linodemachine_controller_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"net/http"
"slices"
"sort"
"time"

"github.com/go-logr/logr"
"github.com/google/uuid"
Expand Down Expand Up @@ -68,8 +67,7 @@ func handleTooManyRequestsError(err error) (ctrl.Result, error) {
if newErr.Response.Request.Method != "POST" {
return ctrl.Result{RequeueAfter: reconciler.DefaultLinodeTooManyRequestsErrorRetryDelay}, nil
}
retrySeconds := 30
return ctrl.Result{RequeueAfter: time.Duration(retrySeconds) * time.Second}, nil
return ctrl.Result{RequeueAfter: reconciler.DefaultLinodeTooManyPOSTRequestsErrorRetryDelay}, nil
}

func retryIfTransient(err error) (ctrl.Result, error) {
Expand Down
9 changes: 9 additions & 0 deletions util/reconciler/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ const (

// DefaultDNSTTLSec is the default TTL used for DNS entries for api server loadbalancing
DefaultDNSTTLSec = 30

// DefaultLinodeTooManyPOSTRequestsErrorRetryDelay is the default requeue delay if there is Linode API error for POST request. Currently, it is set to 10 requests per 30 seconds
DefaultLinodeTooManyPOSTRequestsErrorRetryDelay = 30 * time.Second
// SecondaryLinodeTooManyPOSTRequestsErrorRetryDelay is the secondary requeue delay if there is Linode API error for POST request. Currently, it is set to 5 requests per 15 seconds
SecondaryLinodeTooManyPOSTRequestsErrorRetryDelay = 15 * time.Second
// DefaultPOSTRequestLimit is the default limit of how many POST requests can be made to /linode/instances endpoint in 30 seconds before rate-limit reset.
DefaultPOSTRequestLimit = 10
// SecondaryPOSTRequestLimit is the secondary limit of how many POST requests can be made to /linode/instances endpoint in 15 seconds before rate-limit kicks in.
SecondaryPOSTRequestLimit = 5
)

// DefaultedLoopTimeout will default the timeout if it is zero-valued.
Expand Down

0 comments on commit c256d9c

Please sign in to comment.