Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

schedule(Repeated)(Async)Task (and friends): jitter support #2505

Closed
weissi opened this issue Aug 17, 2023 · 2 comments · Fixed by #2542 or LorenzoFritzsch/swift-nio#2
Closed

schedule(Repeated)(Async)Task (and friends): jitter support #2505

weissi opened this issue Aug 17, 2023 · 2 comments · Fixed by #2542 or LorenzoFritzsch/swift-nio#2
Labels
good first issue Good for newcomers kind/enhancement Improvements to existing feature.

Comments

@weissi
Copy link
Member

weissi commented Aug 17, 2023

For many stable systems -- especially distributed systems -- jitter is very important to reduce synchronising things that shouldn't be synchronised. For example, say you trigger a large number of health checks once every 10 seconds, you probably don't want them to go out exactly every 10 seconds. That would cause load spikes in your and potentially the other system.

A NIO user can of course calculate jitter manually by doing scheduleRepeatedTask(..., .seconds(10) + .microseconds(Int64.random(in: 0..<10_000)) or so but that's kinda tedious.

It would be much better if once could schedule tasks with a maximum allowed jitter amount and NIO would do the jitter "calculation". For example

.scheduleRepeatedTask(initialDelay: .milliseconds(0), delay: .seconds(10), maximumAllowableJitter: .milliseconds(10) { ... }

which means that a random .zero ..< .milliseconds(10) worth of jitter gets applied every iteration (different random values on each iteration).

But even for single-shot scheduled tasks, we should support jitter.

@weissi weissi added kind/enhancement Improvements to existing feature. good first issue Good for newcomers labels Aug 17, 2023
@natikgadzhi
Copy link
Contributor

Would that mean that every schedule* func gets a new overload with maximumAllowableJitter: as a time interval?

@Lukasa
Copy link
Contributor

Lukasa commented Aug 28, 2023

It would indeed. This should also ideally be done without requiring a new protocol witness, though that may not be possible.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
good first issue Good for newcomers kind/enhancement Improvements to existing feature.
Projects
None yet
3 participants