Skip to content

Commit

Permalink
Use singleton EventLoopGroup (#3128)
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jan 7, 2024
1 parent 58969a7 commit 5f7c5a3
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Expand Up @@ -28,7 +28,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "4.0.0"),

// 🚍 High-performance trie-node router.
.package(url: "https://github.com/vapor/routing-kit.git", from: "4.5.0"),
.package(url: "https://github.com/vapor/routing-kit.git", from: "4.8.2"),

// 💥 Backtraces for Swift on Linux
.package(url: "https://github.com/swift-server/swift-backtrace.git", from: "1.1.1"),
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.9.swift
Expand Up @@ -28,7 +28,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "4.0.0"),

// 🚍 High-performance trie-node router.
.package(url: "https://github.com/vapor/routing-kit.git", from: "4.5.0"),
.package(url: "https://github.com/vapor/routing-kit.git", from: "4.8.2"),

// Event-driven network application framework for high performance protocol servers & clients, non-blocking.
.package(url: "https://github.com/apple/swift-nio.git", from: "2.62.0"),
Expand Down
7 changes: 6 additions & 1 deletion Sources/Vapor/Application.swift
Expand Up @@ -96,7 +96,12 @@ public final class Application: Sendable {

public enum EventLoopGroupProvider: Sendable {
case shared(EventLoopGroup)
@available(*, deprecated, renamed: "singleton", message: "Use '.singleton' for a shared 'EventLoopGroup', for better performance")
case createNew

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

public let eventLoopGroupProvider: EventLoopGroupProvider
Expand All @@ -111,7 +116,7 @@ public final class Application: Sendable {

public init(
_ environment: Environment = .development,
_ eventLoopGroupProvider: EventLoopGroupProvider = .createNew
_ eventLoopGroupProvider: EventLoopGroupProvider = .singleton
) {
#if swift(<5.9)
Backtrace.install()
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,
on eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
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,
on eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
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,
on eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
onUpgrade: @Sendable @escaping (WebSocket) -> ()
) async throws {
return try await WebSocketClient(
Expand Down
4 changes: 2 additions & 2 deletions Sources/Vapor/Deprecations/DotEnvFile+load.swift
Expand Up @@ -18,7 +18,7 @@ extension DotEnvFile {
@available(*, deprecated, message: "use `load(for:on:fileio:logger)`")
public static func load(
for environment: Environment = .development,
on eventLoopGroupProvider: Application.EventLoopGroupProvider = .createNew,
on eventLoopGroupProvider: Application.EventLoopGroupProvider = .singleton,
logger: Logger = Logger(label: "dot-env-loggger")
) {
let threadPool = NIOThreadPool(numberOfThreads: 1)
Expand Down Expand Up @@ -50,7 +50,7 @@ extension DotEnvFile {
@available(*, deprecated, message: "use `load(path:on:fileio:logger)`")
public static func load(
path: String,
on eventLoopGroupProvider: Application.EventLoopGroupProvider = .createNew,
on eventLoopGroupProvider: Application.EventLoopGroupProvider = .singleton,
logger: Logger = Logger(label: "dot-env-loggger")
) {
let threadPool = NIOThreadPool(numberOfThreads: 1)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/HTTP/Server/HTTPServer.swift
Expand Up @@ -261,7 +261,7 @@ public final class HTTPServer: Server, Sendable {
application: Application,
responder: Responder,
configuration: Configuration,
on eventLoopGroup: EventLoopGroup
on eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton
) {
self.application = application
self.responder = responder
Expand Down
4 changes: 2 additions & 2 deletions Sources/Vapor/Utilities/DotEnv.swift
Expand Up @@ -44,7 +44,7 @@ public struct DotEnvFile: Sendable {
/// - logger: Optionally provide an existing logger.
public static func load(
for environment: Environment = .development,
on eventLoopGroupProvider: Application.EventLoopGroupProvider = .createNew,
on eventLoopGroupProvider: Application.EventLoopGroupProvider = .singleton,
fileio: NonBlockingFileIO,
logger: Logger = Logger(label: "dot-env-loggger")
) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public struct DotEnvFile: Sendable {
/// - logger: Optionally provide an existing logger.
public static func load(
path: String,
on eventLoopGroupProvider: Application.EventLoopGroupProvider = .createNew,
on eventLoopGroupProvider: Application.EventLoopGroupProvider = .singleton,
fileio: NonBlockingFileIO,
logger: Logger = Logger(label: "dot-env-loggger")
) {
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
eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton
) {
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: NIOSingletons.posixEventLoopGroup)
let client = HTTPClient(eventLoopGroup: MultiThreadedEventLoopGroup.singleton)
defer { try! client.syncShutdown() }
var path = request.url.path
path = path.hasPrefix("/") ? path : "/\(path)"
Expand Down
7 changes: 2 additions & 5 deletions Tests/AsyncTests/AsyncRequestTests.swift
Expand Up @@ -19,16 +19,13 @@ fileprivate extension String {
final class AsyncRequestTests: XCTestCase {

var app: Application!
var eventLoopGroup: EventLoopGroup!

override func setUp() async throws {
eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4)
app = Application(.testing, .shared(eventLoopGroup))
app = Application(.testing)
}

override func tearDown() async throws {
app.shutdown()
try await eventLoopGroup.shutdownGracefully()
}

func testStreamingRequest() async throws {
Expand Down Expand Up @@ -177,7 +174,7 @@ final class AsyncRequestTests: XCTestCase {
headers: [:],
body: .byteBuffer(tenMB))
let delegate = ResponseDelegate(bytesTheClientSent: bytesTheClientSent)
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))
let httpClient = HTTPClient(eventLoopGroup: MultiThreadedEventLoopGroup.singleton)
XCTAssertThrowsError(try httpClient.execute(request: request,
delegate: delegate,
deadline: .now() + .milliseconds(500)).wait()) { error in
Expand Down
6 changes: 3 additions & 3 deletions Tests/VaporTests/ServerTests.swift
Expand Up @@ -507,9 +507,9 @@ final class ServerTests: XCTestCase {
})
}

@available(*, deprecated, message: "To avoid deprecation warnings")
func testEchoServer() throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let app = Application(.testing, .shared(eventLoopGroup))
let app = Application(.testing, .createNew)
defer { app.shutdown() }

final class Context: Sendable {
Expand Down Expand Up @@ -556,7 +556,7 @@ final class ServerTests: XCTestCase {
body: .stream(length: nil, { stream in
// We set the application to have a single event loop so we can use the same
// event loop here
let streamBox = NIOLoopBound(stream, eventLoop: eventLoopGroup.any())
let streamBox = NIOLoopBound(stream, eventLoop: app.eventLoopGroup.any())
return stream.write(.byteBuffer(.init(string: "foo"))).flatMap {
streamBox.value.write(.byteBuffer(.init(string: "bar")))
}.flatMap {
Expand Down

0 comments on commit 5f7c5a3

Please sign in to comment.