Skip to content

Commit 41bf65d

Browse files
committed
Some code cleanup, usage counter in info messages
1 parent e4958d5 commit 41bf65d

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

Sources/PostgresConnectionPool/PoolConnection.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ final class PoolConnection: Identifiable, Equatable {
1616

1717
private static var connectionId: Int = 0
1818

19-
let id: Int
19+
private(set) var usageCounter = 0
2020

21+
let id: Int
2122
var connection: PostgresConnection?
22-
var state: State = .connecting
23+
var state: State = .connecting {
24+
didSet {
25+
if case .active = state { usageCounter += 1 }
26+
}
27+
}
2328

2429
init() {
2530
self.id = PoolConnection.connectionId

Sources/PostgresConnectionPool/PostgresConnectionPool.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,15 @@ public actor PostgresConnectionPool {
5252
self.onReturnConnection = configuration.onReturnConnection
5353
self.onCloseConnection = configuration.onCloseConnection
5454

55-
var postgresConnection = PostgresConnection.Configuration.Connection(
55+
var postgresConfiguration = PostgresConnection.Configuration(
5656
host: configuration.connection.host,
57-
port: configuration.connection.port)
58-
postgresConnection.connectTimeout = .seconds(Int64(configuration.connectTimeout))
59-
60-
self.postgresConfiguration = PostgresConnection.Configuration(
61-
connection: postgresConnection,
62-
authentication: .init(
63-
username: configuration.connection.username,
64-
database: configuration.connection.database,
65-
password: configuration.connection.password),
57+
port: configuration.connection.port,
58+
username: configuration.connection.username,
59+
password: configuration.connection.password,
60+
database: configuration.connection.database,
6661
tls: .disable)
62+
postgresConfiguration.options.connectTimeout = .seconds(Int64(configuration.connectTimeout))
63+
self.postgresConfiguration = postgresConfiguration
6764
}
6865

6966
deinit {
@@ -124,8 +121,12 @@ public actor PostgresConnectionPool {
124121
}
125122

126123
func releaseConnection(_ connection: PoolConnection) async {
127-
connection.state = .available
128-
available.append(connection)
124+
// It can happen that the connection is returned before it's being used
125+
// (e.g. on cancellation)
126+
if connection.state != .available {
127+
connection.state = .available
128+
available.append(connection)
129+
}
129130

130131
Task.detached { [weak self] in
131132
await self?.handleNextContinuation()
@@ -169,7 +170,7 @@ public actor PostgresConnectionPool {
169170
}
170171

171172
// Shut down the event loop.
172-
try? eventLoopGroup.syncShutdownGracefully()
173+
try? await eventLoopGroup.shutdownGracefully()
173174
}
174175

175176
// MARK: - Private
@@ -191,7 +192,8 @@ public actor PostgresConnectionPool {
191192

192193
// TODO: Kill self if too many stuck connections
193194

194-
logger.debug("[\(poolName)] Check connections: \(continuations.count) continuations left, \(connections.count) connections, \(available.count) available")
195+
let usageCounter = connections.reduce(0) { $0 + $1.usageCounter }
196+
logger.info("[\(poolName)] \(connections.count) connections (\(available.count) available, \(usageCounter) queries), \(continuations.count) continuations left")
195197

196198
// Check for waiting continuations and open a new connection if possible
197199
if connections.count < poolSize,

0 commit comments

Comments
 (0)