Skip to content

Commit

Permalink
Further tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasa committed Oct 17, 2024
1 parent bfe84d2 commit 75ad4d7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Sources/NIOCore/EventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,22 @@ public protocol EventLoop: EventLoopGroup {
///
/// - NOTE: Event loops that provide a custom scheduled callback implementation **must** also implement
/// `cancelScheduledCallback`. Failure to do so will result in a runtime error.
@preconcurrency
@discardableResult
func scheduleCallback(
at deadline: NIODeadline,
handler: some NIOScheduledCallbackHandler
handler: some (NIOScheduledCallbackHandler & Sendable)
) throws -> NIOScheduledCallback

/// Schedule a callback after given time.
///
/// - NOTE: Event loops that provide a custom scheduled callback implementation **must** also implement
/// `cancelScheduledCallback`. Failure to do so will result in a runtime error.
@preconcurrency
@discardableResult
func scheduleCallback(
in amount: TimeAmount,
handler: some NIOScheduledCallbackHandler
handler: some (NIOScheduledCallbackHandler & Sendable)
) throws -> NIOScheduledCallback

/// Cancel a scheduled callback.
Expand Down
12 changes: 10 additions & 2 deletions Sources/NIOCore/EventLoopFuture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1457,8 +1457,12 @@ extension EventLoopFuture {
// value across event loops.
future.whenComplete { result in
let voidResult = result.map { _ in }
future.eventLoop.execute {
if eventLoop.inEventLoop {
processResult(index, voidResult)
} else {
eventLoop.execute {
processResult(index, voidResult)
}
}
}
}
Expand Down Expand Up @@ -1684,8 +1688,12 @@ extension EventLoopFuture {
// value across event loops.
future.whenComplete { result in
let voidResult = result.map { _ in }
future.eventLoop.execute {
if eventLoop.inEventLoop {
processResult(index, voidResult)
} else {
eventLoop.execute {
processResult(index, voidResult)
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/NIOCore/NIOScheduledCallback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ extension EventLoop {
}

/// Default implementation of `scheduleCallback(in amount:handler:)`: calls `scheduleCallback(at deadline:handler:)`.
@preconcurrency
@discardableResult
@inlinable
public func scheduleCallback(
in amount: TimeAmount,
handler: some NIOScheduledCallbackHandler
handler: some (NIOScheduledCallbackHandler & Sendable)
) throws -> NIOScheduledCallback {
try self.scheduleCallback(at: .now() + amount, handler: handler)
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/NIOEmbedded/AsyncTestingEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
self.scheduleTask(deadline: self.now + `in`, task)
}

@preconcurrency
public func scheduleCallback(
at deadline: NIODeadline,
handler: some NIOScheduledCallbackHandler
handler: some (NIOScheduledCallbackHandler & Sendable)
) throws -> NIOScheduledCallback {
/// The default implementation of `scheduledCallback(at:handler)` makes two calls to the event loop because it
/// needs to hook the future of the backing scheduled task, which can lead to lost cancellation callbacks when
Expand All @@ -213,10 +214,11 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
}
}

@preconcurrency
@discardableResult
public func scheduleCallback(
in amount: TimeAmount,
handler: some NIOScheduledCallbackHandler
handler: some (NIOScheduledCallbackHandler & Sendable)
) throws -> NIOScheduledCallback {
/// Even though this type does not implement a custom `scheduleCallback(at:handler)`, it uses a manual clock so
/// it cannot rely on the default implementation of `scheduleCallback(in:handler:)`, which computes the deadline
Expand Down
3 changes: 2 additions & 1 deletion Sources/NIOEmbedded/Embedded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ public final class EmbeddedEventLoop: EventLoop, CustomStringConvertible {
scheduleTask(deadline: self._now + `in`, task)
}

@preconcurrency
@discardableResult
public func scheduleCallback(
in amount: TimeAmount,
handler: some NIOScheduledCallbackHandler
handler: some (NIOScheduledCallbackHandler & Sendable)
) -> NIOScheduledCallback {
/// Even though this type does not implement a custom `scheduleCallback(at:handler)`, it uses a manual clock so
/// it cannot rely on the default implementation of `scheduleCallback(in:handler:)`, which computes the deadline
Expand Down

0 comments on commit 75ad4d7

Please sign in to comment.