Skip to content

Commit

Permalink
Support additionalStartupParameters in PostgresClient (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfett authored Oct 21, 2024
1 parent d4c2f38 commit f2a6394
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/PostgresNIO/Pool/ConnectionFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ final class ConnectionFactory: Sendable {
connectionConfig.options.connectTimeout = TimeAmount(config.options.connectTimeout)
connectionConfig.options.tlsServerName = config.options.tlsServerName
connectionConfig.options.requireBackendKeyData = config.options.requireBackendKeyData
connectionConfig.options.additionalStartupParameters = config.options.additionalStartupParameters

return connectionConfig
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/PostgresNIO/Pool/PostgresClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public final class PostgresClient: Sendable, ServiceLifecycle.Service {
/// If you are not using Amazon RDS Proxy, you should leave this set to `true` (the default).
public var requireBackendKeyData: Bool = true

/// Additional parameters to send to the server on startup. The name value pairs are added to the initial
/// startup message that the client sends to the server.
public var additionalStartupParameters: [(String, String)] = []

/// The minimum number of connections that the client shall keep open at any time, even if there is no
/// demand. Default to `0`.
///
Expand Down
35 changes: 35 additions & 0 deletions Tests/IntegrationTests/PostgresClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,41 @@ final class PostgresClientTests: XCTestCase {
}
}

func testApplicationNameIsForwardedCorrectly() async throws {
var mlogger = Logger(label: "test")
mlogger.logLevel = .debug
let logger = mlogger
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 8)
self.addTeardownBlock {
try await eventLoopGroup.shutdownGracefully()
}

var clientConfig = PostgresClient.Configuration.makeTestConfiguration()
let applicationName = "postgres_nio_test_run"
clientConfig.options.additionalStartupParameters = [("application_name", applicationName)]
let client = PostgresClient(configuration: clientConfig, eventLoopGroup: eventLoopGroup, backgroundLogger: logger)

try await withThrowingTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
await client.run()
}

let rows = try await client.query("select * from pg_stat_activity;");
var applicationNameFound = 0
for try await row in rows {
let randomAccessRow = row.makeRandomAccess()
if try randomAccessRow["application_name"].decode(String?.self) == applicationName {
applicationNameFound += 1
}
}

XCTAssertGreaterThanOrEqual(applicationNameFound, 1)

taskGroup.cancelAll()
}
}


func testQueryDirectly() async throws {
var mlogger = Logger(label: "test")
mlogger.logLevel = .debug
Expand Down

0 comments on commit f2a6394

Please sign in to comment.