Skip to content
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

Support additionalStartupParameters in PostgresClient #521

Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading