Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions fastloom/signals/kafka/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,66 @@

if TYPE_CHECKING:
from faststream.confluent.fastapi import KafkaRouter
from faststream.confluent.publisher.producer import (
AsyncConfluentFastProducerImpl,
)
from faststream.confluent.response import KafkaPublishCommand


def get_kafka_router(settings: KafkaSettings) -> KafkaRouter:
# deferred: see docs/signals.md#ordering
from faststream.confluent.fastapi import KafkaRouter
from faststream.confluent.publisher.producer import (
AsyncConfluentFastProducerImpl,
)

_patch_real_tombstones(AsyncConfluentFastProducerImpl)

return KafkaRouter(
settings.KAFKA_URI,
schema_url="/kafkaapi",
)


def _patch_real_tombstones(
producer_cls: type[AsyncConfluentFastProducerImpl],
) -> None:
# NOTE: publish(None, ...) isn't a real Kafka tombstone - the codec
# turns None into b"", and compaction only reclaims a key on a literal
# null value (ag2ai/faststream#1967). The real fix (ag2ai/faststream#2932)
# is merged upstream but PyPI rejects packages with direct git/URL
# dependencies, so we can't just point at git main - keep this shim until
# faststream cuts an actual PyPI release containing it. Wraps .publish()
# in place, at the same deferred point get_kafka_router() imports from -
# see docs/signals.md#ordering.
if getattr(producer_cls, "_fastloom_real_tombstones", False):
return

original_publish = producer_cls.publish

async def publish(
self: AsyncConfluentFastProducerImpl, cmd: KafkaPublishCommand
):
if cmd.body is not None:
return await original_publish(self, cmd)

headers_to_send = {"content-type": "", **cmd.headers_to_publish()}
return await self._producer.producer.send( # noqa: SLF001
topic=cmd.destination,
value=None,
key=cmd.key,
partition=cmd.partition,
timestamp_ms=cmd.timestamp_ms,
headers=[
(i, (j or "").encode()) for i, j in headers_to_send.items()
],
no_confirm=cmd.no_confirm,
)

producer_cls.publish = publish
producer_cls._fastloom_real_tombstones = True


class KafkaSubscriber(SelfSustaining):
"""Owns the shared FastStream KafkaRouter singleton."""

Expand Down
2 changes: 1 addition & 1 deletion plugins/fastloom-sdk/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fastloom-sdk",
"description": "Skills for services that consume the fastloom library \u2014 scaffold new services, add routes and RabbitMQ subscribers, audit settings.",
"version": "0.4.49",
"version": "0.4.50",
"author": {
"name": "aradng"
},
Expand Down
Loading
Loading