Skip to content

Commit 40ab70c

Browse files
authored
Make resend_latest a public attribute for Broadcast channels (#221)
This allows modifying the flag after the channel is constructed, for example in dynamically created channels.
2 parents e32a759 + c9f3119 commit 40ab70c

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ The `Timer` now can be started with a delay.
1414

1515
This can be useful, for example, if the timer needs to be *aligned* to a particular time. The alternative to this would be to `sleep()` for the time needed to align the timer, but if the `sleep()` call gets delayed because the event loop is busy, then a re-alignment is needed and this could go on for a while. The only way to guarantee a certain alignment (with a reasonable precision) is to delay the timer start.
1616

17+
* `Broadcast.resend_latest` is now a public attribute, allowing it to be changed after the channel is created.
18+
1719
## Bug Fixes
1820

1921
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->

src/frequenz/channels/_broadcast.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,35 @@ def __init__(self, name: str, resend_latest: bool = False) -> None:
7575
"""Create a Broadcast channel.
7676
7777
Args:
78-
name: A name for the broadcast channel, typically based on the type
79-
of data sent through it. Used to identify the channel in the
80-
logs.
78+
name: A name for the broadcast channel, typically based on the type of data
79+
sent through it. Used to identify the channel in the logs.
8180
resend_latest: When True, every time a new receiver is created with
82-
`new_receiver`, it will automatically get sent the latest value
83-
on the channel. This allows new receivers on slow streams to
84-
get the latest value as soon as they are created, without having
85-
to wait for the next message on the channel to arrive.
81+
`new_receiver`, it will automatically get sent the latest value on the
82+
channel. This allows new receivers on slow streams to get the latest
83+
value as soon as they are created, without having to wait for the next
84+
message on the channel to arrive. It is safe to be set in
85+
data/reporting channels, but is not recommended for use in channels that
86+
stream control instructions.
8687
"""
8788
self._name: str = name
8889
"""The name of the broadcast channel.
8990
9091
Only used for debugging purposes.
9192
"""
9293

93-
self._resend_latest = resend_latest
94-
"""Whether to resend the latest value to new receivers."""
94+
self.resend_latest: bool = resend_latest
95+
"""Whether to resend the latest value to new receivers.
96+
97+
It is `False` by default.
98+
99+
When `True`, every time a new receiver is created with `new_receiver`, it will
100+
automatically get sent the latest value on the channel. This allows new
101+
receivers on slow streams to get the latest value as soon as they are created,
102+
without having to wait for the next message on the channel to arrive.
103+
104+
It is safe to be set in data/reporting channels, but is not recommended for use
105+
in channels that stream control instructions.
106+
"""
95107

96108
self._recv_cv: Condition = Condition()
97109
"""The condition to wait for data in the channel's buffer."""
@@ -148,7 +160,7 @@ def new_receiver(self, name: str | None = None, maxsize: int = 50) -> Receiver[T
148160
name = str(uuid)
149161
recv: Receiver[T] = Receiver(uuid, name, maxsize, self)
150162
self._receivers[uuid] = weakref.ref(recv)
151-
if self._resend_latest and self._latest is not None:
163+
if self.resend_latest and self._latest is not None:
152164
recv.enqueue(self._latest)
153165
return recv
154166

0 commit comments

Comments
 (0)