Skip to content

Commit

Permalink
Fix lazy acker.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakerouse committed Sep 16, 2021
1 parent cbbe8c2 commit 2316537
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func NewAcker(baseAcker batchAcker, log *logger.Logger) *Acker {

// Ack acknowledges action.
func (f *Acker) Ack(ctx context.Context, action fleetapi.Action) error {
f.queue = append(f.queue, action)
f.log.Debugf("appending action with id '%s' to the queue", action.ID())
f.enqueue(action)

if _, isAckForced := action.(ackForcer); isAckForced {
return f.Commit(ctx)
Expand All @@ -58,3 +57,14 @@ func (f *Acker) Commit(ctx context.Context) error {
f.queue = make([]fleetapi.Action, 0)
return nil
}

func (f *Acker) enqueue(action fleetapi.Action) {
for _, a := range f.queue {
if a.ID() == action.ID() {
f.log.Debugf("action with id '%s' has already been queued", action.ID())
return
}
}
f.queue = append(f.queue, action)
f.log.Debugf("appending action with id '%s' to the queue", action.ID())
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,21 @@ func TestLazyAcker(t *testing.T) {
}()
c := context.Background()

if err := lacker.Ack(c, testAction1); err != nil {
t.Fatal(err)
}
if err := lacker.Ack(c, testAction1); err != nil {
t.Fatal(err)
}
if err := lacker.Ack(c, testAction2); err != nil {
t.Fatal(err)
}
if err := lacker.Ack(c, testAction2); err != nil {
t.Fatal(err)
}
if err := lacker.Ack(c, testAction3); err != nil {
t.Fatal(err)
}
if err := lacker.Ack(c, testAction3); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 2316537

Please sign in to comment.