Skip to content

Commit 3ba2891

Browse files
committed
Prefactor: Every supports rate < 1/s
1 parent cacd1b4 commit 3ba2891

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

webhosting-operator/pkg/experiment/generator/reconciler.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,33 @@ func (r *Every) AddToManager(mgr manager.Manager) error {
5454
r.Client = mgr.GetClient()
5555
}
5656

57+
initialDelay := time.Duration(0)
5758
workers := defaultReconcileWorkers
5859
if r.Workers > 0 {
5960
workers = r.Workers
6061
}
6162

63+
var rateLimiter workqueue.TypedRateLimiter[reconcile.Request] = &workqueue.TypedBucketRateLimiter[reconcile.Request]{
64+
Limiter: rate.NewLimiter(r.Rate, int(r.Rate)),
65+
}
66+
if r.Rate < 1 {
67+
// Special case for controllers running less frequent than every second:
68+
// The token bucket rate limiter would not allow any events as burst is less than 1, so replace it with a custom
69+
// rate limiter that always returns a constant delay.
70+
// Also, delay the first request when starting the scenario.
71+
every := time.Duration(1 / float64(r.Rate) * float64(time.Second))
72+
rateLimiter = constantDelayRateLimiter(every)
73+
initialDelay = every
74+
workers = 1
75+
}
76+
6277
return builder.ControllerManagedBy(mgr).
6378
Named(r.Name).
6479
WithOptions(controller.Options{
6580
MaxConcurrentReconciles: workers,
66-
RateLimiter: &workqueue.TypedBucketRateLimiter[reconcile.Request]{Limiter: rate.NewLimiter(r.Rate, int(r.Rate))},
81+
RateLimiter: rateLimiter,
6782
}).
68-
WatchesRawSource(EmitN(workers, 0)).
83+
WatchesRawSource(EmitN(workers, initialDelay)).
6984
Complete(StopOnContextCanceled(r))
7085
}
7186

webhosting-operator/pkg/experiment/generator/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,12 @@ func CreateClusterScopedOwnerObject(ctx context.Context, c client.Client, opts .
166166

167167
return ownerObject, metav1.NewControllerRef(ownerObject, rbacv1.SchemeGroupVersion.WithKind("ClusterRole")), nil
168168
}
169+
170+
var _ workqueue.TypedRateLimiter[reconcile.Request] = constantDelayRateLimiter(0)
171+
172+
// constantDelayRateLimiter delays all requests with a constant duration.
173+
type constantDelayRateLimiter time.Duration
174+
175+
func (d constantDelayRateLimiter) When(reconcile.Request) time.Duration { return time.Duration(d) }
176+
func (d constantDelayRateLimiter) Forget(reconcile.Request) {}
177+
func (d constantDelayRateLimiter) NumRequeues(reconcile.Request) int { return 0 }

0 commit comments

Comments
 (0)