Skip to content

Commit

Permalink
Don't UPSERT resources to be deleted in the next 30 seconds
Browse files Browse the repository at this point in the history
Don't process UPSERTs if the resource is about to be deleted in the next 30
seconds to  prevent races between simultaneous UPSERT and DELETE statements for
the same resource, where an UPSERT statement can occur after a DELETE statement
has already been executed.
  • Loading branch information
lippserd committed Sep 25, 2024
1 parent d9225fe commit 8b37d4d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/sync/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sync
import (
"context"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"
)

type Item struct {
Expand Down Expand Up @@ -55,6 +56,16 @@ func (s *Sink) ErrorCh() <-chan error {
}

func (s *Sink) Upsert(ctx context.Context, item *Item) error {
if item.Item != nil {
deletionTimestamp := (*item.Item).GetDeletionTimestamp()
if !deletionTimestamp.IsZero() && deletionTimestamp.Time.Compare(time.Now().Add(30*time.Second)) <= 0 {
// Don't process UPSERTs if the resource is about to be deleted in the next 30 seconds to
// prevent races between simultaneous UPSERT and DELETE statements for the same resource,
// where an UPSERT statement can occur after a DELETE statement has already been executed.
return ctx.Err()
}
}

select {
case s.upsert <- s.upsertFunc(item):
return nil
Expand Down

0 comments on commit 8b37d4d

Please sign in to comment.