mirror of
https://github.com/containerd/containerd.git
synced 2026-11-06 13:27:16 +00:00
Add server-streaming variants of CRI list RPCs (StreamContainers, StreamPodSandboxes, StreamContainerStats, StreamPodSandboxStats, StreamPodSandboxMetrics, StreamImages) that send results in batches of 5000 items. Signed-off-by: Ayato Tokubi <atokubi@redhat.com>
38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
/*
|
|
Copyright The containerd Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package util
|
|
|
|
import "context"
|
|
|
|
const DefaultStreamBatchSize = 5000
|
|
|
|
// SendInBatches sends items in batches using the provided send function.
|
|
// It checks for context cancellation before sending each batch.
|
|
// Returns nil immediately if items is empty (gRPC closes the stream with EOF).
|
|
func SendInBatches[T any](ctx context.Context, items []*T, batchSize int, sendFn func([]*T) error) error {
|
|
for i := 0; i < len(items); i += batchSize {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
end := min(i+batchSize, len(items))
|
|
if err := sendFn(items[i:end]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|