forked from encode/broadcaster
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pulsar integration * based off the review * fix failling ci * setup workflow test for pulsar * done
- Loading branch information
Showing
12 changed files
with
141 additions
and
14 deletions.
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
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,77 @@ | ||
import anyio | ||
import logging | ||
import typing | ||
from urllib.parse import urlparse | ||
import pulsar | ||
from broadcaster._base import Event | ||
from .base import BroadcastBackend | ||
|
||
|
||
class PulsarBackend(BroadcastBackend): | ||
def __init__(self, url: str): | ||
parsed_url = urlparse(url) | ||
self._host = parsed_url.hostname or "localhost" | ||
self._port = parsed_url.port or 6650 | ||
self._service_url = f"pulsar://{self._host}:{self._port}" | ||
self._client = None | ||
self._producer = None | ||
self._consumer = None | ||
|
||
async def connect(self) -> None: | ||
try: | ||
logging.info("Connecting to brokers") | ||
self._client = await anyio.to_thread.run_sync( | ||
lambda: pulsar.Client(self._service_url) | ||
) | ||
self._producer = await anyio.to_thread.run_sync( | ||
lambda: self._client.create_producer("broadcast") | ||
) | ||
self._consumer = await anyio.to_thread.run_sync( | ||
lambda: self._client.subscribe( | ||
"broadcast", | ||
subscription_name="broadcast_subscription", | ||
consumer_type=pulsar.ConsumerType.Shared, | ||
) | ||
) | ||
logging.info("Successfully connected to brokers") | ||
except Exception as e: | ||
logging.error(e) | ||
raise e | ||
|
||
async def disconnect(self) -> None: | ||
if self._producer: | ||
await anyio.to_thread.run_sync(self._producer.close) | ||
if self._consumer: | ||
await anyio.to_thread.run_sync(self._consumer.close) | ||
if self._client: | ||
await anyio.to_thread.run_sync(self._client.close) | ||
|
||
async def subscribe(self, channel: str) -> None: | ||
# In this implementation, we're using a single topic 'broadcast' | ||
# So we don't need to do anything here | ||
pass | ||
|
||
async def unsubscribe(self, channel: str) -> None: | ||
# Similarly, we don't need to do anything here | ||
pass | ||
|
||
async def publish(self, channel: str, message: typing.Any) -> None: | ||
encoded_message = f"{channel}:{message}".encode("utf-8") | ||
await anyio.to_thread.run_sync(lambda: self._producer.send(encoded_message)) | ||
|
||
async def next_published(self) -> Event: | ||
while True: | ||
try: | ||
msg = await anyio.to_thread.run_sync(self._consumer.receive) | ||
channel, content = msg.data().decode("utf-8").split(":", 1) | ||
await anyio.to_thread.run_sync(lambda: self._consumer.acknowledge(msg)) | ||
return Event(channel=channel, message=content) | ||
|
||
except anyio.get_cancelled_exc_class(): | ||
# cancellation | ||
logging.info("next_published task is being cancelled") | ||
raise | ||
|
||
except Exception as e: | ||
logging.error(f"Error in next_published: {e}") | ||
raise |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
-e .[redis,postgres,kafka] | ||
-e .[redis,postgres,kafka,pulsar] | ||
|
||
# Documentation | ||
mkdocs==1.3.1 | ||
|
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 |
---|---|---|
@@ -1,19 +1,15 @@ | ||
#!/bin/sh -e | ||
|
||
# Use the Python executable provided from the `-p` option, or a default. | ||
[ "$1" = "-p" ] && PYTHON=$2 || PYTHON="python3" | ||
|
||
REQUIREMENTS="requirements.txt" | ||
VENV="venv" | ||
|
||
set -x | ||
|
||
if [ -z "$GITHUB_ACTIONS" ]; then | ||
"$PYTHON" -m venv "$VENV" | ||
PIP="$VENV/bin/pip" | ||
else | ||
PIP="pip" | ||
fi | ||
|
||
"$PIP" install -r "$REQUIREMENTS" | ||
"$PIP" install --upgrade setuptools twine | ||
"$PIP" install -e . |
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
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