feat(broker): Enables message keys for batch publishing #2586
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Allows specifying a message key when publishing a batch of messages, enabling partition control.
Introduces the option to set a default key for a batch publisher and override it per message if required.
Usage examples (post-update)
Below are concise, copy-ready examples showing how to publish Kafka batches with and without keys, including per-message keys and mixed cases. Behavior is identical for AIOKafka and Confluent on the producer side. See the last section for how to read keys on the consumer side.
1) No keys (legacy behavior)
key=None.2) Single default key on the publisher (or factory)
(If you set the default on a factory, the effect is the same.)
3) Per-message keys (each element wrapped)
{"message": <payload>, "key": <bytes|None>}.4) Mixed batch: some items have per-message keys, some don’t (no default key)
None.5) Mixed with both default key and per-message keys
6) Notes and edge cases
{"message": <payload>, "key": <bytes|None>}. Prefer always including"message"in wrapped items.{"key": None}behaves the same as no"key": it falls back to the default key if one exists; otherwiseNone.None), and the rest use the default key (if provided) orNone.Reading keys on the consumer side
msg.raw_message.key()keys = [m.key() for m in msg.raw_message]msg.raw_message.keykeys = [r.key for r in msg.raw_message]A small helper if you want backend-agnostic access in your handler/tests:
Priority rule (quick recap)
key, use it.key, use it.None.Fixes #2514
Type of change
Please delete options that are not relevant.
Checklist
just lintshows no errors)just test-coveragejust static-analysis