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

Add EventLoop APIs for simpler scheduling of callbacks #2759

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift click to select a range
5f7065f
benchmarks: Add benchmark for MTELG.scheduleTask(in:_:)
simonjbeaumont Jun 28, 2024
33e82e0
api: Add NIOTimer, NIOTimerHandler, and EventLoop.setTimer(for:_:)
simonjbeaumont Jun 28, 2024
b59e31d
benchmarks: Add benchmark for MTELG.setTimer(for:_:)
simonjbeaumont Jun 28, 2024
17cf105
internal: Add NIOCustomTimerImplementation conformance to SelectableE…
simonjbeaumont Jun 28, 2024
0afb3a2
test: Add Linux pre-5.9.2 backport for fulfillment(of:timeout:enforce…
simonjbeaumont Jul 1, 2024
df3cdb1
test: Increase timer used in shutdown test
simonjbeaumont Jul 1, 2024
b0d3f66
feedback: Make NIOTimer Sendable
simonjbeaumont Jul 5, 2024
907c087
feedback: Rename timerFired(loop:) to timerFired(eventLoop:)
simonjbeaumont Jul 8, 2024
b3e4903
feedback(attempted): Store a closure instead of UInt64
simonjbeaumont Jul 8, 2024
fb5e835
feedback(reverted, allocates): Store a closure instead of UInt64
simonjbeaumont Jul 8, 2024
89c57ec
feedback(unsure): Replace extra protocol with runtime checks
simonjbeaumont Jul 8, 2024
59a04de
feedback(unsure): Generic timerFired protocol witness
simonjbeaumont Jul 8, 2024
06a4ce7
feedback(unsure): Make setTimer generic over the handler
simonjbeaumont Jul 8, 2024
950db0c
feedback: Use labelled parameter for handler
simonjbeaumont Jul 8, 2024
d6ae472
feedback: Use separate prepositions for TimeAmount and NIODeadline APIs
simonjbeaumont Jul 8, 2024
4439ac6
Remove DocC disambiguation for now until API is decided
simonjbeaumont Jul 8, 2024
abd4b28
feedback: Add documentation to NIOTimerHandler.timerFired protocol re…
simonjbeaumont Jul 8, 2024
ceabc7b
feedback: Change API terms from setTimer to scheduleCallback
simonjbeaumont Jul 8, 2024
80d91f6
feedback: Local variable rename: taskId -> taskID
simonjbeaumont Jul 10, 2024
2ec5543
feedback: Reanme NIOScheduledCallbackHandler.onSchedule to handleSche…
simonjbeaumont Jul 10, 2024
5d6ac17
feedback: Update protocol requirement documentation comments to use D…
simonjbeaumont Jul 10, 2024
bbf8f9f
feedback: Remove explicit benchmark.stopMeasurement calls
simonjbeaumont Jul 11, 2024
21fd1cc
feedback: Remove TODO following discussion about internal inits
simonjbeaumont Jul 11, 2024
9bf65f6
feedback: Make scheduleCallback throwing
simonjbeaumont Jul 11, 2024
dbba9e3
feedback: Add a missing explicit self
simonjbeaumont Jul 11, 2024
86b8ac3
Merge remote-tracking branch 'upstream/main' into sb/timer-api
simonjbeaumont Jul 12, 2024
b16a575
Remove broken DocC disambiguatoin and fix test calls
simonjbeaumont Jul 12, 2024
4e71ffb
Update implementation comments in EmbeddedEventLoop and AsyncTestingE…
simonjbeaumont Jul 15, 2024
b909365
feedback: Fix preconditionFailure message
simonjbeaumont Jul 15, 2024
5559772
feedback: Move SheduledTask.Kind and kind above other properties
simonjbeaumont Jul 15, 2024
7f159be
feedback: Measure .instructions instead of .cpuTotal
simonjbeaumont Jul 15, 2024
3ad5647
feedback: Remove vestigial references to setTimer in impl comments
simonjbeaumont Jul 15, 2024
bd8fa73
Merge remote-tracking branch 'upstream/main' into HEAD
simonjbeaumont Jul 25, 2024
6af561d
Add cancellation callback and implicitly cancel on shutdown
simonjbeaumont Jul 19, 2024
81cc415
format: Update for new format and lint rules
simonjbeaumont Jul 25, 2024
37ebfde
Merge remote-tracking branch 'upstream/main' into sb/timer-api
simonjbeaumont Jul 30, 2024
4467728
Remove use of Task.sleep(for:) in tests
simonjbeaumont Jul 30, 2024
6544461
Merge remote-tracking branch 'upstream/main' into sb/timer-api
simonjbeaumont Aug 15, 2024
c48b7aa
Update benchmark to use same config as other benchmarks
simonjbeaumont Aug 15, 2024
151c2c8
Rename onCancelScheduledCallback to didCancelScheduledCallback
simonjbeaumont Aug 15, 2024
5e61930
Merge branch 'main' into sb/timer-api
simonjbeaumont Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feedback(attempted): Store a closure instead of UInt64
  • Loading branch information
simonjbeaumont committed Jul 8, 2024
commit b3e49036c6d5ef8f1f7dcbe0da49bce46c91e49f
22 changes: 14 additions & 8 deletions Sources/NIOCore/NIOTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 30,34 @@ public struct NIOTimer: Sendable {
/// A task created using `EventLoop.scheduleTask(deadline:_:)`, used by default for `EventLoop` implementations.
case scheduledTask(Scheduled<Void>)
/// An identifier for a timer, used by `EventLoop` implementations that conform to `CustomTimerImplementation`.
case custom(eventLoop: any NIOCustomTimerImplementation, id: UInt64)
case custom(eventLoop: any NIOCustomTimerImplementation, onCancel: @Sendable () -> Void)
}

@usableFromInline
var backing: Backing

@inlinable
init(_ backing: Backing) {
self.backing = backing
}

fileprivate init(_ scheduled: Scheduled<Void>) {
self.backing = .scheduledTask(scheduled)
}

@inlinable
init(_ eventLoop: any NIOCustomTimerImplementation, id: UInt64) {
self.backing = .custom(eventLoop: eventLoop, id: id)
}
// @inlinable
// init(_ eventLoop: any NIOCustomTimerImplementation, onCancel: @Sendable @escaping () -> Void) {
// self.backing = .custom(eventLoop: eventLoop, onCancel: onCancel)
// }

/// Cancel the timer associated with this handle.
@inlinable
public func cancel() {
simonjbeaumont marked this conversation as resolved.
Show resolved Hide resolved
switch self.backing {
case .scheduledTask(let scheduled):
scheduled.cancel()
case .custom(let eventLoop, let id):
eventLoop.cancelTimer(id)
case .custom(let eventLoop, let onCancel):
onCancel()
}
}
}
Expand Down Expand Up @@ -93,6 98,7 @@ public protocol NIOCustomTimerImplementation: EventLoop {
extension EventLoop where Self: NIOCustomTimerImplementation {
@inlinable
public func setTimer(for deadline: NIODeadline, _ handler: any NIOTimerHandler) -> NIOTimer {
NIOTimer(self, id: self.setTimer(for: deadline, handler))
let id = self.setTimer(for: deadline, handler)
return NIOTimer(.custom(eventLoop: self, onCancel: { self.cancelTimer(id) }))
}
}