Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add raise BatchBufferOverflowException #1989

Merged
merged 2 commits into from
Dec 16, 2024
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
13 changes: 13 additions & 0 deletions faststream/kafka/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from faststream.exceptions import FastStreamException


class BatchBufferOverflowException(FastStreamException):
"""Exception raised when a buffer overflow occurs when adding a new message to the batches."""

def __init__(self,
message_position: int) -> None:
self.message_position = message_position

def __str__(self) -> str:
return f"The batch buffer is full. The position of the message" \
f" in the transferred collection at which the overflow occurred: {self.message_position}"
8 changes: 5 additions & 3 deletions faststream/kafka/publisher/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from faststream._internal.publisher.proto import ProducerProto
from faststream._internal.subscriber.utils import resolve_custom_func
from faststream.exceptions import FeatureNotSupportedException
from faststream.kafka.exceptions import BatchBufferOverflowException
from faststream.kafka.message import KafkaMessage
from faststream.kafka.parser import AioKafkaParser
from faststream.message import encode_message

from .state import EmptyProducerState, ProducerState, RealProducer

if TYPE_CHECKING:
import asyncio

Expand Down Expand Up @@ -90,7 +90,7 @@ async def publish_batch(

headers_to_send = cmd.headers_to_publish()

for body in cmd.batch_bodies:
for message_position, body in enumerate(cmd.batch_bodies):
message, content_type = encode_message(body)

if content_type:
Expand All @@ -101,12 +101,14 @@ async def publish_batch(
else:
final_headers = headers_to_send.copy()

batch.append(
metadata = batch.append(
key=None,
value=message,
timestamp=cmd.timestamp_ms,
headers=[(i, j.encode()) for i, j in final_headers.items()],
)
if metadata is None:
raise BatchBufferOverflowException(message_position=message_position)

send_future = await self._producer.producer.send_batch(
batch,
Expand Down
Loading