Skip to content

Commit

Permalink
enhance: add enqueue key with delay
Browse files Browse the repository at this point in the history
If a user uses resp.RetryAfter in a handler, the object's key is enqueued
without the trigger designation. That means, that all triggers for the object
will be invoked regardless of whether the object changes when it goes through
the handlers. This is not the desired behavior. If the object doesn't change,
then the triggers should not be invoked.

This change adds an EnqueueKeyWithDelay function that achieves this desired
behavior.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams authored Feb 27, 2025
1 parent 3825a76 commit c8ac47e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pkg/router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (m *HandlerSet) handle(gvk schema.GroupVersionKind, key string, unmodifiedO
if unmodifiedObject == nil {
// A nil object here means that the object was deleted, so unregister the triggers
m.triggers.UnregisterAndTrigger(req)
} else {
} else if !req.FromTrigger {
m.triggers.Trigger(req)
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/router/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ func (m *triggers) shouldAddTrigger(gvk schema.GroupVersionKind, key string, tar
}

func (m *triggers) Trigger(req Request) {
if !req.FromTrigger {
m.invokeTriggers(req)
}
m.invokeTriggers(req)
}

func (m *triggers) Register(sourceGVK schema.GroupVersionKind, key string, obj runtime.Object, namespace, name string, selector labels.Selector, fields fields.Selector) (schema.GroupVersionKind, bool, error) {
Expand Down
13 changes: 2 additions & 11 deletions pkg/runtime/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -87,16 +86,8 @@ func (b *Backend) Trigger(ctx context.Context, gvk schema.GroupVersionKind, key
if err != nil {
return err
}
if delay > 0 {
ns, name, ok := strings.Cut(key, "/")
if ok {
controller.EnqueueAfter(ns, name, delay)
} else {
controller.EnqueueAfter("", key, delay)
}
} else {
controller.EnqueueKey(router.TriggerPrefix + key)
}

controller.EnqueueKeyAfter(router.TriggerPrefix+key, delay)
return nil
}

Expand Down
22 changes: 9 additions & 13 deletions pkg/runtime/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Controller interface {
Enqueue(namespace, name string)
EnqueueAfter(namespace, name string, delay time.Duration)
EnqueueKey(key string)
EnqueueKeyAfter(key string, delay time.Duration)
Cache() (cache.Cache, error)
Start(ctx context.Context, workers int) error
}
Expand Down Expand Up @@ -206,7 +207,7 @@ func (c *controller) Start(ctx context.Context, workers int) error {
if c.registration == nil {
registration, err := c.informer.AddEventHandler(clientgocache.ResourceEventHandlerFuncs{
AddFunc: c.handleObject,
UpdateFunc: func(old, new interface{}) {
UpdateFunc: func(old, new any) {
c.handleObject(new)
},
DeleteFunc: c.handleObject,
Expand Down Expand Up @@ -332,27 +333,22 @@ func (c *controller) syncHandler(ctx context.Context, key string) error {
}

func (c *controller) EnqueueKey(key string) {
c.EnqueueKeyAfter(key, 0)
}

func (c *controller) EnqueueKeyAfter(key string, after time.Duration) {
c.startLock.Lock()
defer c.startLock.Unlock()

if c.workqueues == nil {
c.startKeys = append(c.startKeys, startKey{key: key})
c.startKeys = append(c.startKeys, startKey{key: key, after: after})
} else {
c.workqueues[c.splitter.Split(key)].Add(key)
c.workqueues[c.splitter.Split(key)].AddAfter(key, after)
}
}

func (c *controller) Enqueue(namespace, name string) {
key := keyFunc(namespace, name)

c.startLock.Lock()
defer c.startLock.Unlock()

if c.workqueues == nil {
c.startKeys = append(c.startKeys, startKey{key: key})
} else {
c.workqueues[c.splitter.Split(key)].AddRateLimited(key)
}
c.EnqueueAfter(namespace, name, 0)
}

func (c *controller) EnqueueAfter(namespace, name string, duration time.Duration) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/runtime/errorcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func (n *errorController) Enqueue(namespace, name string) {
func (n *errorController) EnqueueAfter(namespace, name string, delay time.Duration) {
}

func (n *errorController) EnqueueKeyAfter(key string, delay time.Duration) {
}

func (n *errorController) EnqueueKey(key string) {
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/runtime/sharedcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (s *sharedController) EnqueueAfter(namespace, name string, delay time.Durat
s.initController().EnqueueAfter(namespace, name, delay)
}

func (s *sharedController) EnqueueKeyAfter(key string, delay time.Duration) {
s.initController().EnqueueKeyAfter(key, delay)
}

func (s *sharedController) EnqueueKey(key string) {
s.initController().EnqueueKey(key)
}
Expand Down

0 comments on commit c8ac47e

Please sign in to comment.