Skip to content

Commit 3c73be8

Browse files
committed
slicetools.Chunks() -> slices.Chunk()
1 parent 04ed469 commit 3c73be8

File tree

3 files changed

+12
-25
lines changed

3 files changed

+12
-25
lines changed

pkg/csplugin/broker.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/crowdsecurity/go-cs-lib/csstring"
2424
"github.com/crowdsecurity/go-cs-lib/ptr"
25-
"github.com/crowdsecurity/go-cs-lib/slicetools"
2625

2726
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
2827
"github.com/crowdsecurity/crowdsec/pkg/models"
@@ -125,7 +124,7 @@ func (pb *PluginBroker) Run(pluginTomb *tomb.Tomb) {
125124
threshold = 1
126125
}
127126

128-
for _, chunk := range slicetools.Chunks(tmpAlerts, threshold) {
127+
for chunk := range slices.Chunk(tmpAlerts, threshold) {
129128
if err := pb.pushNotificationsToPlugin(ctx, pluginName, chunk); err != nil {
130129
log.WithField("plugin:", pluginName).Error(err)
131130
}

pkg/database/alerts.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"slices"
78
"sort"
89
"strconv"
910
"strings"
@@ -14,7 +15,6 @@ import (
1415
log "github.com/sirupsen/logrus"
1516

1617
"github.com/crowdsecurity/go-cs-lib/cstime"
17-
"github.com/crowdsecurity/go-cs-lib/slicetools"
1818

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

167167
decisions := []*ent.Decision{}
168168

169-
builderChunks := slicetools.Chunks(decisionBuilders, c.decisionBulkSize)
170-
171-
for _, builderChunk := range builderChunks {
169+
for builderChunk := range slices.Chunk(decisionBuilders, c.decisionBulkSize) {
172170
decisionsCreateRet, err := c.Ent.Decision.CreateBulk(builderChunk...).Save(ctx)
173171
if err != nil {
174172
return "", fmt.Errorf("creating alert decisions: %w", err)
@@ -179,9 +177,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
179177

180178
// now that we bulk created missing decisions, let's update the alert
181179

182-
decisionChunks := slicetools.Chunks(decisions, c.decisionBulkSize)
183-
184-
for _, decisionChunk := range decisionChunks {
180+
for decisionChunk := range slices.Chunk(decisions, c.decisionBulkSize) {
185181
err = c.Ent.Alert.Update().Where(alert.UUID(alertItem.UUID)).AddDecisions(decisionChunk...).Exec(ctx)
186182
if err != nil {
187183
return "", fmt.Errorf("updating alert %s: %w", alertItem.UUID, err)
@@ -329,9 +325,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
329325
valueList = append(valueList, *decisionItem.Value)
330326
}
331327

332-
deleteChunks := slicetools.Chunks(valueList, c.decisionBulkSize)
333-
334-
for _, deleteChunk := range deleteChunks {
328+
for deleteChunk := range slices.Chunk(valueList, c.decisionBulkSize) {
335329
// Deleting older decisions from capi
336330
deletedDecisions, err := txClient.Decision.Delete().
337331
Where(decision.And(
@@ -346,9 +340,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
346340
deleted += deletedDecisions
347341
}
348342

349-
builderChunks := slicetools.Chunks(decisionBuilders, c.decisionBulkSize)
350-
351-
for _, builderChunk := range builderChunks {
343+
for builderChunk := range slices.Chunk(decisionBuilders, c.decisionBulkSize) {
352344
insertedDecisions, err := txClient.Decision.CreateBulk(builderChunk...).Save(ctx)
353345
if err != nil {
354346
return 0, 0, 0, rollbackOnError(txClient, err, "bulk creating decisions")
@@ -545,8 +537,7 @@ func buildMetaCreates(ctx context.Context, logger log.FieldLogger, client *ent.C
545537
func buildDecisions(ctx context.Context, logger log.FieldLogger, client *Client, alertItem *models.Alert, stopAtTime time.Time) ([]*ent.Decision, int, error) {
546538
decisions := []*ent.Decision{}
547539

548-
decisionChunks := slicetools.Chunks(alertItem.Decisions, client.decisionBulkSize)
549-
for _, decisionChunk := range decisionChunks {
540+
for decisionChunk := range slices.Chunk(alertItem.Decisions, client.decisionBulkSize) {
550541
decisionRet, err := client.createDecisionChunk(ctx, *alertItem.Simulated, stopAtTime, decisionChunk)
551542
if err != nil {
552543
return nil, 0, fmt.Errorf("creating alert decisions: %w", err)
@@ -601,9 +592,8 @@ func saveAlerts(ctx context.Context, c *Client, alertBuilders []*ent.AlertCreate
601592
ret[i] = strconv.Itoa(a.ID)
602593

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

606-
for _, d2 := range decisionsChunk {
596+
for d2 := range slices.Chunk(d, c.decisionBulkSize) {
607597
if err := retryOnBusy(func() error {
608598
_, err := c.Ent.Alert.Update().Where(alert.IDEQ(a.ID)).AddDecisions(d2...).Save(ctx)
609599
return err
@@ -720,10 +710,9 @@ func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []
720710

721711
c.Log.Debugf("writing %d items", len(alertList))
722712

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

726-
for _, alertChunk := range alertChunks {
715+
for alertChunk := range slices.Chunk(alertList, alertCreateBulkSize) {
727716
ids, err := c.createAlertChunk(ctx, machineID, owner, alertChunk)
728717
if err != nil {
729718
return nil, fmt.Errorf("machine '%s': %w", machineID, err)

pkg/database/decisions.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package database
33
import (
44
"context"
55
"fmt"
6+
"slices"
67
"strconv"
78
"strings"
89
"time"
910

1011
"entgo.io/ent/dialect/sql"
1112
"github.com/pkg/errors"
1213

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

415414
total := 0
416415

417-
for _, chunk := range slicetools.Chunks(decisions, decisionDeleteBulkSize) {
416+
for chunk := range slices.Chunk(decisions, decisionDeleteBulkSize) {
418417
rows, err := c.ExpireDecisions(ctx, chunk)
419418
if err != nil {
420419
return total, err
@@ -446,7 +445,7 @@ func (c *Client) DeleteDecisions(ctx context.Context, decisions []*ent.Decision)
446445

447446
tot := 0
448447

449-
for _, chunk := range slicetools.Chunks(decisions, decisionDeleteBulkSize) {
448+
for chunk := range slices.Chunk(decisions, decisionDeleteBulkSize) {
450449
rows, err := c.DeleteDecisions(ctx, chunk)
451450
if err != nil {
452451
return tot, err

0 commit comments

Comments
 (0)