-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
// DebounceReconciler is a Reconciler that debounces reconcile requests. | ||
type DebounceReconciler struct { | ||
client.Client | ||
// Minimum time to wait before processing requests. | ||
debounceDuration time.Duration | ||
// Last request time. | ||
lastRequest time.Time | ||
// Channel to trigger reconciliations. | ||
reconcileChan chan reconcile.Request | ||
// The actual reconciler that processes the requests. | ||
actualReconciler reconcile.Reconciler | ||
} | ||
|
||
// NewDebounceReconciler creates a new DebounceReconciler. | ||
func NewDebounceReconciler(client client.Client, duration time.Duration, actual reconcile.Reconciler) *DebounceReconciler { | ||
return &DebounceReconciler{ | ||
Client: client, | ||
debounceDuration: duration, | ||
reconcileChan: make(chan reconcile.Request, 1), | ||
actualReconciler: actual, | ||
} | ||
} | ||
|
||
// Reconcile implements the reconcile.Reconciler interface. | ||
func (r *DebounceReconciler) Reconcile(_ context.Context, req reconcile.Request) (reconcile.Result, error) { | ||
select { | ||
case r.reconcileChan <- req: | ||
default: | ||
// Channel is full, drop the request to avoid spamming. | ||
} | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// Start begins the debouncing mechanism. | ||
func (r *DebounceReconciler) Start(ctx context.Context) { | ||
go func() { | ||
ticker := time.NewTicker(r.debounceDuration) | ||
defer ticker.Stop() | ||
|
||
var latestRequest reconcile.Request | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case req := <-r.reconcileChan: | ||
latestRequest = req | ||
r.lastRequest = time.Now() | ||
case <-ticker.C: | ||
// Check if enough time has passed since the last request. | ||
if time.Since(r.lastRequest) >= r.debounceDuration { | ||
// Process the debounced request. | ||
if err := r.processRequest(ctx, latestRequest); err != nil { | ||
fmt.Printf("Error processing request: %v\n", err) | ||
} | ||
} | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// processRequest performs the actual reconciliation. | ||
func (r *DebounceReconciler) processRequest(ctx context.Context, req reconcile.Request) error { | ||
_, err := r.actualReconciler.Reconcile(ctx, req) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters