Skip to content

Commit ef4e50a

Browse files
committed
slicetools.Chunks() -> slices.Chunk()
1 parent 95a8b47 commit ef4e50a

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

pkg/csplugin/broker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package csplugin
22

33
import (
4+
"cmp"
45
"context"
56
"errors"
67
"fmt"
@@ -22,7 +23,6 @@ import (
2223

2324
"github.com/crowdsecurity/go-cs-lib/csstring"
2425
"github.com/crowdsecurity/go-cs-lib/ptr"
25-
"github.com/crowdsecurity/go-cs-lib/slicetools"
2626

2727
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
2828
"github.com/crowdsecurity/crowdsec/pkg/models"
@@ -153,7 +153,7 @@ func (pb *PluginBroker) Run(pluginTomb *tomb.Tomb) {
153153
threshold = 1
154154
}
155155

156-
for _, chunk := range slicetools.Chunks(tmpAlerts, threshold) {
156+
for chunk := range slices.Chunk(tmpAlerts, max(1, cmp.Or(threshold, len(tmpAlerts)))) {
157157
if err := pb.pushNotificationsToPlugin(ctx, pluginName, chunk); err != nil {
158158
log.WithField("plugin:", pluginName).Error(err)
159159
}

pkg/database/alerts.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package database
22

33
import (
4+
"cmp"
45
"context"
56
"encoding/json"
67
"fmt"
8+
"slices"
79
"sort"
810
"strconv"
911
"strings"
@@ -14,7 +16,6 @@ import (
1416
log "github.com/sirupsen/logrus"
1517

1618
"github.com/crowdsecurity/go-cs-lib/cstime"
17-
"github.com/crowdsecurity/go-cs-lib/slicetools"
1819

1920
"github.com/crowdsecurity/crowdsec/pkg/csnet"
2021
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
@@ -166,9 +167,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
166167

167168
decisions := []*ent.Decision{}
168169

169-
builderChunks := slicetools.Chunks(decisionBuilders, c.decisionBulkSize)
170-
171-
for _, builderChunk := range builderChunks {
170+
for builderChunk := range slices.Chunk(decisionBuilders, max(1, cmp.Or(c.decisionBulkSize, len(decisionBuilders)))) {
172171
decisionsCreateRet, err := c.Ent.Decision.CreateBulk(builderChunk...).Save(ctx)
173172
if err != nil {
174173
return "", fmt.Errorf("creating alert decisions: %w", err)
@@ -179,9 +178,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
179178

180179
// now that we bulk created missing decisions, let's update the alert
181180

182-
decisionChunks := slicetools.Chunks(decisions, c.decisionBulkSize)
183-
184-
for _, decisionChunk := range decisionChunks {
181+
for decisionChunk := range slices.Chunk(decisions, max(1, cmp.Or(c.decisionBulkSize, len(decisions)))) {
185182
err = c.Ent.Alert.Update().Where(alert.UUID(alertItem.UUID)).AddDecisions(decisionChunk...).Exec(ctx)
186183
if err != nil {
187184
return "", fmt.Errorf("updating alert %s: %w", alertItem.UUID, err)
@@ -329,9 +326,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
329326
valueList = append(valueList, *decisionItem.Value)
330327
}
331328

332-
deleteChunks := slicetools.Chunks(valueList, c.decisionBulkSize)
333-
334-
for _, deleteChunk := range deleteChunks {
329+
for deleteChunk := range slices.Chunk(valueList, max(1, cmp.Or(c.decisionBulkSize, len(valueList)))) {
335330
// Deleting older decisions from capi
336331
deletedDecisions, err := txClient.Decision.Delete().
337332
Where(decision.And(
@@ -346,9 +341,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
346341
deleted += deletedDecisions
347342
}
348343

349-
builderChunks := slicetools.Chunks(decisionBuilders, c.decisionBulkSize)
350-
351-
for _, builderChunk := range builderChunks {
344+
for builderChunk := range slices.Chunk(decisionBuilders, max(1, cmp.Or(c.decisionBulkSize, len(decisionBuilders)))) {
352345
insertedDecisions, err := txClient.Decision.CreateBulk(builderChunk...).Save(ctx)
353346
if err != nil {
354347
return 0, 0, 0, rollbackOnError(txClient, err, "bulk creating decisions")
@@ -545,8 +538,7 @@ func buildMetaCreates(ctx context.Context, logger log.FieldLogger, client *ent.C
545538
func buildDecisions(ctx context.Context, logger log.FieldLogger, client *Client, alertItem *models.Alert, stopAtTime time.Time) ([]*ent.Decision, int, error) {
546539
decisions := []*ent.Decision{}
547540

548-
decisionChunks := slicetools.Chunks(alertItem.Decisions, client.decisionBulkSize)
549-
for _, decisionChunk := range decisionChunks {
541+
for decisionChunk := range slices.Chunk(alertItem.Decisions, max(1, cmp.Or(client.decisionBulkSize, len(alertItem.Decisions)))) {
550542
decisionRet, err := client.createDecisionChunk(ctx, *alertItem.Simulated, stopAtTime, decisionChunk)
551543
if err != nil {
552544
return nil, 0, fmt.Errorf("creating alert decisions: %w", err)
@@ -601,9 +593,8 @@ func saveAlerts(ctx context.Context, c *Client, alertBuilders []*ent.AlertCreate
601593
ret[i] = strconv.Itoa(a.ID)
602594

603595
d := alertDecisions[i]
604-
decisionsChunk := slicetools.Chunks(d, c.decisionBulkSize)
605596

606-
for _, d2 := range decisionsChunk {
597+
for d2 := range slices.Chunk(d, max(1, cmp.Or(c.decisionBulkSize, len(d)))) {
607598
if err := retryOnBusy(func() error {
608599
_, err := c.Ent.Alert.Update().Where(alert.IDEQ(a.ID)).AddDecisions(d2...).Save(ctx)
609600
return err
@@ -720,10 +711,9 @@ func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []
720711

721712
c.Log.Debugf("writing %d items", len(alertList))
722713

723-
alertChunks := slicetools.Chunks(alertList, alertCreateBulkSize)
724714
alertIDs := []string{}
725715

726-
for _, alertChunk := range alertChunks {
716+
for alertChunk := range slices.Chunk(alertList, max(1, cmp.Or(alertCreateBulkSize, len(alertList)))) {
727717
ids, err := c.createAlertChunk(ctx, machineID, owner, alertChunk)
728718
if err != nil {
729719
return nil, fmt.Errorf("machine '%s': %w", machineID, err)

pkg/database/decisions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package database
22

33
import (
4+
"cmp"
45
"context"
56
"fmt"
7+
"slices"
68
"strconv"
79
"strings"
810
"time"
911

1012
"entgo.io/ent/dialect/sql"
1113
"github.com/pkg/errors"
1214

13-
"github.com/crowdsecurity/go-cs-lib/slicetools"
14-
1515
"github.com/crowdsecurity/crowdsec/pkg/csnet"
1616
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
1717
"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
@@ -414,7 +414,7 @@ func (c *Client) ExpireDecisions(ctx context.Context, decisions []*ent.Decision)
414414

415415
total := 0
416416

417-
for _, chunk := range slicetools.Chunks(decisions, decisionDeleteBulkSize) {
417+
for chunk := range slices.Chunk(decisions, max(1, cmp.Or(decisionDeleteBulkSize, len(decisions)))) {
418418
rows, err := c.ExpireDecisions(ctx, chunk)
419419
if err != nil {
420420
return total, err
@@ -446,7 +446,7 @@ func (c *Client) DeleteDecisions(ctx context.Context, decisions []*ent.Decision)
446446

447447
tot := 0
448448

449-
for _, chunk := range slicetools.Chunks(decisions, decisionDeleteBulkSize) {
449+
for chunk := range slices.Chunk(decisions, max(1, cmp.Or(decisionDeleteBulkSize, len(decisions)))) {
450450
rows, err := c.DeleteDecisions(ctx, chunk)
451451
if err != nil {
452452
return tot, err

0 commit comments

Comments
 (0)