Change websocket cleanup performance from O(n^2) to O(n).#429
Open
marshallbrekka wants to merge 1 commit intoNetflix:masterfrom
Open
Change websocket cleanup performance from O(n^2) to O(n).#429marshallbrekka wants to merge 1 commit intoNetflix:masterfrom
marshallbrekka wants to merge 1 commit intoNetflix:masterfrom
Conversation
Move subscription tracking to a dict keyed by the underlying websocket object. Changes the cleanup from two nested for loops (the 2nd being the unsubscribe_from method), to a single loop with (the common case) deleting a single key from a dict.
savingoyal
approved these changes
Jun 17, 2024
Collaborator
|
lgtm! we can ship this once @saikonen approves |
Author
|
@savingoyal @saikonen is there any update here, anything I can do to help get this upstreamed? thanks! |
saikonen
reviewed
Aug 23, 2024
Collaborator
saikonen
left a comment
There was a problem hiding this comment.
overall changes look good. had one minor note on the filter part.
linter step is failing due to unrelated reasons, this is fixed in master now if you want to rebase
| # This is primarily useful when calling `unsubscribe_from` during the cleanup | ||
| # loop in the event handler. | ||
| for k in list(self._subscriptions.keys()): | ||
| for sub in self._subscriptions[k]: |
Collaborator
There was a problem hiding this comment.
note to self: no KeyError possible due to self._subscriptions being a defaultdict(list)
| self.subscriptions = list( | ||
| filter(lambda s: uuid != s.uuid or ws != s.ws, self.subscriptions)) | ||
| self._subscriptions[ws] = list( | ||
| filter(lambda s: uuid != s.uuid or ws != s.ws, self._subscriptions[ws])) |
Collaborator
There was a problem hiding this comment.
is the ws != s.ws check necessary anymore as the subscription is keyed per websocket?
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.
Move subscription tracking to a dict keyed by the underlying websocket object.
Changes the cleanup from two nested for loops (the 2nd being the unsubscribe_from method), to a single loop with (the common case) deleting a single key from a dict.
We found that with a sufficient number of disconnected clients, and long gaps between database activity, the cleanup could end up taking multiple hours. This would cause the python web service to become unresponsive.