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

Make resend_latest a public attribute for Broadcast channels #221

Merged
merged 3 commits into from
Oct 26, 2023
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
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The `Timer` now can be started with a delay.

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.

* `Broadcast.resend_latest` is now a public attribute, allowing it to be changed after the channel is created.

## Bug Fixes

<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
32 changes: 22 additions & 10 deletions src/frequenz/channels/_broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,35 @@ def __init__(self, name: str, resend_latest: bool = False) -> None:
"""Create a Broadcast channel.

Args:
name: A name for the broadcast channel, typically based on the type
of data sent through it. Used to identify the channel in the
logs.
name: A name for the broadcast channel, typically based on the type of data
sent through it. Used to identify the channel in the logs.
resend_latest: When True, every time a new receiver is created with
`new_receiver`, it will automatically get sent the latest value
on the channel. This allows new receivers on slow streams to
get the latest value as soon as they are created, without having
to wait for the next message on the channel to arrive.
`new_receiver`, it will automatically get sent the latest value on the
channel. This allows new receivers on slow streams to get the latest
value as soon as they are created, without having to wait for the next
message on the channel to arrive. It is safe to be set in
data/reporting channels, but is not recommended for use in channels that
stream control instructions.
"""
self._name: str = name
"""The name of the broadcast channel.

Only used for debugging purposes.
"""

self._resend_latest = resend_latest
"""Whether to resend the latest value to new receivers."""
self.resend_latest: bool = resend_latest
"""Whether to resend the latest value to new receivers.

It is `False` by default.

When `True`, every time a new receiver is created with `new_receiver`, it will
automatically get sent the latest value on the channel. This allows new
receivers on slow streams to get the latest value as soon as they are created,
without having to wait for the next message on the channel to arrive.

It is safe to be set in data/reporting channels, but is not recommended for use
in channels that stream control instructions.
"""

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

Expand Down