Skip to content

Commit 2bb122b

Browse files
authored
refactor(main): add rate limit (#128)
Signed-off-by: cuisongliu <[email protected]>
1 parent 33416b6 commit 2bb122b

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

library/controller/rate_limiter.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,45 @@ package controller
1616

1717
import (
1818
"flag"
19+
"golang.org/x/time/rate"
1920
"time"
2021

2122
"k8s.io/client-go/util/workqueue"
2223
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
2324
)
2425

2526
const (
26-
defaultMinRetryDelay = 750 * time.Millisecond
27-
defaultMaxRetryDelay = 15 * time.Minute
27+
defaultMinRetryDelay = 5 * time.Millisecond
28+
defaultMaxRetryDelay = 1000 * time.Second
29+
defaultQPS = float64(10.0)
30+
defaultBurst = 100
2831
flagMinRetryDelay = "min-retry-delay"
2932
flagMaxRetryDelay = "max-retry-delay"
33+
flagQPS = "default-qps"
34+
flagBurst = "default-burst"
3035
)
3136

3237
// RateLimiterOptions used on reconcilers.
3338
type RateLimiterOptions struct {
3439
MinRetryDelay time.Duration
35-
40+
QPS float64
41+
Burst int
3642
MaxRetryDelay time.Duration
3743
}
3844

3945
func (o *RateLimiterOptions) BindFlags(fs *flag.FlagSet) {
4046
fs.DurationVar(&o.MinRetryDelay, flagMinRetryDelay, defaultMinRetryDelay,
41-
"The minimum amount of time for which an object being reconciled will have to wait before a retry.")
47+
"Specifies the minimum delay time before retrying the reconciliation of an object. This delay provides a buffer to prevent rapid-fire retries.")
4248
fs.DurationVar(&o.MaxRetryDelay, flagMaxRetryDelay, defaultMaxRetryDelay,
43-
"The maximum amount of time for which an object being reconciled will have to wait before a retry.")
49+
"Specifies the maximum delay time before retrying the reconciliation of an object. This cap ensures that retry delays don't grow excessively long.")
50+
fs.Float64Var(&o.QPS, flagQPS, defaultQPS, "Sets the maximum allowed quantity of process units (batches) that can be processed per second. This limit helps maintain a controlled processing rate.")
51+
fs.IntVar(&o.Burst, flagBurst, defaultBurst, "Sets the maximum quantity of process units (batches) that can be processed in a burst. This limit helps control the processing rate during short periods of high activity.")
4452
}
4553

4654
func GetRateLimiter(opts RateLimiterOptions) ratelimiter.RateLimiter {
47-
return workqueue.NewItemExponentialFailureRateLimiter(
48-
opts.MinRetryDelay,
49-
opts.MaxRetryDelay)
50-
}
51-
52-
// GetDefaultRateLimiter
53-
// rate-limiter.RateLimiter with the default configuration.
54-
func GetDefaultRateLimiter() ratelimiter.RateLimiter {
55-
return workqueue.NewItemExponentialFailureRateLimiter(
56-
defaultMinRetryDelay,
57-
defaultMaxRetryDelay)
55+
return workqueue.NewMaxOfRateLimiter(
56+
workqueue.NewItemExponentialFailureRateLimiter(opts.MinRetryDelay, opts.MaxRetryDelay),
57+
// 10 qps, 100 bucket size. This is only for retry speed and its only the overall factor (not per item)
58+
&workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(opts.QPS), opts.Burst)},
59+
)
5860
}

0 commit comments

Comments
 (0)