Skip to content

Commit e542890

Browse files
authored
Fix infinite loop in decisions list for some values of limit (#3695)
1 parent 27d0992 commit e542890

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

pkg/database/alerts.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ func (c *Client) createAlertChunk(ctx context.Context, machineID string, owner *
658658
if discardCount > 0 && len(decisions) == 0 {
659659
c.Log.Warningf("dropping alert %s: all decisions invalid", alertItem.UUID)
660660
continue
661-
}
661+
}
662662

663663
alertBuilder := c.Ent.Alert.
664664
Create().
@@ -845,6 +845,13 @@ func (c *Client) QueryAlertWithFilter(ctx context.Context, filter map[string][]s
845845
return nil, errors.Wrapf(QueryFail, "pagination size: %d, offset: %d: %s", paginationSize, offset, err)
846846
}
847847

848+
if len(result) == 0 { // no results, no need to try to paginate further
849+
c.Log.Debugf("Pagination done because no results found at offset %d | len(ret) %d", offset, len(ret))
850+
break
851+
}
852+
log.Debugf("QueryAlertWithFilter: pagination size %d, offset %d, got %d results", paginationSize, offset, len(result))
853+
log.Debugf("diff is %d, limit is %d", limit-len(ret), limit)
854+
848855
if diff := limit - len(ret); diff < paginationSize {
849856
if len(result) < diff {
850857
ret = append(ret, result...)
@@ -858,7 +865,7 @@ func (c *Client) QueryAlertWithFilter(ctx context.Context, filter map[string][]s
858865
ret = append(ret, result...)
859866
}
860867

861-
if len(ret) == limit || len(ret) == 0 || len(ret) < paginationSize {
868+
if len(ret) == limit || len(result) < paginationSize {
862869
c.Log.Debugf("Pagination done len(ret) = %d", len(ret))
863870
break
864871
}

0 commit comments

Comments
 (0)