Skip to content

Commit f2a6394

Browse files
authored
Support additionalStartupParameters in PostgresClient (#521)
1 parent d4c2f38 commit f2a6394

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Sources/PostgresNIO/Pool/ConnectionFactory.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ final class ConnectionFactory: Sendable {
8989
connectionConfig.options.connectTimeout = TimeAmount(config.options.connectTimeout)
9090
connectionConfig.options.tlsServerName = config.options.tlsServerName
9191
connectionConfig.options.requireBackendKeyData = config.options.requireBackendKeyData
92+
connectionConfig.options.additionalStartupParameters = config.options.additionalStartupParameters
9293

9394
return connectionConfig
9495
}

Sources/PostgresNIO/Pool/PostgresClient.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
106106
/// If you are not using Amazon RDS Proxy, you should leave this set to `true` (the default).
107107
public var requireBackendKeyData: Bool = true
108108

109+
/// Additional parameters to send to the server on startup. The name value pairs are added to the initial
110+
/// startup message that the client sends to the server.
111+
public var additionalStartupParameters: [(String, String)] = []
112+
109113
/// The minimum number of connections that the client shall keep open at any time, even if there is no
110114
/// demand. Default to `0`.
111115
///

Tests/IntegrationTests/PostgresClientTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ final class PostgresClientTests: XCTestCase {
4343
}
4444
}
4545

46+
func testApplicationNameIsForwardedCorrectly() async throws {
47+
var mlogger = Logger(label: "test")
48+
mlogger.logLevel = .debug
49+
let logger = mlogger
50+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 8)
51+
self.addTeardownBlock {
52+
try await eventLoopGroup.shutdownGracefully()
53+
}
54+
55+
var clientConfig = PostgresClient.Configuration.makeTestConfiguration()
56+
let applicationName = "postgres_nio_test_run"
57+
clientConfig.options.additionalStartupParameters = [("application_name", applicationName)]
58+
let client = PostgresClient(configuration: clientConfig, eventLoopGroup: eventLoopGroup, backgroundLogger: logger)
59+
60+
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
61+
taskGroup.addTask {
62+
await client.run()
63+
}
64+
65+
let rows = try await client.query("select * from pg_stat_activity;");
66+
var applicationNameFound = 0
67+
for try await row in rows {
68+
let randomAccessRow = row.makeRandomAccess()
69+
if try randomAccessRow["application_name"].decode(String?.self) == applicationName {
70+
applicationNameFound += 1
71+
}
72+
}
73+
74+
XCTAssertGreaterThanOrEqual(applicationNameFound, 1)
75+
76+
taskGroup.cancelAll()
77+
}
78+
}
79+
80+
4681
func testQueryDirectly() async throws {
4782
var mlogger = Logger(label: "test")
4883
mlogger.logLevel = .debug

0 commit comments

Comments
 (0)