Skip to content

Commit

Permalink
Support connection pool timeout configuration (#187)
Browse files Browse the repository at this point in the history
* support conn pool timeout options

* increase timeout

* improve ci

* 10 minutes

* use new benchmark pooling

* updates
  • Loading branch information
tanner0101 authored Jul 23, 2020
1 parent d42b46d commit 3a4b45b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
26 changes: 5 additions & 21 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test Matrix
name: test
on:
- pull_request
defaults:
Expand All @@ -15,28 +15,11 @@ jobs:
- mariadb:latest
runner:
# 5.2 Stable
- swift:5.2-xenial
- swift:5.2-bionic
# 5.2 Unstable
- swiftlang/swift:nightly-5.2-xenial
- swiftlang/swift:nightly-5.2-bionic
# 5.3 Unstable
- swiftlang/swift:nightly-5.3-xenial
- swiftlang/swift:nightly-5.3-bionic
# Master Unsable
- swiftlang/swift:nightly-master-xenial
- swiftlang/swift:nightly-master-bionic
- swiftlang/swift:nightly-master-focal
- swiftlang/swift:nightly-master-centos8
- swiftlang/swift:nightly-master-amazonlinux2
include:
- runner: swiftlang/swift:nightly-master-centos8
installcmd: dnf install -y zlib-devel
- runner: swiftlang/swift:nightly-master-amazonlinux2
installcmd: yum install -y zlib-devel
exclude:
- runner: swiftlang/swift:nightly-master-amazonlinux2
dbimage: mysql:8.0
container: ${{ matrix.runner }}
runs-on: ubuntu-latest
services:
Expand All @@ -55,13 +38,12 @@ jobs:
MYSQL_PASSWORD: vapor_password
MYSQL_DATABASE: vapor_database
steps:
- name: Install dependencies
run: ${{ matrix.installcmd }}
- name: Check out code
uses: actions/checkout@v2
- name: Run tests with Thread Sanitizer
run: swift test --enable-test-discovery --sanitize=thread
env:
LOG_LEVEL: info
MYSQL_HOSTNAME_A: mysql-a
MYSQL_HOSTNAME_B: mysql-b
macOS:
Expand All @@ -71,7 +53,8 @@ jobs:
formula: ['mysql@8.0', 'mysql@5.7', 'mariadb']
include:
- username: root
- { 'formula': 'mariadb', 'username': 'runner' }
- formula: mariadb
username: runner
runs-on: macos-latest
steps:
- name: Select latest available Xcode
Expand All @@ -96,6 +79,7 @@ jobs:
- name: Run tests with Thread Sanitizer
run: swift test --enable-test-discovery --sanitize=thread
env:
LOG_LEVEL: info
MYSQL_HOSTNAME_A: '127.0.0.1'
MYSQL_HOSTNAME_B: '127.0.0.1'
MYSQL_DATABASE_A: vapor_database_a
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.library(name: "FluentMySQLDriver", targets: ["FluentMySQLDriver"]),
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-rc.2"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0"),
.package(url: "https://github.com/vapor/mysql-kit.git", from: "4.0.0-rc.1.2"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
],
Expand Down
38 changes: 27 additions & 11 deletions Sources/FluentMySQLDriver/FluentMySQLConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import AsyncKit
import struct NIO.TimeAmount

extension DatabaseConfigurationFactory {
public static func mysql(
unixDomainSocketPath: String,
username: String,
password: String,
database: String? = nil,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) throws -> Self {
let configuration = MySQLConfiguration(
unixDomainSocketPath: unixDomainSocketPath,
Expand All @@ -16,29 +18,37 @@ extension DatabaseConfigurationFactory {
)
return .mysql(
configuration: configuration,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout
)
}
public static func mysql(
url urlString: String,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) throws -> Self {
guard let url = URL(string: urlString) else {
throw FluentMySQLError.invalidURL(urlString)
}
return try self.mysql(url: url, maxConnectionsPerEventLoop: maxConnectionsPerEventLoop)
return try self.mysql(
url: url,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout
)
}

public static func mysql(
url: URL,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) throws -> Self {
guard let configuration = MySQLConfiguration(url: url) else {
throw FluentMySQLError.invalidURL(url.absoluteString)
}
return .mysql(
configuration: configuration,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout
)
}

Expand All @@ -49,7 +59,8 @@ extension DatabaseConfigurationFactory {
password: String,
database: String? = nil,
tlsConfiguration: TLSConfiguration? = .forClient(),
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) -> Self {
return .mysql(
configuration: .init(
Expand All @@ -60,18 +71,21 @@ extension DatabaseConfigurationFactory {
database: database,
tlsConfiguration: tlsConfiguration
),
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout
)
}

public static func mysql(
configuration: MySQLConfiguration,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
connectionPoolTimeout: NIO.TimeAmount = .seconds(10)
) -> Self {
return Self {
FluentMySQLConfiguration(
configuration: configuration,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
connectionPoolTimeout: connectionPoolTimeout,
middleware: []
)
}
Expand All @@ -81,15 +95,17 @@ extension DatabaseConfigurationFactory {
struct FluentMySQLConfiguration: DatabaseConfiguration {
let configuration: MySQLConfiguration
let maxConnectionsPerEventLoop: Int
let connectionPoolTimeout: TimeAmount
var middleware: [AnyModelMiddleware]

func makeDriver(for databases: Databases) -> DatabaseDriver {
let db = MySQLConnectionSource(
configuration: configuration
configuration: self.configuration
)
let pool = EventLoopGroupConnectionPool(
source: db,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
maxConnectionsPerEventLoop: self.maxConnectionsPerEventLoop,
requestTimeout: self.connectionPoolTimeout,
on: databases.eventLoopGroup
)
return _FluentMySQLDriver(pool: pool)
Expand Down
6 changes: 4 additions & 2 deletions Tests/FluentMySQLDriverTests/FluentMySQLDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ final class FluentMySQLDriverTests: XCTestCase {
username: env("MYSQL_USERNAME_A") ?? "vapor_username",
password: env("MYSQL_PASSWORD_A") ?? "vapor_password",
database: databaseA,
tlsConfiguration: .forClient(certificateVerification: .none)
tlsConfiguration: .forClient(certificateVerification: .none),
connectionPoolTimeout: .seconds(10)
), as: .a)

self.dbs.use(.mysql(
Expand All @@ -306,7 +307,8 @@ final class FluentMySQLDriverTests: XCTestCase {
username: env("MYSQL_USERNAME_B") ?? "vapor_username",
password: env("MYSQL_PASSWORD_B") ?? "vapor_password",
database: databaseB,
tlsConfiguration: .forClient(certificateVerification: .none)
tlsConfiguration: .forClient(certificateVerification: .none),
connectionPoolTimeout: .seconds(10)
), as: .b)

// clear dbs
Expand Down

0 comments on commit 3a4b45b

Please sign in to comment.