Skip to content

Sendable cleanup #271

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

Merged
merged 7 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.8
// swift-tools-version:5.9
import PackageDescription

let package = Package(
Expand Down Expand Up @@ -39,6 +39,9 @@ let package = Package(
)

var swiftSettings: [SwiftSetting] { [
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ConciseMagicFile"),
.enableUpcomingFeature("ForwardTrailingClosures"),
.enableUpcomingFeature("DisableOutwardActorInference"),
.enableExperimentalFeature("StrictConcurrency=complete"),
] }
47 changes: 0 additions & 47 deletions [email protected]

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<a href="LICENSE"><img src="https://design.vapor.codes/images/mitlicense.svg" alt="MIT License"></a>
<a href="https://github.com/vapor/postgres-kit/actions/workflows/test.yml"><img src="https://img.shields.io/github/actions/workflow/status/vapor/postgres-kit/test.yml?event=push&style=plastic&logo=github&label=tests&logoColor=%23ccc" alt="Continuous Integration"></a>
<a href="https://codecov.io/github/vapor/postgres-kit"><img src="https://img.shields.io/codecov/c/github/vapor/postgres-kit?style=plastic&logo=codecov&label=codecov"></a>
<a href="https://swift.org"><img src="https://design.vapor.codes/images/swift58up.svg" alt="Swift 5.8+"></a>
<a href="https://swift.org"><img src="https://design.vapor.codes/images/swift59up.svg" alt="Swift 5.9+"></a>
</p>

<br>
Expand Down
8 changes: 4 additions & 4 deletions Sources/PostgresKit/ConnectionPool+Postgres.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import NIOCore
import PostgresNIO
@preconcurrency import AsyncKit
import Logging
import NIOCore
import PostgresNIO

extension EventLoopGroupConnectionPool where Source == PostgresConnectionSource {
public func database(logger: Logger) -> any PostgresDatabase {
Expand Down Expand Up @@ -39,11 +39,11 @@ private struct _EventLoopConnectionPoolPostgresDatabase: PostgresDatabase {
var eventLoop: any EventLoop {
self.pool.eventLoop
}

func send(_ request: any PostgresRequest, logger: Logger) -> EventLoopFuture<Void> {
self.pool.withConnection(logger: logger) { $0.send(request, logger: logger) }
}

func withConnection<T>(_ closure: @escaping (PostgresConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
self.pool.withConnection(logger: self.logger, closure)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NIOSSL
import Foundation
import NIOCore
import NIOSSL

@available(*, deprecated, message: "Use `SQLPostgresConfiguration` instead.")
public struct PostgresConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import NIOSSL
import Atomics
import Logging
import PostgresNIO
import NIOCore
import NIOSSL
import PostgresNIO

extension PostgresConnectionSource {
@available(*, deprecated, message: "Use `sqlConfiguration` instead.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PostgresNIO
import Foundation
import PostgresNIO
import SQLKit

@available(*, deprecated, message: "Use `.sql(jsonEncoder:jsonDecoder:)` instead.")
Expand Down
3 changes: 3 additions & 0 deletions Sources/PostgresKit/Docs.docc/theme-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"psqlkit": "#336791",
"documentation-intro-fill": "radial-gradient(circle at top, var(--color-psqlkit) 30%, #000 100%)",
"documentation-intro-accent": "var(--color-psqlkit)",
"documentation-intro-eyebrow": "white",
"documentation-intro-figure": "white",
"documentation-intro-title": "white",
"logo-base": { "dark": "#fff", "light": "#000" },
"logo-shape": { "dark": "#000", "light": "#fff" },
"fill": { "dark": "#000", "light": "#fff" }
Expand Down
19 changes: 11 additions & 8 deletions Sources/PostgresKit/PostgresConnectionSource.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import NIOSSL
import NIOConcurrencyHelpers
import AsyncKit
import Logging
import NIOConcurrencyHelpers
import NIOCore
import NIOSSL
import PostgresNIO
import SQLKit
import NIOCore

public struct PostgresConnectionSource: ConnectionPoolSource {
public let sqlConfiguration: SQLPostgresConfiguration

private static let idGenerator = NIOLockedValueBox<Int>(0)

public init(sqlConfiguration: SQLPostgresConfiguration) {
self.sqlConfiguration = sqlConfiguration
}
Expand All @@ -22,10 +22,13 @@ public struct PostgresConnectionSource: ConnectionPoolSource {
let connectionFuture = PostgresConnection.connect(
on: eventLoop,
configuration: self.sqlConfiguration.coreConfiguration,
id: Self.idGenerator.withLockedValue { $0 += 1; return $0 },
id: Self.idGenerator.withLockedValue {
$0 += 1
return $0
},
logger: logger
)

if let searchPath = self.sqlConfiguration.searchPath {
return connectionFuture.flatMap { conn in
conn.sql(queryLogLevel: nil)
Expand All @@ -39,4 +42,4 @@ public struct PostgresConnectionSource: ConnectionPoolSource {
}
}

extension PostgresConnection: ConnectionPoolItem {}
extension PostgresNIO.PostgresConnection: AsyncKit.ConnectionPoolItem {}
4 changes: 2 additions & 2 deletions Sources/PostgresKit/PostgresDataTranslation.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PostgresNIO
import Foundation
import PostgresNIO

/// Quick and dirty ``CodingKey``, borrowed from FluentKit. If ``CodingKeyRepresentable`` wasn't broken by design
/// (specifically, it can't be back-deployed before macOS 12.3 etc., even though it was introduced in Swift 5.6),
Expand All @@ -17,7 +17,7 @@ private extension PostgresCell {
}

/// Sidestep problems with URL coding behavior by making it conform directly to Postgres coding.
extension URL: PostgresNonThrowingEncodable, PostgresDecodable {
extension Foundation.URL: PostgresNIO.PostgresNonThrowingEncodable, PostgresNIO.PostgresDecodable {
public static var psqlType: PostgresDataType {
String.psqlType
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgresKit/PostgresDatabase+SQL.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PostgresNIO
import Logging
import PostgresNIO
import SQLKit

extension PostgresDatabase {
Expand Down Expand Up @@ -95,7 +95,7 @@ extension PostgresSQLDatabase: SQLDatabase, PostgresDatabase {
self.database.withConnection(closure)
}

func withSession<R>(_ closure: @escaping @Sendable (any SQLDatabase) async throws -> R) async throws -> R {
func withSession<R: Sendable>(_ closure: @escaping @Sendable (any SQLDatabase) async throws -> R) async throws -> R {
try await self.withConnection { c in
c.eventLoop.makeFutureWithTask {
try await closure(c.sql(
Expand Down
6 changes: 3 additions & 3 deletions Sources/PostgresKit/PostgresRow+SQL.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import PostgresNIO
import Foundation
import PostgresNIO
import SQLKit

extension PostgresRow {
@inlinable
public func sql() -> some SQLRow {
self.sql(decodingContext: .default)
}

public func sql(decodingContext: PostgresDecodingContext<some PostgresJSONDecoder>) -> some SQLRow {
_PostgresSQLRow(randomAccessView: self.makeRandomAccess(), decodingContext: decodingContext)
}
Expand Down Expand Up @@ -39,7 +39,7 @@ extension _PostgresSQLRow: SQLRow {
guard self.randomAccessView.contains(column) else {
throw _Error.missingColumn(column)
}

return try PostgresDataTranslation.decode(T.self, from: self.randomAccessView[column], in: self.decodingContext)
}
}
4 changes: 2 additions & 2 deletions Sources/PostgresKit/SQLPostgresConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import NIOSSL
import Foundation
import NIOCore
import NIOSSL
import PostgresNIO

/// Provides configuration paramters for establishing PostgreSQL database connections.
public struct SQLPostgresConfiguration {
public struct SQLPostgresConfiguration: Sendable {
/// IANA-assigned port number for PostgreSQL
/// `UInt16(getservbyname("postgresql", "tcp").pointee.s_port).byteSwapped`
public static var ianaPortNumber: Int { 5432 }
Expand Down
28 changes: 17 additions & 11 deletions Tests/PostgresKitTests/PostgresKitTests.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
@testable import PostgresKit
import SQLKitBenchmark
import XCTest
import Foundation
import Logging
import PostgresNIO
import NIOCore
import Foundation
import PostgresNIO
import SQLKitBenchmark
import XCTest
@testable import PostgresKit

final class PostgresKitTests: XCTestCase {
func testSQLKitBenchmark() throws {
let conn = try PostgresConnection.test(on: self.eventLoop).wait()
defer { try? conn.close().wait() }
let benchmark = SQLBenchmarker(on: conn.sql())
func testSQLKitBenchmark() async throws {
let conn = try await PostgresConnection.test(on: self.eventLoop).get()
do {
try benchmark.run()
let benchmark = SQLBenchmarker(on: conn.sql())

try await benchmark.runAllTests()
} catch {
try? await conn.close()
XCTFail("Caught error: \(String(reflecting: error))")
throw error
}
try await conn.close()
}


// Disable for now, test is of questionable utility
/*
func testPerformance() throws {
let db = PostgresConnectionSource(sqlConfiguration: .test)
let pool = EventLoopGroupConnectionPool(
Expand All @@ -42,6 +47,7 @@ final class PostgresKitTests: XCTestCase {
}
}
}
*/

func testLeak() throws {
struct Foo: Codable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testable import PostgresKit
import PostgresKit
import XCTest

final class SQLPostgresConfigurationTests: XCTestCase {
Expand Down
11 changes: 7 additions & 4 deletions Tests/PostgresKitTests/Utilities.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import XCTest
import PostgresKit
import Logging
import Foundation
import Logging
import NIOCore
import PostgresKit
import PostgresNIO
import XCTest

extension PostgresConnection {
static func test(on eventLoop: any EventLoop) -> EventLoopFuture<PostgresConnection> {
PostgresConnectionSource(sqlConfiguration: .test).makeConnection(logger: .init(label: "vapor.codes.postgres-kit.test"), on: eventLoop)
PostgresConnectionSource(sqlConfiguration: .test).makeConnection(
logger: .init(label: "vapor.codes.postgres-kit.test"),
on: eventLoop
)
}
}

Expand Down
Loading