mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 21:47:20 +00:00
#24804 # Checklist for submitter - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files) for more information. - [x] Added/updated tests - [x] If database migrations are included, checked table schema to confirm autoupdate - [x] Manual QA for all new/changed functionality
26 lines
640 B
Go
26 lines
640 B
Go
package common_mysql
|
|
|
|
// BatchProcessSimple is a simple utility function to batch process a slice of payloads.
|
|
// Provide a slice of payloads, a batch size, and a function to execute on each batch.
|
|
func BatchProcessSimple[T any](
|
|
payloads []T,
|
|
batchSize int,
|
|
executeBatch func(payloadsInThisBatch []T) error,
|
|
) error {
|
|
if len(payloads) == 0 || batchSize <= 0 || executeBatch == nil {
|
|
return nil
|
|
}
|
|
|
|
for i := 0; i < len(payloads); i += batchSize {
|
|
start := i
|
|
end := i + batchSize
|
|
if end > len(payloads) {
|
|
end = len(payloads)
|
|
}
|
|
if err := executeBatch(payloads[start:end]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|