-
Notifications
You must be signed in to change notification settings - Fork 638
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
Add jitter support to recurring tasks #2542
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this code looks broadly correct! Can we add some unit tests to validate their correct behaviour? EmbeddedEventLoop
will be useful for this as it has a fake clock.
Sources/NIOCore/EventLoop.swift
Outdated
@discardableResult | ||
@inlinable | ||
@preconcurrency | ||
public func flatScheduleTask<T>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also add an extension that offers a variation of scheduleTask
with jitter
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function that schedules a task after a given amount of time, with that amount being jittered? If this is the case I don't see the difference with this overloaded function, and as you pointed out as there's no rescheduling jittering should be unnecessary.
Sources/NIOCore/EventLoop.swift
Outdated
/// Schedule a `task` that is executed by this `EventLoop` after the given amount of time. | ||
/// | ||
/// - parameters: | ||
/// - delay: The delay between the end of one task and the start of the next. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems wrong, as flatScheduleTask
doesn't reschedule anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I misunderstood the nature of this function, I will remove this overload as it's unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, if we remove this overload then that's fine.
Sources/NIOCore/EventLoop.swift
Outdated
/// - note: You can only cancel a task before it has started executing. | ||
@discardableResult | ||
@inlinable | ||
@preconcurrency |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not @preconcurrency
.
Sources/NIOCore/EventLoop.swift
Outdated
_ task: @escaping @Sendable (RepeatedTask) throws -> Void | ||
) -> RepeatedTask { | ||
let jitteredDelay = self._getJitteredDelay(delay: delay, maximumAllowableJitter: maximumAllowableJitter) | ||
return self.scheduleRepeatedTask(initialDelay: initialDelay, delay: jitteredDelay, notifying: promise, task) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we jitter the initial delay as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes will do, jittering the initial delay will avoid a first load spike, which is part of what this PR should fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great, just a quick note.
Sources/NIOCore/EventLoop.swift
Outdated
/// - task: The closure that will be executed. | ||
/// - return: `RepeatedTask` | ||
@discardableResult | ||
@preconcurrency |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove the @preconcurrency
annotations here, these functions are new.
Sources/NIOCore/EventLoop.swift
Outdated
/// | ||
/// - return: `RepeatedTask` | ||
@discardableResult | ||
@preconcurrency |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same preconcurrency note here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one, LGTM.
@swift-server-bot test this please |
Couple of build errors need to be cleaned up I'm afraid:
|
@swift-server-bot test this please |
1 similar comment
@swift-server-bot test this please |
Add jitter support to recurring tasks (apple#2542)
Add jitter support to functions managing recurring tasks.
Motivation:
Closes #2505.
Modifications:
Overloaded the following
EventLoop
functions that might need jitter support:flatScheduleTask
scheduleRepeatedTask
scheduleRepeatedAsyncTask
with the following new parameter:
maximumAllowableJitter: TimeAmount
, which is the exclusive upper bound of the jitter range that will be added to thedelay
andinitialDelay
parameters.Result:
If jitter is needed when scheduling a repeated task with one of the functions listed above, it will be possible to add it by using the
maximumAllowableJitter
parameter.