-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.py
52 lines (40 loc) · 1.22 KB
/
kafka.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import Iterator, NamedTuple
from confluent_kafka import Consumer
from sslog import logger
from lib import config
class Msg(NamedTuple):
topic: str
offset: int
key: bytes | None
value: bytes
class KafkaConsumer:
def __init__(self, *topics: str):
self.c = Consumer(
{
"group.id": "tg-notify-bot",
"bootstrap.servers": config.KAFKA_BROKER,
"auto.offset.reset": "earliest",
}
)
self.c.subscribe(list(topics))
def __iter__(self) -> Iterator[Msg]:
while True:
msg = self.c.poll(3)
if msg is None:
continue
if msg.error():
logger.error("consumer error", err=msg.error())
continue
msg_value = _ensure_binary(msg.value())
if msg_value is None:
continue
yield Msg(
topic=msg.topic() or "",
offset=msg.offset() or 0,
key=_ensure_binary(msg.key()),
value=msg_value,
)
def _ensure_binary(s: str | bytes | None) -> bytes | None:
if isinstance(s, str):
return s.encode()
return s