Add MessageBufferingConfig to allow custom back-pressure config for message.new events#6406
Draft
VelikovPetar wants to merge 1 commit intov6from
Draft
Add MessageBufferingConfig to allow custom back-pressure config for message.new events#6406VelikovPetar wants to merge 1 commit intov6from
MessageBufferingConfig to allow custom back-pressure config for message.new events#6406VelikovPetar wants to merge 1 commit intov6from
Conversation
Contributor
PR checklist ✅All required conditions are satisfied:
🎉 Great job! This PR is ready for review. |
Contributor
SDK Size Comparison 📏
|
|
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.


Goal
High-traffic channel types (e.g. livestreams) can produce a flood of
message.newevents that arrive faster than the sequential event-handling pipeline can process them. The current implementation funnels every socket event through a singleMutableSharedFlowwithextraBufferCapacity = Int.MAX_VALUE, which means there is no back-pressure: a burst of new-message events queues up unbounded memory and starves more important signals (reads, bans, member updates) of timely processing.This PR introduces a
MessageBufferConfigthat lets integrators opt specific channel types into a bounded buffer forNewMessageEvents, with a configurable overflow strategy (SUSPEND/DROP_OLDEST/DROP_LATEST). Signal-critical events and events for non-opted-in channel types are unaffected.Implementation
MessageBufferConfig(underMessageLimitConfig.messageBufferConfig) exposing:channelTypes: Set<String>— channel types whoseNewMessageEvents go through the bounded buffer (empty by default → feature is a no-op).capacity: Int— buffer capacity (defaults toInt.MAX_VALUE).overflow: BufferOverflow— overflow strategy (defaults toSUSPEND).EventHandlerSequentialnow allocates a secondaryMutableSharedFlow(bufferedNewMessageEvents) lazily, only when buffering is enabled, so the default configuration pays no cost for it.defaultSocketEventListener— the existing unbuffered path; used when no channel types are opted in.bufferedSocketEventListener— routesNewMessageEvents for opted-in channel types to the bounded flow, and everything else (including non-opted-inNewMessageEvents and all other event types) to the unbuffered flow.startListening()picks the listener based onbufferConfig.channelTypes.isNotEmpty()and only collects frombufferedNewMessageEventswhen buffering is enabled.StreamStatePluginFactorywires the config fromStatePluginConfig.messageLimitConfig.messageBufferConfigintoEventHandlerSequential.The bounded flow shares the same downstream pipeline (
socketEventCollector→handleBatchEvent) as the unbuffered flow, so ordering inside each flow is preserved and back-pressure is applied independently per flow.Testing
EventHandlerSequentialTestcovering:channelTypesis empty.NewMessageEvents for opted-in channel types are routed through the bounded buffer.NewMessageEvents for non-opted-in channel types and all non-NewMessageEventevents keep using the unbuffered path.DROP_OLDEST/DROP_LATEST/SUSPENDoverflow strategies behave as expected when the buffer is full.TotalUnreadCountTest,EventHandlerSequentialUserMessagesDeletedTest) updated to pass the newbufferConfigargument.