-
Notifications
You must be signed in to change notification settings - Fork 656
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 async version of NIOThreadPool.runIfActive #2566
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, a couple of quick notes here.
Sources/NIOPosix/NIOThreadPool.swift
Outdated
@@ -292,6 +292,48 @@ extension NIOThreadPool { | |||
} | |||
} | |||
|
|||
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | |||
extension NIOThreadPool { | |||
#if swift(>=5.7) |
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 no longer support older versions of Swift than 5.7, so we can drop this conditional compilation step.
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.
ok, I've removed the older version
3bdc6c4
to
3f947e4
Compare
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.
Generally looking good, a few quick notes on the API.
Sources/NIOPosix/NIOThreadPool.swift
Outdated
try await self._runIfActive(body) | ||
} | ||
|
||
private func _runIfActive<T>(_ body: @escaping () throws -> T) async throws -> 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.
I don't think we need this inner function now, and so we can avoid dropping the @Sendable
annotation 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.
done
Sources/NIOPosix/NIOThreadPool.swift
Outdated
/// - parameters: | ||
/// - body: The closure which performs some blocking work to be done on the thread pool. | ||
/// - returns: result of the passed closure. | ||
@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 drop preconcurrency
, this method doesn't predate concurrency.
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.
done
Sources/NIOPosix/NIOThreadPool.swift
Outdated
/// - body: The closure which performs some blocking work to be done on the thread pool. | ||
/// - returns: result of the passed closure. | ||
@preconcurrency | ||
public func runIfActive<T>(_ body: @escaping @Sendable () throws -> T) async throws -> 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.
T: Sendable
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.
done.
Remove no longer supported code Add tests for errors being thrown, and the thread pool not being active
89171e3
to
d0bf0d1
Compare
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, thanks!
Add async version of NIOThreadPool.runIfActive
Motivation:
Currently when using NIOThreadPool in a Swift concurrency context you still need an EventLoop and the call will make two hops, one to the EventLoop and then one back to your Task.
Modifications:
Added
NIOThreadPool.runIfActive(_) async throws
Result:
You don't need an EventLoop to use
NIOThreadPool
This is the first stage of completing #2565