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

Fix: More accurate heartbeat Interval #3072

Merged
merged 1 commit into from
Sep 21, 2023

Conversation

chakflying
Copy link
Collaborator

⚠️⚠️⚠️ Since we do not accept all types of pull requests and do not want to waste your time. Please be sure that you have read pull request rules:
https://github.com/louislam/uptime-kuma/blob/master/CONTRIBUTING.md#can-i-create-a-pull-request-for-uptime-kuma

Tick the checkbox if you understand [x]:

  • I have read and understand the pull request rules.

Description

Fixes #3058

Previously, we await on all the async operations in a beat before scheduling the next beat, which means that any blocking operation and timeout will count towards the interval of the next beat. This will drastically increase the duration for long timeouts, and lead to inconsistent beat intervals.

A very simple fix is to schedule the next beat right at the start, so no other operations can affect the accuracy. This gets the beat interval accuracy down to 5-15ms on a Ryzen 5 5600x, which I think the precise ms will depend on CPU load.

Special handling is added for the push monitor. If you call the Push url faster than your set Interval, this will not solve the issue, but I think it's out of scope for this PR.

Type of change

Please delete any options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code follows the style guidelines of this project
  • I ran ESLint and other linters for modified files
  • I have performed a self-review of my own code and tested it
  • I have commented my code, particularly in hard-to-understand areas
    (including JSDoc for methods)
  • My changes generate no new warnings
  • My code needed automated testing. I have added them (this is optional task)

Screenshots (if any)

@chakflying chakflying force-pushed the fix/beat-schedule-delay branch from bf0d2d8 to e6d71a6 Compare May 7, 2023 15:18
@chakflying chakflying force-pushed the fix/beat-schedule-delay branch from e6d71a6 to 9fade3d Compare June 25, 2023 21:02
@CommanderStorm CommanderStorm mentioned this pull request Jun 30, 2023
@louislam louislam added this to the 2.0.0 milestone Jul 8, 2023
@chakflying chakflying force-pushed the fix/beat-schedule-delay branch from 9fade3d to 5b23d33 Compare July 18, 2023 07:21
@chakflying chakflying force-pushed the fix/beat-schedule-delay branch from 5b23d33 to 2dd40d0 Compare July 31, 2023 19:36
@chakflying chakflying changed the title Fix: Schedule next beat at the start of a beat Fix: More accurate heartbeat Interval Jul 31, 2023
@chakflying
Copy link
Collaborator Author

After discussion in #3490, I came up with an even simpler solution:

When setting the next setTimeout, subtract the time taken to run the current heartbeat. Using this method, the loop will run as fast as possible, but there will not be overlap even if the time to run the heartbeat exceeds the defined interval.

Accuracy remains high at around 2-10ms.

@chakflying chakflying force-pushed the fix/beat-schedule-delay branch from 2dd40d0 to 32c60fa Compare August 16, 2023 08:43
@chakflying chakflying changed the base branch from master to 2.0.X August 16, 2023 08:43
@chakflying chakflying changed the base branch from 2.0.X to master September 3, 2023 20:45
@louislam louislam merged commit 33cc96f into louislam:master Sep 21, 2023
@Saibamen
Copy link
Contributor

Please merge this into 1.23.X branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Time axes are not aligned
3 participants