feat(batching): CoalesceBy de-duplication + IBatchContext (GH-3289 Phase 1)#3300
Merged
Merged
Conversation
Phase 1) Phase 1 of the #3289 batch-processing plan — the two "safe additions" that only add behavior and never change what gets acknowledged. Item 1 — CoalesceBy: - New CoalescingMessageBatcher<T,TKey> groups by tenant id (like the default batcher) then de-duplicates by a user key, last-wins for the message the handler sees. EVERY member envelope still rides on the batch, so settlement (inbox/outbox tracking + dead-lettering) is identical to a normal batch. - BatchingOptions.CoalesceBy<T,TKey>(Func<T,TKey>) installs it; validates the selector's element type matches the batched element type. Item 5 — IBatchContext: - Read-only batch identity (BatchId + Members: MessageId/Attempts/SentAt) injectable into any handler via a new BatchContextVariableSource, built from the active Envelope exactly like TenantId/now are supplied. BatchContext is public so the generated handler code can call its For(Envelope) factory. Tests: CoalescingMessageBatcher unit tests (last-wins, all-members-retained, tenant grouping, element-type guard) + end-to-end CoalesceBy and IBatchContext injection acceptance tests. Docs updated with both features. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jul 9, 2026
Merged
This was referenced Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Phase 1 of the #3289 batch-processing plan — the two safe additions that only add behavior and never change what gets acknowledged. (Phase 0 docs are #3299.)
Item 1 —
CoalesceBy(de-duplicate a batch by key)The "trigger storm" case: 1,000
RecalculateScoresevents about ~40 aggregates reach the handler as 1,000 items and each aggregate is recomputed ~25×.CoalesceBycollapses that to one message per key, last-wins:CoalescingMessageBatcher<T,TKey>: groups by tenant id (exactly likeDefaultMessageBatcher<T>), then de-duplicates within each tenant group.new Envelope(coalesced, allMembers)), so inbox/outbox tracking and dead-lettering behave identically to a normal batch. Dropping 1,000 messages → 40 distinct keys still settles all 1,000.BatchingOptions.CoalesceBy<T,TKey>(Func<T,TKey>)is sugar over the existingBatcherseam. It validates that the selector's element type matches the batched element type and throws a clear message otherwise. The lambda parameter must be explicitly typed ((RecalculateScores x) => ...) so both type args infer without restructuring the existingBatchMessagesOf<T>(Action<BatchingOptions>)API — no breaking change to existing call sites.Item 5 —
IBatchContext(read-only batch identity)BatchContextVariableSource, built from the activeEnvelopethe same wayTenantIdand the "now" clock are supplied (registered inWolverineOptionsalongside them).BatchContextispublic(private ctor, public staticFor(Envelope)factory) because the generated handler code has no access to Wolverine internals and must call the factory to read the internalEnvelope.Batch.CoalesceBy,Membersstill lists every original member.Tests
src/Testing/CoreTests/Acceptance/batch_coalescing_and_context.cs:CoalescingMessageBatcherunit tests: last-wins de-dup, all members retained on the batch, tenant grouping, element-type guard,CoalesceByinstalls the coalescing batcher.CoalesceBy(handler sees the deduped set) andIBatchContextinjection (BatchId non-empty, Members counts all published).All 6 new tests green; the existing 34 batch acceptance tests still pass.
dotnet build wolverine.slnx -c Releaseclean.Docs
docs/guide/handlers/batching.md: new "De-duplicating a batch withCoalesceBy" and "Batch identity withIBatchContext" sections.🤖 Generated with Claude Code