1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ratelimiter 16 17 import ( 18 "time" 19 20 "golang.org/x/time/rate" 21 "k8s.io/client-go/util/workqueue" 22 "sigs.k8s.io/controller-runtime/pkg/ratelimiter" 23 ) 24 25 func NewRateLimiter() ratelimiter.RateLimiter { 26 // This is based on workqueue.DefaultControllerRateLimiter, but with different parameters better suited to KRM reconciliation. 27 // Context is in b/188203307 28 29 // We have both overall and per-item rate limiting. 30 // The overall is a token bucket and the per-item is exponential 31 // The per-item rate limiter initially retries much more slowly (2 seconds vs 2 milliseconds), 32 // and a much faster ultimate limit (120 seconds instead of 1000 seconds). 33 34 // If we implement b/190097904 we should revisit these values, in particular the max delay could 35 // likely be much higher again. 36 37 return workqueue.NewMaxOfRateLimiter( 38 workqueue.NewItemExponentialFailureRateLimiter(2*time.Second, 120*time.Second), 39 // 10 qps, 100 bucket size. This is only for retry speed and its only the overall factor (not per item) 40 &workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(10), 100)}, 41 ) 42 } 43 44 func RequeueRateLimiter() ratelimiter.RateLimiter { 45 return workqueue.NewMaxOfRateLimiter( 46 // 5 qps, 50 bucket size. This is the overall factor, and must be slower than the NewRateLimiter limit, to leave "room" for new items. 47 &workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(5), 50)}, 48 ) 49 } 50