feat: python sdk batch operations & smart batching
Merged
VI
7mo ago+1218+1218+0123456789012345678901234567890123456789-57-57-01234567890123456789
Summary
- Add
batch_enqueue()method for explicit multi-message RPCs on both sync and async clients - Add smart batching via
BatchModeenum (AUTO/DISABLED) andLingerdataclass, routingenqueue()through a background batcher thread by default - Add delivery batching: consume stream transparently unpacks
ConsumeResponse.messagesrepeated field with backward-compatible fallback to singularmessagefield - Single-item optimization: when flushing 1 message, use singular
EnqueueRPC to preserve error types (QueueNotFoundErrorvsBatchEnqueueError) close()drains pending batched messages before disconnecting- Update proto to include
BatchEnqueueRPC andConsumeResponse.messagesrepeated field - 16 new unit tests (batcher mechanics, flush functions, batch mode types) + 9 integration tests (batch enqueue, smart batching modes)
Test plan
- Unit tests for
_flush_singleand_flush_batchwith mock stubs - Unit tests for
AutoBatcher(single message, concurrent batching, close drains, stub update) - Unit tests for
LingerBatcher(batch size trigger, linger timeout, close drains) - Unit tests for
BatchMode,Linger,BatchEnqueueResulttypes - Integration tests for explicit
batch_enqueue()(multiple messages, single message, consume verification) - Integration tests for async
batch_enqueue() - Integration tests for smart batching modes (AUTO, DISABLED, LINGER, default-is-AUTO)
- Existing integration tests still pass (enqueue/consume/ack lifecycle, nack redelivery, API key auth)
- ruff lint clean, mypy clean (no new errors)
Generated with Claude Code
Summary by cubic
Add explicit batch_enqueue() to the Python SDK and enable smart batching by default (AUTO) for enqueue(). Also add batched delivery in consume() and update the proto with BatchEnqueue and ConsumeResponse.messages.
-
New Features
Client.batch_enqueue()andAsyncClient.batch_enqueue()send many messages in one RPC; return per-messageBatchEnqueueResult.- Smart batching via
BatchMode(AUTOdefault,DISABLED) andLinger; background batcher with single-item optimization;close()drains pending; reconnect updates batcher stub. - Delivery batching:
consume()transparently iteratesConsumeResponse.messageswith fallback to singularmessage. - Proto changes: add
BatchEnqueueRPC andConsumeResponse.messages; newBatchEnqueueError; public exports updated.
-
Migration
enqueue()now routes through a background batcher by default; disable withbatch_mode=BatchMode.DISABLED, or useLinger(linger_ms, batch_size)for timer-based batching.- Call
close()to flush any pending batched messages before shutdown. - No changes needed for consumers; batched deliveries are unpacked automatically.