-
Notifications
You must be signed in to change notification settings - Fork 6
feat: SSE #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feat: SSE #75
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0c4862c
sse init
JamieSinn 8b524ae
init sse work
JamieSinn d7bc293
sse wip
JamieSinn c10ae92
sse refetch config
JamieSinn 2268099
sse refetch config and fix delay
JamieSinn fd6492d
update requirements
JamieSinn d60ed93
lint
JamieSinn 005cfa3
update versions used in testing
JamieSinn 45e2761
fix import
JamieSinn 0d5048f
Use optional
JamieSinn d86fc19
ignore ldeventsource in mypy
JamieSinn ab7de77
Fix callable args
JamieSinn 047c3e5
Fix callable args
JamieSinn 56996ea
reformat
JamieSinn ee7ce5b
remove time delay for sse
JamieSinn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import threading | ||
|
|
||
| import ld_eventsource | ||
| import ld_eventsource.actions | ||
| import ld_eventsource.config | ||
| from typing import Callable | ||
|
|
||
|
|
||
| class SSEManager: | ||
| def __init__( | ||
| self, | ||
| handlestate: Callable[[ld_eventsource.actions.Start], None], | ||
| handleerror: Callable[[ld_eventsource.actions.Fault], None], | ||
| handlemessage: Callable[[ld_eventsource.actions.Event], None], | ||
| ): | ||
| self.client: ld_eventsource.SSEClient = None | ||
| self.url = "" | ||
| self.handlestate = handlestate | ||
| self.handleerror = handleerror | ||
| self.handlemessage = handlemessage | ||
|
|
||
| self.read_thread = threading.Thread( | ||
| target=self.read_events, | ||
| args=(self.handlestate, self.handleerror, self.handlemessage), | ||
| ) | ||
|
|
||
| def read_events( | ||
| self, | ||
| handlestate: Callable[[ld_eventsource.actions.Start], None], | ||
| handleerror: Callable[[ld_eventsource.actions.Fault], None], | ||
| handlemessage: Callable[[ld_eventsource.actions.Event], None], | ||
| ): | ||
| self.client.start() | ||
| for event in self.client.all: | ||
| if isinstance(event, ld_eventsource.actions.Start): | ||
| handlestate(event) | ||
| elif isinstance(event, ld_eventsource.actions.Fault): | ||
| handleerror(event) | ||
| elif isinstance(event, ld_eventsource.actions.Event): | ||
| handlemessage(event) | ||
|
|
||
| def update(self, config: dict): | ||
| if self.use_new_config(config["sse"]): | ||
| self.url = config["sse"]["hostname"] + config["sse"]["path"] | ||
| if self.client is not None: | ||
| self.client.close() | ||
| if self.read_thread.is_alive(): | ||
| self.read_thread.join() | ||
| self.client = ld_eventsource.SSEClient( | ||
| connect=ld_eventsource.config.ConnectStrategy.http(self.url), | ||
| error_strategy=ld_eventsource.config.ErrorStrategy.CONTINUE, | ||
| ) | ||
| self.read_thread = threading.Thread( | ||
| target=self.read_events, | ||
| args=(self.handlestate, self.handleerror, self.handlemessage), | ||
| ) | ||
| self.read_thread.start() | ||
|
|
||
| def use_new_config(self, config: dict) -> bool: | ||
| new_url = config["hostname"] + config["path"] | ||
| if self.url == "" or self.url is None and new_url != "": | ||
| return True | ||
| return self.url != new_url | ||
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does the threading here work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SSE Manager itself creates a thread managed by it - when a new url is found - it creates/updates the current thread used for processing the events incoming.
This is mostly required due to the eventsource client must run in its own thread - and you must read from the same thread that you started the client in.