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
Implement ThreadPoolExecutor publisher pattern for EventBridge (WIP) #10260
Open
josefdlange
wants to merge
4
commits into
localstack:master
Choose a base branch
from
josefdlange:feature/eventbridge-threadpool-for-sending
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e52ceaf
Implement ThreadPoolExecutor publisher pattern for EventBridge so tha…
josefdlange 8a50ec4
format/lint
josefdlange cc114da
Merge branch 'master' of https://github.com/localstack/localstack int…
josefdlange 74ca3ca
Wrap publisher usage in a function to appease moto
josefdlange File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,64 @@ | ||
import logging | ||
from concurrent.futures.thread import ThreadPoolExecutor | ||
from typing import Dict | ||
|
||
from localstack.utils.aws.message_forwarding import send_event_to_target as send_event_to_target_ | ||
from localstack.utils.common import truncate | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def send_event_to_target( | ||
target_arn: str, | ||
event: Dict, | ||
target_attributes: Dict = None, | ||
asynchronous: bool = True, | ||
target: Dict = None, | ||
role: str = None, | ||
source_arn: str = None, | ||
source_service: str = None, | ||
): | ||
try: | ||
send_event_to_target_( | ||
target_arn, | ||
event, | ||
target_attributes=target_attributes, | ||
asynchronous=asynchronous, | ||
target=target, | ||
role=role, | ||
source_arn=source_arn, | ||
source_service=source_service, | ||
) | ||
except Exception as e: | ||
LOG.info(f"Unable to send event notification {truncate(event)} to target {target}: {e}") | ||
|
||
|
||
class EventTargetPublisher: | ||
def __init__(self, thread_count: int = 10): | ||
self.executor = ThreadPoolExecutor(thread_count, thread_name_prefix="eb_targets") | ||
|
||
def shutdown(self): | ||
self.executor.shutdown(wait=False) | ||
|
||
def send_event_to_target( | ||
self, | ||
target_arn: str, | ||
event: Dict, | ||
target_attributes: Dict = None, | ||
asynchronous: bool = True, | ||
target: Dict = None, | ||
role: str = None, | ||
source_arn: str = None, | ||
source_service: str = None, | ||
): | ||
self.executor.submit( | ||
send_event_to_target, | ||
target_arn, | ||
event, | ||
target_attributes=target_attributes, | ||
asynchronous=asynchronous, | ||
target=target, | ||
role=role, | ||
source_arn=source_arn, | ||
source_service=source_service, | ||
) |
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.
The diff, especially around here, is a little messy-looking but it's mainly creating class
PutEventsHandler
that is callable, the__call__
method being the same implementation asevents_handler_put_events
. This class also absorbs some functions into internal methods, mainly because they're only used and needed for the handler's sake. Implementation details should remain relatively the same, save for the executor-publisher being involved instead of directly callingsend_event_to_target