-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mátyás Kuti
committed
Nov 30, 2023
1 parent
dd9e87e
commit a434d3f
Showing
21 changed files
with
247 additions
and
200 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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import annotations | ||
|
||
from confluent_kafka import Consumer | ||
from confluent_kafka.admin import PartitionMetadata | ||
from confluent_kafka.error import KafkaException | ||
from karapace.kafka.common import _KafkaConfigMixin, KafkaClientParams, raise_from_kafkaexception | ||
from typing import Iterable | ||
from typing_extensions import Unpack | ||
|
||
import secrets | ||
|
||
|
||
class KafkaConsumer(_KafkaConfigMixin, Consumer): | ||
def __init__( | ||
self, | ||
topic: str, | ||
bootstrap_servers: Iterable[str] | str, | ||
verify_connection: bool = True, | ||
**params: Unpack[KafkaClientParams], | ||
) -> None: | ||
# The `confluent_kafka.Consumer` does not allow for a missing group id | ||
# if the client of this class does not provide one, we'll generate a | ||
# unique group id to achieve the groupless behaviour | ||
if "group_id" not in params: | ||
params["group_id"] = self._create_group_id() | ||
|
||
super().__init__(bootstrap_servers, verify_connection, **params) | ||
|
||
self.subscribe([topic]) | ||
|
||
@staticmethod | ||
def _create_group_id() -> str: | ||
return f"karapace-{secrets.token_hex(6)}" | ||
|
||
def partitions_for_topic(self, topic: str) -> dict[int, PartitionMetadata]: | ||
"""Returns all partition metadata for the given topic.""" | ||
try: | ||
return self.list_topics(topic).topics[topic].partitions | ||
except KafkaException as exc: | ||
raise_from_kafkaexception(exc) |
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
Oops, something went wrong.