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

Use "best for platform" EventLoopGroup instead of always using MultiThreadedEventLoopGroup #3172

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Expand Up @@ -16,7 +16,7 @@ let package = Package(
],
dependencies: [
// HTTP client library built on SwiftNIO
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.19.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.21.0"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is kind of unrelated to the goal of this PR but it should be fine considering the version comes with the HTTPClient.shared and is going to be needed by something if not Vapor.


// Sugary extensions for the SwiftNIO library
.package(url: "https://github.com/vapor/async-kit.git", from: "1.15.0"),
Expand Down
10 changes: 9 additions & 1 deletion Sources/Vapor/Application.swift
@@ -1,5 +1,6 @@
import ConsoleKit
import Logging
import AsyncHTTPClient
import NIOConcurrencyHelpers
import NIOCore
import NIOPosix
Expand Down Expand Up @@ -100,7 +101,7 @@ public final class Application: Sendable {
case createNew

public static var singleton: EventLoopGroupProvider {
.shared(MultiThreadedEventLoopGroup.singleton)
.shared(Application.defaultEventLoopGroup)
}
}

Expand All @@ -114,6 +115,13 @@ public final class Application: Sendable {
private let _lifecycle: NIOLockedValueBox<Lifecycle>
private let _locks: NIOLockedValueBox<Locks>

/// Returns the default `EventLoopGroup` singleton, automatically selecting the best for the platform.
///
/// This will select the concrete `EventLoopGroup` depending which platform this is running on.
public static var defaultEventLoopGroup: any EventLoopGroup {
HTTPClient.defaultEventLoopGroup
}

public init(
_ environment: Environment = .development,
_ eventLoopGroupProvider: EventLoopGroupProvider = .singleton
Expand Down
6 changes: 3 additions & 3 deletions Sources/Vapor/Concurrency/WebSocket+Concurrency.swift
Expand Up @@ -83,7 +83,7 @@ extension WebSocket {
to url: String,
headers: HTTPHeaders = [:],
configuration: WebSocketClient.Configuration = .init(),
on eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
on eventLoopGroup: EventLoopGroup = Application.defaultEventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) async throws {
guard let url = URL(string: url) else {
Expand All @@ -103,7 +103,7 @@ extension WebSocket {
to url: URL,
headers: HTTPHeaders = [:],
configuration: WebSocketClient.Configuration = .init(),
on eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
on eventLoopGroup: EventLoopGroup = Application.defaultEventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) async throws {
let scheme = url.scheme ?? "ws"
Expand All @@ -127,7 +127,7 @@ extension WebSocket {
path: String = "/",
headers: HTTPHeaders = [:],
configuration: WebSocketClient.Configuration = .init(),
on eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
on eventLoopGroup: EventLoopGroup = Application.defaultEventLoopGroup,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) async throws {
return try await WebSocketClient(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/HTTP/Server/HTTPServer.swift
Expand Up @@ -282,7 +282,7 @@ public final class HTTPServer: Server, Sendable {
application: Application,
responder: Responder,
configuration: Configuration,
on eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton
on eventLoopGroup: EventLoopGroup = Application.defaultEventLoopGroup
) {
self.application = application
self.responder = responder
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/View/PlaintextRenderer.swift
Expand Up @@ -12,7 +12,7 @@ public struct PlaintextRenderer: ViewRenderer, Sendable {
fileio: NonBlockingFileIO,
viewsDirectory: String,
logger: Logger,
eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton
eventLoopGroup: EventLoopGroup = Application.defaultEventLoopGroup
) {
self.fileio = fileio
self.viewsDirectory = viewsDirectory.finished(with: "/")
Expand Down
2 changes: 1 addition & 1 deletion Sources/XCTVapor/XCTApplication.swift
Expand Up @@ -48,7 +48,7 @@ extension Application {
try app.server.start(address: .hostname(self.hostname, port: self.port))
defer { app.server.shutdown() }

let client = HTTPClient(eventLoopGroup: MultiThreadedEventLoopGroup.singleton)
let client = HTTPClient(eventLoopGroup: Application.defaultEventLoopGroup)
defer { try! client.syncShutdown() }
var path = request.url.path
path = path.hasPrefix("/") ? path : "/\(path)"
Expand Down
2 changes: 1 addition & 1 deletion Tests/VaporTests/AsyncRequestTests.swift
Expand Up @@ -174,7 +174,7 @@ final class AsyncRequestTests: XCTestCase {
headers: [:],
body: .byteBuffer(tenMB))
let delegate = ResponseDelegate(bytesTheClientSent: bytesTheClientSent)
let httpClient = HTTPClient(eventLoopGroup: MultiThreadedEventLoopGroup.singleton)
let httpClient = HTTPClient(eventLoopGroup: Application.defaultEventLoopGroup)
XCTAssertThrowsError(try httpClient.execute(request: request,
delegate: delegate,
deadline: .now() + .milliseconds(500)).wait()) { error in
Expand Down