diff --git a/Package.swift b/Package.swift index c1cb4bda..a45925ed 100644 --- a/Package.swift +++ b/Package.swift @@ -14,8 +14,8 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-atomics.git", from: "1.1.0"), - .package(url: "https://github.com/apple/swift-nio.git", from: "2.52.0"), - .package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.16.0"), + .package(url: "https://github.com/apple/swift-nio.git", from: "2.58.0"), + .package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.18.0"), .package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.23.1"), .package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"), .package(url: "https://github.com/apple/swift-metrics.git", from: "2.0.0"), diff --git a/README.md b/README.md index 51e0b8c5..441a41e3 100644 --- a/README.md +++ b/README.md @@ -67,19 +67,7 @@ let config = PostgresConnection.Configuration( ) ``` -A connection must be created on a SwiftNIO `EventLoop`. In most server use cases, an -`EventLoopGroup` is created at app startup and closed during app shutdown. - -```swift -import NIOPosix - -let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - -// Much later -try await eventLoopGroup.shutdownGracefully() -``` - -A [`Logger`] is also required. +To create a connection we need a [`Logger`], that is used to log connection background events. ```swift import Logging @@ -91,10 +79,8 @@ Now we can put it together: ```swift import PostgresNIO -import NIOPosix import Logging -let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) let logger = Logger(label: "postgres-logger") let config = PostgresConnection.Configuration( @@ -107,7 +93,6 @@ let config = PostgresConnection.Configuration( ) let connection = try await PostgresConnection.connect( - on: eventLoopGroup.next(), configuration: config, id: 1, logger: logger @@ -115,9 +100,6 @@ let connection = try await PostgresConnection.connect( // Close your connection once done try await connection.close() - -// Shutdown the EventLoopGroup, once all connections are closed. -try await eventLoopGroup.shutdownGracefully() ``` #### Querying diff --git a/Sources/PostgresNIO/Connection/PostgresConnection.swift b/Sources/PostgresNIO/Connection/PostgresConnection.swift index 6f849bdd..f8a9709e 100644 --- a/Sources/PostgresNIO/Connection/PostgresConnection.swift +++ b/Sources/PostgresNIO/Connection/PostgresConnection.swift @@ -360,13 +360,13 @@ extension PostgresConnection { /// Creates a new connection to a Postgres server. /// /// - Parameters: - /// - eventLoop: The `EventLoop` the request shall be created on + /// - eventLoop: The `EventLoop` the connection shall be created on. /// - configuration: A ``Configuration`` that shall be used for the connection /// - connectionID: An `Int` id, used for metadata logging /// - logger: A logger to log background events into /// - Returns: An established ``PostgresConnection`` asynchronously that can be used to run queries. public static func connect( - on eventLoop: EventLoop, + on eventLoop: EventLoop = PostgresConnection.defaultEventLoopGroup.any(), configuration: PostgresConnection.Configuration, id connectionID: ID, logger: Logger @@ -661,3 +661,20 @@ extension EventLoopFuture { } } } + +extension PostgresConnection { + /// 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: EventLoopGroup { +#if canImport(Network) + if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) { + return NIOTSEventLoopGroup.singleton + } else { + return MultiThreadedEventLoopGroup.singleton + } +#else + return MultiThreadedEventLoopGroup.singleton +#endif + } +}