Skip to content

Commit 1d0d162

Browse files
committed
EventLoop fixes
1 parent 6582d1e commit 1d0d162

4 files changed

Lines changed: 51 additions & 27 deletions

File tree

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ deps.append("https://github.com/vapor/mysql-nio.git", from: "1.0.0-beta.2", targ
3535
if localDev {
3636
deps.appendLocal("Bridges", targets: "Bridges")
3737
} else {
38-
deps.append("https://github.com/SwifQL/Bridges.git", from: "1.0.0-beta.1", targets: "Bridges")
38+
deps.append("https://github.com/SwifQL/Bridges.git", from: "1.0.0-beta.2", targets: "Bridges")
3939
}
4040

4141
// MARK: - Package

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Work with MySQL with SwifQL through its pure NIO driver.
1515
## Installation
1616

1717
```swift
18-
.package(url: "https://github.com/SwifQL/MySQLBridge.git", from:"1.0.0-beta.1"),
19-
.package(url: "https://github.com/SwifQL/VaporBridges.git", from:"1.0.0-beta.1"),
18+
.package(url: "https://github.com/SwifQL/MySQLBridge.git", from:"1.0.0-beta.2"),
19+
.package(url: "https://github.com/SwifQL/VaporBridges.git", from:"1.0.0-beta.2"),
2020
.target(name: "App", dependencies: ["Vapor", "MySQLBridge", "VaporBridges"]),
2121
```
2222

Sources/MySQLBridge/Extensions/Bridges+Application.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import Bridges
99

1010
extension BridgesApplication {
1111
public var mysql: MySQLBridge {
12-
bridges.bridge(to: MySQLBridge.self)
12+
.init(bridges.bridge(to: _MySQLBridge.self, on: eventLoopGroup.next()))
1313
}
1414
}
1515

1616
extension BridgesRequest {
1717
public var mysql: MySQLBridge {
18-
bridgesApplication.mysql
18+
.init(bridgesApplication.bridges.bridge(to: _MySQLBridge.self, on: eventLoop))
1919
}
2020
}
2121

2222
import NIO
2323
import Logging
2424

25-
extension MySQLBridge {
25+
extension _MySQLBridge {
2626
public static func create(eventLoopGroup: EventLoopGroup, logger: Logger) -> AnyBridge {
27-
MySQLBridge(eventLoopGroup: eventLoopGroup, logger: logger)
27+
_MySQLBridge(eventLoopGroup: eventLoopGroup, logger: logger)
2828
}
2929
}

Sources/MySQLBridge/MySQLBridge.swift

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,54 @@ import AsyncKit
44
import Bridges
55
import Logging
66

7-
public final class MySQLBridge: Bridgeable {
8-
public typealias Source = MySQLConnectionSource
9-
public typealias Database = MySQLDatabase
10-
public typealias Connection = MySQLConnection
7+
public struct MySQLBridge {
8+
let context: BridgeWithContext<_MySQLBridge>
9+
10+
init (_ context: BridgeWithContext<_MySQLBridge>) {
11+
self.context = context
12+
}
13+
14+
public func connection<T>(to db: DatabaseIdentifier,
15+
_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
16+
context.bridge.connection(to: db, on: context.eventLoop, closure)
17+
}
18+
19+
public func transaction<T>(to db: DatabaseIdentifier,
20+
_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
21+
context.bridge.transaction(to: db, on: context.eventLoop, closure)
22+
}
23+
24+
public func register(_ db: DatabaseIdentifier) {
25+
context.bridge.register(db)
26+
}
1127

12-
public static var dialect: SQLDialect { .mysql }
28+
public func migrator(for db: DatabaseIdentifier) -> Migrator {
29+
BridgeDatabaseMigrations<_MySQLBridge>(context.bridge, db: db)
30+
}
31+
}
32+
33+
final class _MySQLBridge: Bridgeable {
34+
typealias Source = MySQLConnectionSource
35+
typealias Database = MySQLDatabase
36+
typealias Connection = MySQLConnection
37+
38+
static var dialect: SQLDialect { .mysql }
1339

14-
public var pools: [String: GroupPool] = [:]
40+
var pools: [String: GroupPool] = [:]
1541

16-
public let logger: Logger
17-
public let eventLoopGroup: EventLoopGroup
42+
let logger: Logger
43+
let eventLoopGroup: EventLoopGroup
1844

19-
required public init (eventLoopGroup: EventLoopGroup, logger: Logger) {
45+
required init (eventLoopGroup: EventLoopGroup, logger: Logger) {
2046
self.eventLoopGroup = eventLoopGroup
2147
self.logger = logger
2248
}
2349

2450
/// Gives a connection to the database and closes it automatically in both success and error cases
25-
public func connection<T>(to db: DatabaseIdentifier,
26-
_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
27-
self.db(db).withConnection { conn in
51+
func connection<T>(to db: DatabaseIdentifier,
52+
on eventLoop: EventLoop,
53+
_ closure: @escaping (MySQLConnection) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
54+
self.db(db, on: eventLoop).withConnection { conn in
2855
closure(conn).flatMap { result in
2956
if conn.isClosed {
3057
return conn.eventLoop.future(result)
@@ -43,8 +70,8 @@ public final class MySQLBridge: Bridgeable {
4370
}
4471
}
4572

46-
public func db(_ db: DatabaseIdentifier) -> MySQLDatabase {
47-
_ConnectionPoolMySQLDatabase(pool: pool(db), logger: logger)
73+
func db(_ db: DatabaseIdentifier, on eventLoop: EventLoop) -> MySQLDatabase {
74+
_ConnectionPoolMySQLDatabase(pool: pool(db, for: eventLoop), logger: logger, eventLoop: eventLoop)
4875
}
4976

5077
deinit {
@@ -55,21 +82,18 @@ public final class MySQLBridge: Bridgeable {
5582
// MARK: Database on pool
5683

5784
extension EventLoopConnectionPool where Source == MySQLConnectionSource {
58-
public func database(logger: Logger) -> MySQLDatabase {
59-
_ConnectionPoolMySQLDatabase(pool: self, logger: logger)
85+
public func database(logger: Logger, on eventLoop: EventLoop) -> MySQLDatabase {
86+
_ConnectionPoolMySQLDatabase(pool: self, logger: logger, eventLoop: eventLoop)
6087
}
6188
}
6289

6390
private struct _ConnectionPoolMySQLDatabase {
6491
let pool: EventLoopConnectionPool<MySQLConnectionSource>
6592
let logger: Logger
93+
let eventLoop: EventLoop
6694
}
6795

6896
extension _ConnectionPoolMySQLDatabase: MySQLDatabase {
69-
var eventLoop: EventLoop {
70-
self.pool.eventLoop
71-
}
72-
7397
func send(_ command: MySQLCommand, logger: Logger) -> EventLoopFuture<Void> {
7498
self.pool.withConnection(logger: logger) {
7599
$0.send(command, logger: logger)

0 commit comments

Comments
 (0)