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

Optimize job setTimer #363

Merged
merged 2 commits into from Aug 1, 2022
Merged

Conversation

fufuok
Copy link

@fufuok fufuok commented Jul 31, 2022

What does this do?

One more optimization:

When NTP time is calibrated or a leap second occurs, the following conditions may occur:

Now: 2022-01-01T00:00:00Z
Job:
nextRun.duration = 2*time.Mintue,
nextRun.dateTime = 2022-01-01T00:02:00Z

//...

Now: 2022-01-01T00:02:00Z
// ntpdate
// system time is corrected
Now: 2022-01-01T00:01:58Z

job.setTimer(time.AfterFunc(next.duration, func() {
	if !next.dateTime.IsZero() {
		for {
			if s.now().Unix() >= next.dateTime.Unix() {
				break
			}
			// ...will loop for 2 seconds
		}
	}
	s.runContinuous(job)
}))

This should be better:

job.setTimer(time.AfterFunc(next.duration, func() {
	if !next.dateTime.IsZero() {
		for {
			n := s.now().UnixNano() - next.dateTime.UnixNano()
			if n >= 0 {
				break
			}
			s.time.Sleep(time.Duration(n))
		}
	}
	s.runContinuous(job)
}))

Which issue(s) does this PR fix/relate to?

List any changes that modify/break current functionality

Have you included tests for your changes?

Did you document any new/modified functionality?

  • Updated example_test.go
  • Updated README.md

Notes

Copy link
Contributor

@JohnRoesler JohnRoesler left a comment

Choose a reason for hiding this comment

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

nice, thank you!

@JohnRoesler JohnRoesler merged commit 22fc237 into go-co-op:main Aug 1, 2022
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.

None yet

2 participants