Skip to content

Commit 565084d

Browse files
authored
Support for disabling overflow-warning logs in broadcast receivers (#329)
2 parents 718ce9d + 8472db2 commit 565084d

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
## New Features
1212

13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
13+
- Added support for disabling the overflow-warning log messagess in broadcast receivers, through the `warn_on_overflow` parameter on `Broadcast.new_receiver()` calls.
1414

1515
## Bug Fixes
1616

src/frequenz/channels/_broadcast.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def new_sender(self) -> Sender[ChannelMessageT]:
267267
return _Sender(self)
268268

269269
def new_receiver(
270-
self, *, name: str | None = None, limit: int = 50
270+
self, *, name: str | None = None, limit: int = 50, warn_on_overflow: bool = True
271271
) -> Receiver[ChannelMessageT]:
272272
"""Return a new receiver attached to this channel.
273273
@@ -278,11 +278,15 @@ def new_receiver(
278278
Args:
279279
name: A name to identify the receiver in the logs.
280280
limit: Number of messages the receiver can hold in its buffer.
281+
warn_on_overflow: Whether to log a warning when the receiver's
282+
buffer is full and a message is dropped.
281283
282284
Returns:
283285
A new receiver attached to this channel.
284286
"""
285-
recv: _Receiver[ChannelMessageT] = _Receiver(self, name=name, limit=limit)
287+
recv: _Receiver[ChannelMessageT] = _Receiver(
288+
self, name=name, limit=limit, warn_on_overflow=warn_on_overflow
289+
)
286290
self._receivers[hash(recv)] = weakref.ref(recv)
287291
if self.resend_latest and self._latest is not None:
288292
recv.enqueue(self._latest)
@@ -371,7 +375,13 @@ class _Receiver(Receiver[_T]):
371375
"""
372376

373377
def __init__(
374-
self, channel: Broadcast[_T], /, *, name: str | None, limit: int
378+
self,
379+
channel: Broadcast[_T],
380+
/,
381+
*,
382+
name: str | None,
383+
limit: int,
384+
warn_on_overflow: bool,
375385
) -> None:
376386
"""Initialize this receiver.
377387
@@ -387,7 +397,11 @@ def __init__(
387397
purposes, it will be shown in the string representation of the
388398
receiver.
389399
limit: Number of messages the receiver can hold in its buffer.
400+
warn_on_overflow: Whether to log a warning when the receiver's
401+
buffer is full and a message is dropped.
390402
"""
403+
self._warn_on_overflow: bool = warn_on_overflow
404+
391405
self._name: str = name if name is not None else f"{id(self):_}"
392406
"""The name to identify the receiver.
393407
@@ -412,10 +426,11 @@ def enqueue(self, message: _T, /) -> None:
412426
"""
413427
if len(self._q) == self._q.maxlen:
414428
self._q.popleft()
415-
_logger.warning(
416-
"Broadcast receiver [%s] is full. Oldest message was dropped.",
417-
self,
418-
)
429+
if self._warn_on_overflow:
430+
_logger.warning(
431+
"Broadcast receiver [%s] is full. Oldest message was dropped.",
432+
self,
433+
)
419434
self._q.append(message)
420435

421436
def __len__(self) -> int:

0 commit comments

Comments
 (0)