Skip to content

Commit

Permalink
Remove the global usage counter as it is misleading
Browse files Browse the repository at this point in the history
  • Loading branch information
trasch committed May 12, 2023
1 parent 7af7a75 commit cbe2c1c
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 17 deletions.
2 changes: 0 additions & 2 deletions Sources/PostgresConnectionPool/PoolConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ final class PoolConnection: Identifiable, Equatable {

// TODO: Serialize access
private(set) static var connectionId = 0
private(set) static var globalUsageCounter = 0

private(set) var usageCounter = 0

Expand All @@ -21,7 +20,6 @@ final class PoolConnection: Identifiable, Equatable {
didSet {
if case .active = state {
usageCounter += 1
PoolConnection.globalUsageCounter += 1
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions Sources/PostgresConnectionPool/PoolInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public struct PoolInfo {
public let activeConnections: Int
/// The number of connections that are currently available.
public let availableConnections: Int
/// The total number of queries that were sent to the server.
public let usageCounter: Int

/// Information about individual open connections to the server.
public let connections: [ConnectionInfo]
Expand All @@ -55,16 +53,15 @@ extension PoolInfo: CustomStringConvertible {
public var description: String {
var lines: [String] = [
"Pool: \(name)",
"Connections: \(openConnections)/\(activeConnections)/\(availableConnections) (open/active/available)",
"Usage: \(usageCounter)",
"Shutdown? \(isShutdown) \(shutdownError != nil ? "(\(shutdownError!.description))" : "")",
" Connections: \(openConnections)/\(activeConnections)/\(availableConnections) (open/active/available)",
" Is shut down? \(isShutdown) \(shutdownError != nil ? "(\(shutdownError!.description))" : "")",
]

if connections.isNotEmpty {
lines.append("Connections:")
lines.append(" Connections:")

for connection in connections.sorted(by: { $0.id < $1.id }) {
lines.append(contentsOf: connection.description.components(separatedBy: "\n").map({ " " + $0 }))
lines.append(contentsOf: connection.description.components(separatedBy: "\n").map({ " " + $0 }))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ public actor PostgresConnectionPool {
openConnections: connections.count,
activeConnections: connections.count - available.count,
availableConnections: available.count,
usageCounter: PoolConnection.globalUsageCounter,
connections: connections,
isShutdown: isShutdown,
shutdownError: shutdownError)
Expand Down
7 changes: 0 additions & 7 deletions Tests/PostgresConnectionPoolTests/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ final class ConnectionTests: XCTestCase {
}

func testPoolInfo() async throws {
let initialUsageCounter = PoolConnection.globalUsageCounter
let pool = PostgresConnectionPool(configuration: PostgresHelpers.poolConfiguration(), logger: logger)

let poolInfoBefore = await pool.poolInfo()
print(poolInfoBefore)
XCTAssertEqual(poolInfoBefore.activeConnections, 0)
XCTAssertEqual(poolInfoBefore.availableConnections, 0)
XCTAssertEqual(poolInfoBefore.openConnections, 0)
XCTAssertEqual(poolInfoBefore.usageCounter, initialUsageCounter)
XCTAssertEqual(poolInfoBefore.connections.count, poolInfoBefore.openConnections)
XCTAssertFalse(poolInfoBefore.isShutdown)
XCTAssertNil(poolInfoBefore.shutdownError)
Expand All @@ -66,7 +64,6 @@ final class ConnectionTests: XCTestCase {
XCTAssertEqual(poolInfo.activeConnections, 0)
XCTAssertGreaterThan(poolInfo.availableConnections, 0)
XCTAssertGreaterThan(poolInfo.openConnections, 0)
XCTAssertEqual(poolInfo.usageCounter, 1000 + initialUsageCounter)
XCTAssertEqual(poolInfo.connections.count, poolInfo.openConnections)
XCTAssertFalse(poolInfo.isShutdown)
XCTAssertNil(poolInfo.shutdownError)
Expand All @@ -78,14 +75,12 @@ final class ConnectionTests: XCTestCase {
XCTAssertEqual(poolInfoAfterShutdown.activeConnections, 0)
XCTAssertEqual(poolInfoAfterShutdown.availableConnections, 0)
XCTAssertEqual(poolInfoAfterShutdown.openConnections, 0)
XCTAssertGreaterThan(poolInfoAfterShutdown.usageCounter, 0)
XCTAssertEqual(poolInfoAfterShutdown.connections.count, 0)
XCTAssertTrue(poolInfoAfterShutdown.isShutdown)
XCTAssertNil(poolInfoAfterShutdown.shutdownError)
}

func testPoolSize100() async throws {
let initialUsageCounter = PoolConnection.globalUsageCounter
let pool = PostgresConnectionPool(configuration: PostgresHelpers.poolConfiguration(poolSize: 100), logger: logger)

let start = 1
Expand All @@ -105,7 +100,6 @@ final class ConnectionTests: XCTestCase {
XCTAssertEqual(poolInfo.activeConnections, 0)
XCTAssertGreaterThan(poolInfo.availableConnections, 0)
XCTAssertGreaterThan(poolInfo.openConnections, 0)
XCTAssertEqual(poolInfo.usageCounter, 10000 + initialUsageCounter)
XCTAssertEqual(poolInfo.connections.count, poolInfo.openConnections)
XCTAssertFalse(poolInfo.isShutdown)
XCTAssertNil(poolInfo.shutdownError)
Expand All @@ -116,7 +110,6 @@ final class ConnectionTests: XCTestCase {
XCTAssertEqual(poolInfoIdleClosed.activeConnections, 0)
XCTAssertEqual(poolInfoIdleClosed.availableConnections, 0)
XCTAssertEqual(poolInfoIdleClosed.openConnections, 0)
XCTAssertEqual(poolInfoIdleClosed.usageCounter, 10000 + initialUsageCounter)
XCTAssertEqual(poolInfoIdleClosed.connections.count, poolInfoIdleClosed.openConnections)
XCTAssertFalse(poolInfoIdleClosed.isShutdown)
XCTAssertNil(poolInfoIdleClosed.shutdownError)
Expand Down

0 comments on commit cbe2c1c

Please sign in to comment.