-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Outdooractive/pool_info
Added PostgresConnectionPool.poolInfo()
- Loading branch information
Showing
7 changed files
with
173 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Created by Thomas Rasch on 24.04.23. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum PoolConnectionState: Equatable { | ||
case active(Date) | ||
case available | ||
case closed | ||
case connecting | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// Created by Thomas Rasch on 24.04.23. | ||
// | ||
|
||
import Foundation | ||
|
||
/// General information about the pool and its open connections. | ||
public struct PoolInfo { | ||
|
||
/// Information about an open connection. | ||
public struct ConnectionInfo { | ||
public var id: Int | ||
public var name: String | ||
public var usageCounter: Int | ||
public var query: String? | ||
public var queryRuntime: TimeInterval? | ||
public var state: PoolConnectionState | ||
} | ||
|
||
public var name: String | ||
public var openConnections: Int | ||
public var activeConnections: Int | ||
public var availableConnections: Int | ||
|
||
public var connections: [ConnectionInfo] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
Sources/PostgresConnectionPool/PostgresConnectionWrapper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Created by Thomas Rasch on 24.04.23. | ||
// | ||
|
||
import Foundation | ||
import PostgresNIO | ||
|
||
public final class PostgresConnectionWrapper { | ||
|
||
private let poolConnection: PoolConnection | ||
private let postgresConnection: PostgresConnection | ||
|
||
static func distribute<T>( | ||
poolConnection: PoolConnection?, | ||
callback: (PostgresConnectionWrapper) async throws -> T) | ||
async throws -> T | ||
{ | ||
guard let poolConnection, | ||
let connectionWrapper = PostgresConnectionWrapper(poolConnection) | ||
else { throw PoolError.unknown } | ||
|
||
connectionWrapper.poolConnection.query = nil | ||
let result = try await callback(connectionWrapper) | ||
connectionWrapper.poolConnection.query = nil | ||
|
||
return result | ||
} | ||
|
||
init?(_ poolConnection: PoolConnection) { | ||
guard let postgresConnection = poolConnection.connection else { return nil } | ||
|
||
self.poolConnection = poolConnection | ||
self.postgresConnection = postgresConnection | ||
} | ||
|
||
// MARK: - Public interface, from PostgresConnection | ||
|
||
/// A logger to use in case | ||
public var logger: Logger { | ||
postgresConnection.logger | ||
} | ||
|
||
public var isClosed: Bool { | ||
postgresConnection.isClosed | ||
} | ||
|
||
/// Run a query on the Postgres server the connection is connected to. | ||
/// | ||
/// - Parameters: | ||
/// - query: The ``PostgresQuery`` to run | ||
/// - logger: The `Logger` to log into for the query | ||
/// - file: The file, the query was started in. Used for better error reporting. | ||
/// - line: The line, the query was started in. Used for better error reporting. | ||
/// - Returns: A ``PostgresRowSequence`` containing the rows the server sent as the query result. | ||
/// The sequence can be discarded. | ||
@discardableResult | ||
public func query( | ||
_ query: PostgresQuery, | ||
logger: Logger, | ||
file: String = #fileID, | ||
line: Int = #line) | ||
async throws -> PostgresRowSequence | ||
{ | ||
poolConnection.query = query.sql | ||
return try await postgresConnection.query(query, logger: logger, file: file, line: line) | ||
} | ||
|
||
/// Add a handler for NotificationResponse messages on a certain channel. This is used in conjunction with PostgreSQL's `LISTEN`/`NOTIFY` support: to listen on a channel, you add a listener using this method to handle the NotificationResponse messages, then issue a `LISTEN` query to instruct PostgreSQL to begin sending NotificationResponse messages. | ||
@discardableResult | ||
public func addListener( | ||
channel: String, | ||
handler notificationHandler: @escaping (PostgresListenContext, PostgresMessage.NotificationResponse) -> Void) | ||
-> PostgresListenContext | ||
{ | ||
postgresConnection.addListener(channel: channel, handler: notificationHandler) | ||
} | ||
|
||
} |