This repository was archived by the owner on Aug 19, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 127
feat: add kafka broadcast support #2
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import asyncio | ||
| import typing | ||
| from urllib.parse import urlparse | ||
|
|
||
| from aiokafka import AIOKafkaConsumer, AIOKafkaProducer | ||
|
|
||
| from .._base import Event | ||
| from .base import BroadcastBackend | ||
|
|
||
|
|
||
| class KafkaBackend(BroadcastBackend): | ||
| def __init__(self, url: str): | ||
| self._servers = [urlparse(url).netloc] | ||
| self._consumer_channels: typing.Set = set() | ||
|
|
||
| async def connect(self) -> None: | ||
| loop = asyncio.get_event_loop() | ||
| self._producer = AIOKafkaProducer(loop=loop, bootstrap_servers=self._servers) | ||
| self._consumer = AIOKafkaConsumer(loop=loop, bootstrap_servers=self._servers) | ||
| await self._producer.start() | ||
| await self._consumer.start() | ||
|
|
||
| async def disconnect(self) -> None: | ||
| await self._producer.stop() | ||
| await self._consumer.stop() | ||
|
|
||
| async def subscribe(self, channel: str) -> None: | ||
| self._consumer_channels.add(channel) | ||
| self._consumer.subscribe(topics=self._consumer_channels) | ||
|
|
||
| async def unsubscribe(self, channel: str) -> None: | ||
| await self._consumer.unsubscribe() | ||
|
|
||
| async def publish(self, channel: str, message: typing.Any) -> None: | ||
| await self._producer.send_and_wait(channel, message.encode("utf8")) | ||
|
|
||
| async def next_published(self) -> Event: | ||
| message = await self._consumer.getone() | ||
| return Event(channel=message.topic, message=message.value.decode("utf8")) | ||
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,38 @@ | ||
| version: '3' | ||
woile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| services: | ||
| zookeeper: | ||
| image: "confluentinc/cp-zookeeper" | ||
| hostname: zookeeper | ||
| ports: | ||
| - 32181:32181 | ||
| environment: | ||
| - ZOOKEEPER_CLIENT_PORT=32181 | ||
| - ALLOW_ANONYMOUS_LOGIN=yes | ||
| kafka: | ||
| image: confluentinc/cp-kafka | ||
| hostname: kafka | ||
| ports: | ||
| - 9092:9092 | ||
| - 29092:29092 | ||
| depends_on: | ||
| - zookeeper | ||
| environment: | ||
| - KAFKA_ZOOKEEPER_CONNECT=zookeeper:32181 | ||
| - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 | ||
| - KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT | ||
| - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT_HOST://localhost:29092,PLAINTEXT://kafka:9092 | ||
| - KAFKA_BROKER_ID=1 | ||
| - ALLOW_PLAINTEXT_LISTENER=yes | ||
| redis: | ||
| image: "redis:alpine" | ||
| ports: | ||
| - 6379:6379 | ||
| postgres: | ||
| image: "postgres:12" | ||
| environment: | ||
| - POSTGRES_DB=hostedapi | ||
| - POSTGRES_PASSWORD=postgres | ||
| - POSTGRES_HOST_AUTH_METHOD=trust | ||
| - POSTGRES_USER=postgres | ||
| ports: | ||
| - 5432:5432 | ||
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,24 @@ | ||
| # Setup | ||
|
|
||
| Install python dependencies in your virtualenv | ||
|
|
||
| ```bash | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| Run example with memory as backend. | ||
|
|
||
| ```bash | ||
| uvicorn example.app:app | ||
| ``` | ||
|
|
||
| You can also install broadcaster locally using `pip install -e .`. | ||
|
|
||
| In order to run the app with different backends, you have to set the env | ||
| `BROADCAST_URL` and start the docker services. | ||
|
|
||
| | Backend | Env | Service command | | ||
| | -------- | ---------------------------------------------------------- | ---------------------------- | | ||
| | kafka | `export BROADCAST_URL=kafka://localhost:9092` | `docker-compose up kafka` | | ||
| | redis | `export BROADCAST_URL=redis://localhost:6379` | `docker-compose up redis` | | ||
| | postgres | `export BROADCAST_URL=postgres://localhost:5432/hostedapi` | `docker-compose up postgres` | |
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,4 @@ | ||
| uvicorn | ||
| starlette | ||
| jinja2 | ||
| broadcaster[redis,postgres,kafka] |
File renamed without changes.
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,14 @@ | ||
| #!/bin/sh -e | ||
| # Accepted values: postgres, kafka, redis | ||
| # If no variable provided all services will start | ||
| if [ -n "$1" ]; then | ||
| if [ "$1" != "kafka" ] && [ "$1" != "redis" ] && [ "$1" != "postgres" ]; then | ||
| echo "Not a valid value. Choose one or none: | ||
| kafka | ||
| redis | ||
| postgres "; | ||
| exit 1; | ||
| fi | ||
| fi | ||
|
|
||
| docker-compose up $1 |
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
Empty file.
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
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.
Uh oh!
There was an error while loading. Please reload this page.