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

Timer: Add support for rearming timer with new interval #321

Merged
merged 3 commits into from
Sep 19, 2024

Conversation

Marenz
Copy link
Contributor

@Marenz Marenz commented Sep 18, 2024

Ugly alternative to #320

They eat our timers, our start_delays!

@github-actions github-actions bot added part:docs Affects the documentation part:utilities Affects the utility receivers (`Timer`, `Event`, `FileWatcher`) labels Sep 18, 2024
@Marenz Marenz marked this pull request as ready for review September 18, 2024 11:08
@Marenz Marenz requested a review from a team as a code owner September 18, 2024 11:08
@Marenz Marenz requested a review from llucax September 18, 2024 11:08
Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of minor comments to improve clarity, as it wasn't trivial for me to understand all that was going on.

Also I think this bug fix and new feature deserve some tests.

self._next_tick_time = self._now() + start_delay_ms + self._interval

if prev_next_tick_time and prev_next_tick_time > self._next_tick_time:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is to know if this is the first time the timer is started? I think it is worth a comment. Also for the > condition, as it wasn't obvious for me (I hope I'm getting it right).

Suggested change
if prev_next_tick_time and prev_next_tick_time > self._next_tick_time:
# If there was no previous `next_tick_time`, it means the timer wasn't started yet, so no need to
# fire the reset event. If the new `_next_tick_time` is bigger than the previous one, we also don't
# need to fire the reset even, as `ready()` will keep looping until the new `_next_tick_time` is
# reached.
if prev_next_tick_time and prev_next_tick_time > self._next_tick_time:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny enough, I didn't even think about it, I was just making mypy happy 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mainly checking if the new time is ealier or not like you described.
But I think we can simplify this.

@@ -664,7 +683,12 @@ async def ready(self) -> bool: # noqa: DOC502
# could be reset while we are sleeping, in which case we need to recalculate
# the time to the next tick and try again.
while time_to_next_tick > 0:
await asyncio.sleep(time_to_next_tick / 1_000_000)
for fin in asyncio.as_completed(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does fin stands for? Why not completed or something more obvious?

Comment on lines 686 to 690
for fin in asyncio.as_completed(
[asyncio.sleep(time_to_next_tick / 1_000_000), self._reset_event.wait()]
):
await fin
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit confused by this break too (I somehow thought it was breaking the outer while instead of the for).

Maybe it could be a bit more clear like this?

Suggested change
for fin in asyncio.as_completed(
[asyncio.sleep(time_to_next_tick / 1_000_000), self._reset_event.wait()]
):
await fin
break
await next(asyncio.as_completed(
[asyncio.sleep(time_to_next_tick / 1_000_000), self._reset_event.wait()]
))

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
@github-actions github-actions bot added the part:tests Affects the unit, integration and performance (benchmarks) tests label Sep 19, 2024
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
llucax
llucax previously approved these changes Sep 19, 2024
Comment on lines +622 to +623
if self.is_running:
self._reset_event.set()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, was the if necessary or is it just an optimization? Going through it quickly it looks like it should work without it too (if the timer is not running, it will just break the wait loop but it should resume normally, afterwards, right?), but maybe I'm missing some edge case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would probably be fine if we just set it every time, yes.

@llucax
Copy link
Contributor

llucax commented Sep 19, 2024

Not sure how strict we should be about this, but the bug fix should be applied to v1.1.x, but the new feature should go to v1.2.0.

I think to have it both and not being that bureaucratic, you could split it in 2 PRs, bug fix and feature, and we merge first the bug fix, release v1.1.3, then merge the feature and release v1.2.0.

@llucax
Copy link
Contributor

llucax commented Sep 19, 2024

For simplicity I think we can release all of this in v1.2.0 and that's it.

@llucax llucax added this to the v1.2.0 milestone Sep 19, 2024
@Marenz Marenz added this pull request to the merge queue Sep 19, 2024
Merged via the queue into frequenz-floss:v1.x.x with commit 68eb88e Sep 19, 2024
14 checks passed
@Marenz Marenz deleted the rearm branch September 19, 2024 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:utilities Affects the utility receivers (`Timer`, `Event`, `FileWatcher`)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants