Skip to content

Commit e4e086a

Browse files
committed
Small cleanup, added comments and some TODOs
1 parent b358cac commit e4e086a

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This package requires Swift 5.7 or higher (at least Xcode 13), and compiles on m
1313

1414
```swift
1515
dependencies: [
16-
.package(url: "https://github.com/Outdooractive/PostgresConnectionPool.git", from: "0.5.1"),
16+
.package(url: "https://github.com/Outdooractive/PostgresConnectionPool.git", from: "0.5.4"),
1717
],
1818
targets: [
1919
.target(name: "MyTarget", dependencies: [
@@ -53,10 +53,10 @@ try await pool.connection(callback: { connection in
5353
try await connection.query(PostgresQuery(stringLiteral: "SELECT 1"), logger: logger)
5454
})
5555

56-
// Generic object loading
57-
func fetchObjects<T: Decodable>(_ sql: String) async throws -> [T] {
56+
// With PostgresKit
57+
func fetchObjects<T: Decodable>(_ sql: SQLQueryString) async throws -> [T] {
5858
try await pool.connection({ connection in
59-
return try await connection.sql().raw(SQLQueryString(stringLiteral: sql)).all(decoding: T.self)
59+
return try await connection.sql().raw(sql).all(decoding: T.self)
6060
})
6161
}
6262

Sources/PostgresConnectionPool/PoolConnection.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ import PostgresNIO
77

88
final class PoolConnection: Identifiable, Equatable {
99

10-
private static var connectionId: Int = 0
10+
// TODO: Serialize access
11+
private(set) static var connectionId = 0
12+
private(set) static var globalUsageCounter = 0
1113

1214
private(set) var usageCounter = 0
1315

1416
let id: Int
1517
var connection: PostgresConnection?
1618
var state: PoolConnectionState = .connecting {
1719
didSet {
18-
if case .active = state { usageCounter += 1 }
20+
if case .active = state {
21+
usageCounter += 1
22+
PoolConnection.globalUsageCounter += 1
23+
}
1924
}
2025
}
2126

Sources/PostgresConnectionPool/PoolConnectionState.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
import Foundation
66

77
public enum PoolConnectionState: Equatable {
8+
9+
/// The connection is in use.
810
case active(Date)
11+
/// The connection is open and available.
912
case available
13+
/// The connection is closed and can't be used.
1014
case closed
15+
/// The connection is currently being established.
1116
case connecting
17+
1218
}

Sources/PostgresConnectionPool/PoolInfo.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ public struct PoolInfo {
99

1010
/// Information about an open connection.
1111
public struct ConnectionInfo {
12-
public var id: Int
13-
public var name: String
14-
public var usageCounter: Int
15-
public var query: String?
16-
public var queryRuntime: TimeInterval?
17-
public var state: PoolConnectionState
12+
public let id: Int
13+
public let name: String
14+
public let usageCounter: Int
15+
public let query: String?
16+
public let queryRuntime: TimeInterval?
17+
public let state: PoolConnectionState
1818
}
1919

20-
public var name: String
21-
public var openConnections: Int
22-
public var activeConnections: Int
23-
public var availableConnections: Int
20+
public let name: String
21+
public let openConnections: Int
22+
public let activeConnections: Int
23+
public let availableConnections: Int
24+
public let usageCounter: Int
2425

25-
public var connections: [ConnectionInfo]
26+
public let connections: [ConnectionInfo]
2627

2728
}

Sources/PostgresConnectionPool/PostgresConnectionPool.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public actor PostgresConnectionPool {
7676
@discardableResult
7777
public func connection<T>(
7878
_ callback: (PostgresConnectionWrapper) async throws -> T)
79-
async throws
80-
-> T
79+
async throws -> T
8180
{
8281
var poolConnection: PoolConnection?
8382

@@ -189,6 +188,7 @@ public actor PostgresConnectionPool {
189188
openConnections: connections.count,
190189
activeConnections: connections.count - available.count,
191190
availableConnections: available.count,
191+
usageCounter: PoolConnection.globalUsageCounter,
192192
connections: connections)
193193
}
194194

@@ -426,6 +426,8 @@ public actor PostgresConnectionPool {
426426
logger.debug("[\(poolName)] Connection \(poolConnection.id) failed after \(connectionRuntime.rounded(toPlaces: 2))s: \(error)")
427427
poolConnection.state = .closed
428428

429+
// TODO: Don't just open a new connection, check first if this is a permanent error like wrong password etc.
430+
429431
Task.detached { [weak self] in
430432
await self?.openConnection()
431433
}

Sources/PostgresConnectionPool/PostgresConnectionWrapper.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public final class PostgresConnectionWrapper {
8989
decoder: PostgresDataDecoder = PostgresDataDecoder())
9090
-> SQLDatabase
9191
{
92+
// TODO: Track the current query
9293
postgresConnection.sql(encoder: encoder, decoder: decoder)
9394
}
9495

0 commit comments

Comments
 (0)