11#!/usr/bin/env python
22# Copyright 2026 NetBox Labs Inc
3- """Message chunking utilities for Diode SDK.
3+ """
4+ Message chunking utilities for Diode SDK.
45
56This module provides utilities for chunking large lists of entities into
67size-appropriate chunks for gRPC ingestion, ensuring no chunk exceeds
78the gRPC message size limit.
89"""
910
10- from typing import Iterable
11+ from collections . abc import Iterable
1112
1213from .diode .v1 import ingester_pb2
1314
1415
1516def create_message_chunks (
1617 entities : Iterable [ingester_pb2 .Entity ], max_chunk_size_mb : float = 3.0
1718) -> list [list [ingester_pb2 .Entity ]]:
18- """Create size-aware chunks from entities using greedy bin-packing.
19+ """
20+ Create size-aware chunks from entities using greedy bin-packing.
1921
2022 This function chunks entities to ensure each chunk stays under the specified
2123 size limit. It uses a greedy bin-packing algorithm that accumulates entities
@@ -40,6 +42,7 @@ def create_message_chunks(
4042
4143 >>> # Use a custom chunk size
4244 >>> chunks = create_message_chunks(entities, max_chunk_size_mb=3.5)
45+
4346 """
4447 # Convert iterable to list if necessary for size estimation
4548 if not isinstance (entities , list ):
@@ -85,7 +88,8 @@ def create_message_chunks(
8588
8689
8790def estimate_message_size (entities : Iterable [ingester_pb2 .Entity ]) -> int :
88- """Estimate the serialized size of entities in bytes.
91+ """
92+ Estimate the serialized size of entities in bytes.
8993
9094 Calculates the total size by summing individual entity sizes plus the
9195 IngestRequest protobuf overhead.
@@ -101,6 +105,7 @@ def estimate_message_size(entities: Iterable[ingester_pb2.Entity]) -> int:
101105 >>> size_bytes = estimate_message_size(entities)
102106 >>> size_mb = size_bytes / (1024 * 1024)
103107 >>> print(f"Estimated size: {size_mb:.2f} MB")
108+
104109 """
105110 # Convert iterable to list if necessary
106111 if not isinstance (entities , list ):
0 commit comments