feat(protocol): implement O(1) event routing with Strategy Pattern#63
Merged
feat(protocol): implement O(1) event routing with Strategy Pattern#63
Conversation
e3558d2 to
ecb15ac
Compare
- Refactor protocol into subpackage; add routing strategies (Channel, Global, Composite) - Channel.from_session: register CHANNEL_STATE via register_channel_handler for O(1) - Session._awaitable_complete_command: register CHANNEL_EXECUTE_COMPLETE and CHANNEL_HANGUP_COMPLETE per channel when session has uuid - test_outbound_blocking_command: broadcast with session.uuid so event is routed O(1) - Centralize EventHandler in genesis/types.py; add routing metrics and docs Co-authored-by: Cursor <cursoragent@cursor.com>
ecb15ac to
bc8cf89
Compare
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.
Overview
This PR implements O(1) event routing using the Strategy Pattern, directly addressing the scalability concerns raised in #59.
Scalability Impact
Problem Addressed
As discussed in #59, the previous implementation used an Observer Pattern where every channel listens to the global event stream, resulting in O(N) overhead:
if event.uuid != self.uuid, and sleep againSolution
Replaced O(N) broadcasting with O(1) hash-based routing:
Performance: 100 parallel calls now trigger exactly 100 dispatch actions instead of 10,000 checks.
Implementation Details
Architecture
Refactored
genesis/protocolinto a structured subpackage:Strategy Pattern
Implemented the Strategy Pattern for flexible routing:
BaseRouter: Abstract interface for all routing strategiesGlobalRouter: Handles global event subscriptionsChannelRouter: Manages channel-specific handlersCompositeRouter: Orchestrates both strategiesPerformance Characteristics
Where n = number of registered handlers
Related